blob: dc32efd75376c8360ef7f3ae417a1befc7cfd853 [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>
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -070018#include <linux/mfd/wcd9xxx/core-resource.h>
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053019#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/delay.h>
22#include <linux/irqdomain.h>
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053023#include <linux/interrupt.h>
Joonwoo Parkf6574c72012-10-10 17:29:57 -070024#include <linux/of.h>
25#include <linux/of_irq.h>
26#include <linux/slab.h>
Joonwoo Park1c958f1a2013-04-23 19:41:46 -070027#include <linux/ratelimit.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
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -070042static int virq_to_phyirq(
43 struct wcd9xxx_core_resource *wcd9xxx_res, int virq);
44static int phyirq_to_virq(
45 struct wcd9xxx_core_resource *wcd9xxx_res, int irq);
46static unsigned int wcd9xxx_irq_get_upstream_irq(
47 struct wcd9xxx_core_resource *wcd9xxx_res);
48static void wcd9xxx_irq_put_upstream_irq(
49 struct wcd9xxx_core_resource *wcd9xxx_res);
50static int wcd9xxx_map_irq(
51 struct wcd9xxx_core_resource *wcd9xxx_res, int irq);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053052
53static void wcd9xxx_irq_lock(struct irq_data *data)
54{
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -070055 struct wcd9xxx_core_resource *wcd9xxx_res =
56 irq_data_get_irq_chip_data(data);
57 mutex_lock(&wcd9xxx_res->irq_lock);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053058}
59
60static void wcd9xxx_irq_sync_unlock(struct irq_data *data)
61{
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -070062 struct wcd9xxx_core_resource *wcd9xxx_res =
63 irq_data_get_irq_chip_data(data);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053064 int i;
65
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -070066 if ((ARRAY_SIZE(wcd9xxx_res->irq_masks_cur) >
67 WCD9XXX_MAX_IRQ_REGS) ||
68 (ARRAY_SIZE(wcd9xxx_res->irq_masks_cache) >
69 WCD9XXX_MAX_IRQ_REGS)) {
Simmi Pateriya4fd69932012-10-26 00:57:06 +053070 pr_err("%s: Array Size out of bound\n", __func__);
71 return;
72 }
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -070073 if (!wcd9xxx_res->codec_reg_write) {
74 pr_err("%s: Codec reg write callback function not defined\n",
75 __func__);
76 return;
77 }
Simmi Pateriya4fd69932012-10-26 00:57:06 +053078
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -070079 for (i = 0; i < ARRAY_SIZE(wcd9xxx_res->irq_masks_cur); i++) {
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053080 /* If there's been a change in the mask write it back
81 * to the hardware.
82 */
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -070083 if (wcd9xxx_res->irq_masks_cur[i] !=
84 wcd9xxx_res->irq_masks_cache[i]) {
85
86 wcd9xxx_res->irq_masks_cache[i] =
87 wcd9xxx_res->irq_masks_cur[i];
88 wcd9xxx_res->codec_reg_write(wcd9xxx_res,
Joonwoo Parkf6574c72012-10-10 17:29:57 -070089 WCD9XXX_A_INTR_MASK0 + i,
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -070090 wcd9xxx_res->irq_masks_cur[i]);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053091 }
92 }
93
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -070094 mutex_unlock(&wcd9xxx_res->irq_lock);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053095}
96
97static void wcd9xxx_irq_enable(struct irq_data *data)
98{
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -070099 struct wcd9xxx_core_resource *wcd9xxx_res =
100 irq_data_get_irq_chip_data(data);
101 int wcd9xxx_irq = virq_to_phyirq(wcd9xxx_res, data->irq);
102 wcd9xxx_res->irq_masks_cur[BIT_BYTE(wcd9xxx_irq)] &=
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530103 ~(BYTE_BIT_MASK(wcd9xxx_irq));
104}
105
106static void wcd9xxx_irq_disable(struct irq_data *data)
107{
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700108 struct wcd9xxx_core_resource *wcd9xxx_res =
109 irq_data_get_irq_chip_data(data);
110 int wcd9xxx_irq = virq_to_phyirq(wcd9xxx_res, data->irq);
111 wcd9xxx_res->irq_masks_cur[BIT_BYTE(wcd9xxx_irq)]
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700112 |= BYTE_BIT_MASK(wcd9xxx_irq);
113}
114
115static void wcd9xxx_irq_mask(struct irq_data *d)
116{
117 /* do nothing but required as linux calls irq_mask without NULL check */
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530118}
119
120static struct irq_chip wcd9xxx_irq_chip = {
121 .name = "wcd9xxx",
122 .irq_bus_lock = wcd9xxx_irq_lock,
123 .irq_bus_sync_unlock = wcd9xxx_irq_sync_unlock,
124 .irq_disable = wcd9xxx_irq_disable,
125 .irq_enable = wcd9xxx_irq_enable,
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700126 .irq_mask = wcd9xxx_irq_mask,
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530127};
128
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700129bool wcd9xxx_lock_sleep(
130 struct wcd9xxx_core_resource *wcd9xxx_res)
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530131{
132 enum wcd9xxx_pm_state os;
133
Joonwoo Park18383dc2012-09-20 17:45:57 -0700134 /*
135 * wcd9xxx_{lock/unlock}_sleep will be called by wcd9xxx_irq_thread
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530136 * and its subroutines only motly.
137 * but btn0_lpress_fn is not wcd9xxx_irq_thread's subroutine and
Joonwoo Park18383dc2012-09-20 17:45:57 -0700138 * It can race with wcd9xxx_irq_thread.
139 * So need to embrace wlock_holders with mutex.
140 *
141 * If system didn't resume, we can simply return false so codec driver's
142 * IRQ handler can return without handling IRQ.
143 * As interrupt line is still active, codec will have another IRQ to
144 * retry shortly.
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530145 */
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700146 mutex_lock(&wcd9xxx_res->pm_lock);
147 if (wcd9xxx_res->wlock_holders++ == 0) {
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700148 pr_debug("%s: holding wake lock\n", __func__);
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700149 pm_qos_update_request(&wcd9xxx_res->pm_qos_req,
Stephen Boyd2fcabf92012-05-30 10:41:11 -0700150 msm_cpuidle_get_deep_idle_latency());
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700151 }
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700152 mutex_unlock(&wcd9xxx_res->pm_lock);
153 os = wcd9xxx_pm_cmpxchg(wcd9xxx_res,
154 WCD9XXX_PM_SLEEPABLE,
155 WCD9XXX_PM_AWAKE);
156 if (!wait_event_timeout(wcd9xxx_res->pm_wq,
157 (os == WCD9XXX_PM_SLEEPABLE ||
158 os == WCD9XXX_PM_AWAKE),
Joonwoo Park18383dc2012-09-20 17:45:57 -0700159 msecs_to_jiffies(WCD9XXX_SYSTEM_RESUME_TIMEOUT_MS))) {
160 pr_warn("%s: system didn't resume within %dms, s %d, w %d\n",
161 __func__,
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700162 WCD9XXX_SYSTEM_RESUME_TIMEOUT_MS, wcd9xxx_res->pm_state,
163 wcd9xxx_res->wlock_holders);
164 wcd9xxx_unlock_sleep(wcd9xxx_res);
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700165 return false;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530166 }
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700167 wake_up_all(&wcd9xxx_res->pm_wq);
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700168 return true;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530169}
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700170EXPORT_SYMBOL(wcd9xxx_lock_sleep);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530171
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700172void wcd9xxx_unlock_sleep(
173 struct wcd9xxx_core_resource *wcd9xxx_res)
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530174{
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700175 mutex_lock(&wcd9xxx_res->pm_lock);
176 if (--wcd9xxx_res->wlock_holders == 0) {
Joonwoo Park18383dc2012-09-20 17:45:57 -0700177 pr_debug("%s: releasing wake lock pm_state %d -> %d\n",
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700178 __func__, wcd9xxx_res->pm_state, WCD9XXX_PM_SLEEPABLE);
Joonwoo Park18383dc2012-09-20 17:45:57 -0700179 /*
180 * if wcd9xxx_lock_sleep failed, pm_state would be still
181 * WCD9XXX_PM_ASLEEP, don't overwrite
182 */
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700183 if (likely(wcd9xxx_res->pm_state == WCD9XXX_PM_AWAKE))
184 wcd9xxx_res->pm_state = WCD9XXX_PM_SLEEPABLE;
185 pm_qos_update_request(&wcd9xxx_res->pm_qos_req,
Stephen Boyd2fcabf92012-05-30 10:41:11 -0700186 PM_QOS_DEFAULT_VALUE);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530187 }
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700188 mutex_unlock(&wcd9xxx_res->pm_lock);
189 wake_up_all(&wcd9xxx_res->pm_wq);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530190}
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700191EXPORT_SYMBOL(wcd9xxx_unlock_sleep);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530192
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700193void wcd9xxx_nested_irq_lock(struct wcd9xxx_core_resource *wcd9xxx_res)
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800194{
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700195 mutex_lock(&wcd9xxx_res->nested_irq_lock);
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800196}
197
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700198void wcd9xxx_nested_irq_unlock(struct wcd9xxx_core_resource *wcd9xxx_res)
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800199{
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700200 mutex_unlock(&wcd9xxx_res->nested_irq_lock);
Joonwoo Park1f9d7fd2013-01-07 12:40:03 -0800201}
202
Joonwoo Park055ae4f2013-05-28 19:09:56 -0700203
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700204static void wcd9xxx_irq_dispatch(struct wcd9xxx_core_resource *wcd9xxx_res,
205 struct intr_data *irqdata)
Joonwoo Park055ae4f2013-05-28 19:09:56 -0700206{
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700207 int irqbit = irqdata->intr_num;
208 if (!wcd9xxx_res->codec_reg_write) {
209 pr_err("%s: codec read/write callback not defined\n",
210 __func__);
211 return;
Joonwoo Park03324832012-03-19 19:36:16 -0700212 }
Joonwoo Park03324832012-03-19 19:36:16 -0700213
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700214 if (irqdata->clear_first) {
215 wcd9xxx_nested_irq_lock(wcd9xxx_res);
216 wcd9xxx_res->codec_reg_write(wcd9xxx_res,
217 WCD9XXX_A_INTR_CLEAR0 + BIT_BYTE(irqbit),
218 BYTE_BIT_MASK(irqbit));
219
220 if (wcd9xxx_get_intf_type() == WCD9XXX_INTERFACE_TYPE_I2C)
221 wcd9xxx_res->codec_reg_write(wcd9xxx_res,
222 WCD9XXX_A_INTR_MODE, 0x02);
223 handle_nested_irq(phyirq_to_virq(wcd9xxx_res, irqbit));
224 wcd9xxx_nested_irq_unlock(wcd9xxx_res);
225 } else {
226 wcd9xxx_nested_irq_lock(wcd9xxx_res);
227 handle_nested_irq(phyirq_to_virq(wcd9xxx_res, irqbit));
228 wcd9xxx_res->codec_reg_write(wcd9xxx_res,
229 WCD9XXX_A_INTR_CLEAR0 + BIT_BYTE(irqbit),
230 BYTE_BIT_MASK(irqbit));
231 if (wcd9xxx_get_intf_type() == WCD9XXX_INTERFACE_TYPE_I2C)
232 wcd9xxx_res->codec_reg_write(wcd9xxx_res,
233 WCD9XXX_A_INTR_MODE, 0x02);
234
235 wcd9xxx_nested_irq_unlock(wcd9xxx_res);
236 }
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700237}
238
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530239static irqreturn_t wcd9xxx_irq_thread(int irq, void *data)
240{
241 int ret;
Joonwoo Park03324832012-03-19 19:36:16 -0700242 int i;
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700243 struct intr_data irqdata;
Joonwoo Park1c958f1a2013-04-23 19:41:46 -0700244 char linebuf[128];
Joonwoo Park1c958f1a2013-04-23 19:41:46 -0700245 static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 1);
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700246 struct wcd9xxx_core_resource *wcd9xxx_res = data;
247 int num_irq_regs = wcd9xxx_res->num_irq_regs;
248 u8 status[num_irq_regs], status1[num_irq_regs];
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530249
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700250 if (unlikely(wcd9xxx_lock_sleep(wcd9xxx_res) == false)) {
251 dev_err(wcd9xxx_res->dev, "Failed to hold suspend\n");
Joonwoo Parkd7cf2e92012-03-19 19:38:23 -0700252 return IRQ_NONE;
253 }
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700254
255 if (!wcd9xxx_res->codec_bulk_read) {
256 dev_err(wcd9xxx_res->dev,
257 "%s: Codec Bulk Register read callback not supplied\n",
258 __func__);
259 goto err_disable_irq;
260 }
261
262 ret = wcd9xxx_res->codec_bulk_read(wcd9xxx_res,
263 WCD9XXX_A_INTR_STATUS0,
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700264 num_irq_regs, status);
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700265
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530266 if (ret < 0) {
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700267 dev_err(wcd9xxx_res->dev,
268 "Failed to read interrupt status: %d\n", ret);
269 goto err_disable_irq;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530270 }
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700271
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530272 /* Apply masking */
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700273 for (i = 0; i < num_irq_regs; i++)
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700274 status[i] &= ~wcd9xxx_res->irq_masks_cur[i];
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530275
Joonwoo Park1c958f1a2013-04-23 19:41:46 -0700276 memcpy(status1, status, sizeof(status1));
277
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530278 /* Find out which interrupt was triggered and call that interrupt's
279 * handler function
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700280 *
281 * Since codec has only one hardware irq line which is shared by
Joonwoo Park03324832012-03-19 19:36:16 -0700282 * codec's different internal interrupts, so it's possible master irq
283 * handler dispatches multiple nested irq handlers after breaking
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700284 * order. Dispatch interrupts in the order that is maintained by
285 * the interrupt table.
286 */
287 for (i = 0; i < wcd9xxx_res->intr_table_size; i++) {
288 irqdata = wcd9xxx_res->intr_table[i];
289 if (status[BIT_BYTE(irqdata.intr_num)] &
290 BYTE_BIT_MASK(irqdata.intr_num)) {
291 wcd9xxx_irq_dispatch(wcd9xxx_res, &irqdata);
292 status1[BIT_BYTE(irqdata.intr_num)] &=
293 ~BYTE_BIT_MASK(irqdata.intr_num);
Joonwoo Park1c958f1a2013-04-23 19:41:46 -0700294 }
295 }
296
297 /*
298 * As a failsafe if unhandled irq is found, clear it to prevent
299 * interrupt storm.
300 * Note that we can say there was an unhandled irq only when no irq
301 * handled by nested irq handler since Taiko supports qdsp as irqs'
302 * destination for few irqs. Therefore driver shouldn't clear pending
303 * irqs when few handled while few others not.
304 */
305 if (unlikely(!memcmp(status, status1, sizeof(status)))) {
306 if (__ratelimit(&ratelimit)) {
307 pr_warn("%s: Unhandled irq found\n", __func__);
308 hex_dump_to_buffer(status, sizeof(status), 16, 1,
309 linebuf, sizeof(linebuf), false);
310 pr_warn("%s: status0 : %s\n", __func__, linebuf);
311 hex_dump_to_buffer(status1, sizeof(status1), 16, 1,
312 linebuf, sizeof(linebuf), false);
313 pr_warn("%s: status1 : %s\n", __func__, linebuf);
314 }
315
Joonwoo Park66485a92013-04-25 13:06:34 -0700316 memset(status, 0xff, num_irq_regs);
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700317 wcd9xxx_bulk_write(wcd9xxx_res, WCD9XXX_A_INTR_CLEAR0,
Joonwoo Park1c958f1a2013-04-23 19:41:46 -0700318 num_irq_regs, status);
Joonwoo Park66485a92013-04-25 13:06:34 -0700319 if (wcd9xxx_get_intf_type() == WCD9XXX_INTERFACE_TYPE_I2C)
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700320 wcd9xxx_reg_write(wcd9xxx_res,
321 WCD9XXX_A_INTR_MODE, 0x02);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530322 }
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700323 wcd9xxx_unlock_sleep(wcd9xxx_res);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530324
325 return IRQ_HANDLED;
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700326
327err_disable_irq:
328 dev_err(wcd9xxx_res->dev,
329 "Disable irq %d\n", wcd9xxx_res->irq);
330
331 disable_irq_wake(wcd9xxx_res->irq);
332 disable_irq_nosync(wcd9xxx_res->irq);
333 wcd9xxx_unlock_sleep(wcd9xxx_res);
334 return IRQ_NONE;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530335}
336
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700337void wcd9xxx_free_irq(struct wcd9xxx_core_resource *wcd9xxx_res,
338 int irq, void *data)
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700339{
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700340 free_irq(phyirq_to_virq(wcd9xxx_res, irq), data);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700341}
342
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700343void wcd9xxx_enable_irq(struct wcd9xxx_core_resource *wcd9xxx_res, int irq)
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700344{
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700345 enable_irq(phyirq_to_virq(wcd9xxx_res, irq));
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700346}
347
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700348void wcd9xxx_disable_irq(struct wcd9xxx_core_resource *wcd9xxx_res, int irq)
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700349{
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700350 disable_irq_nosync(phyirq_to_virq(wcd9xxx_res, irq));
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700351}
352
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700353void wcd9xxx_disable_irq_sync(
354 struct wcd9xxx_core_resource *wcd9xxx_res, int irq)
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700355{
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700356 disable_irq(phyirq_to_virq(wcd9xxx_res, irq));
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700357}
358
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700359static int wcd9xxx_irq_setup_downstream_irq(
360 struct wcd9xxx_core_resource *wcd9xxx_res)
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700361{
362 int irq, virq, ret;
363
364 pr_debug("%s: enter\n", __func__);
365
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700366 for (irq = 0; irq < wcd9xxx_res->num_irqs; irq++) {
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700367 /* Map OF irq */
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700368 virq = wcd9xxx_map_irq(wcd9xxx_res, irq);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700369 pr_debug("%s: irq %d -> %d\n", __func__, irq, virq);
370 if (virq == NO_IRQ) {
371 pr_err("%s, No interrupt specifier for irq %d\n",
372 __func__, irq);
373 return NO_IRQ;
374 }
375
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700376 ret = irq_set_chip_data(virq, wcd9xxx_res);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700377 if (ret) {
378 pr_err("%s: Failed to configure irq %d (%d)\n",
379 __func__, irq, ret);
380 return ret;
381 }
382
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700383 if (wcd9xxx_res->irq_level_high[irq])
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700384 irq_set_chip_and_handler(virq, &wcd9xxx_irq_chip,
385 handle_level_irq);
386 else
387 irq_set_chip_and_handler(virq, &wcd9xxx_irq_chip,
388 handle_edge_irq);
389
390 irq_set_nested_thread(virq, 1);
391 }
392
393 pr_debug("%s: leave\n", __func__);
394
395 return 0;
396}
397
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700398int wcd9xxx_irq_init(struct wcd9xxx_core_resource *wcd9xxx_res)
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530399{
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700400 int i, ret;
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700401 u8 irq_level[wcd9xxx_res->num_irq_regs];
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530402
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700403 mutex_init(&wcd9xxx_res->irq_lock);
404 mutex_init(&wcd9xxx_res->nested_irq_lock);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530405
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700406 wcd9xxx_res->irq = wcd9xxx_irq_get_upstream_irq(wcd9xxx_res);
407 if (!wcd9xxx_res->irq) {
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700408 pr_warn("%s: irq driver is not yet initialized\n", __func__);
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700409 mutex_destroy(&wcd9xxx_res->irq_lock);
410 mutex_destroy(&wcd9xxx_res->nested_irq_lock);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700411 return -EPROBE_DEFER;
412 }
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700413 pr_debug("%s: probed irq %d\n", __func__, wcd9xxx_res->irq);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700414
415 /* Setup downstream IRQs */
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700416 ret = wcd9xxx_irq_setup_downstream_irq(wcd9xxx_res);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700417 if (ret) {
418 pr_err("%s: Failed to setup downstream IRQ\n", __func__);
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700419 wcd9xxx_irq_put_upstream_irq(wcd9xxx_res);
420 mutex_destroy(&wcd9xxx_res->irq_lock);
421 mutex_destroy(&wcd9xxx_res->nested_irq_lock);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700422 return ret;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530423 }
424
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700425 /* All other wcd9xxx interrupts are edge triggered */
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700426 wcd9xxx_res->irq_level_high[0] = true;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530427
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700428 /* mask all the interrupts */
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700429 memset(irq_level, 0, wcd9xxx_res->num_irq_regs);
430 for (i = 0; i < wcd9xxx_res->num_irqs; i++) {
431 wcd9xxx_res->irq_masks_cur[BIT_BYTE(i)] |= BYTE_BIT_MASK(i);
432 wcd9xxx_res->irq_masks_cache[BIT_BYTE(i)] |= BYTE_BIT_MASK(i);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700433 irq_level[BIT_BYTE(i)] |=
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700434 wcd9xxx_res->irq_level_high[i] << (i % BITS_PER_BYTE);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530435 }
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700436
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700437 if (!wcd9xxx_res->codec_reg_write) {
438 dev_err(wcd9xxx_res->dev,
439 "%s: Codec Register write callback not defined\n",
440 __func__);
441 ret = -EINVAL;
442 goto fail_irq_init;
443 }
444
445 for (i = 0; i < wcd9xxx_res->num_irq_regs; i++) {
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530446 /* Initialize interrupt mask and level registers */
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700447 wcd9xxx_res->codec_reg_write(wcd9xxx_res,
448 WCD9XXX_A_INTR_LEVEL0 + i,
449 irq_level[i]);
450 wcd9xxx_res->codec_reg_write(wcd9xxx_res,
451 WCD9XXX_A_INTR_MASK0 + i,
452 wcd9xxx_res->irq_masks_cur[i]);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530453 }
454
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700455 ret = request_threaded_irq(wcd9xxx_res->irq, NULL, wcd9xxx_irq_thread,
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530456 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700457 "wcd9xxx", wcd9xxx_res);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530458 if (ret != 0)
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700459 dev_err(wcd9xxx_res->dev, "Failed to request IRQ %d: %d\n",
460 wcd9xxx_res->irq, ret);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530461 else {
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700462 ret = enable_irq_wake(wcd9xxx_res->irq);
Joonwoo Parkc98049a2013-07-30 16:43:34 -0700463 if (ret)
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700464 dev_err(wcd9xxx_res->dev,
465 "Failed to set wake interrupt on IRQ %d: %d\n",
466 wcd9xxx_res->irq, ret);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530467 if (ret)
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700468 free_irq(wcd9xxx_res->irq, wcd9xxx_res);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530469 }
470
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700471 if (ret)
472 goto fail_irq_init;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530473
474 return ret;
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700475
476fail_irq_init:
477 dev_err(wcd9xxx_res->dev,
478 "%s: Failed to init wcd9xxx irq\n", __func__);
479 wcd9xxx_irq_put_upstream_irq(wcd9xxx_res);
480 mutex_destroy(&wcd9xxx_res->irq_lock);
481 mutex_destroy(&wcd9xxx_res->nested_irq_lock);
482 return ret;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530483}
484
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700485int wcd9xxx_request_irq(struct wcd9xxx_core_resource *wcd9xxx_res,
486 int irq, irq_handler_t handler,
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700487 const char *name, void *data)
488{
489 int virq;
490
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700491 virq = phyirq_to_virq(wcd9xxx_res, irq);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700492
493 /*
494 * ARM needs us to explicitly flag the IRQ as valid
495 * and will set them noprobe when we do so.
496 */
497#ifdef CONFIG_ARM
498 set_irq_flags(virq, IRQF_VALID);
499#else
500 set_irq_noprobe(virq);
501#endif
502
503 return request_threaded_irq(virq, NULL, handler, IRQF_TRIGGER_RISING,
504 name, data);
505}
506
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700507void wcd9xxx_irq_exit(struct wcd9xxx_core_resource *wcd9xxx_res)
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530508{
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700509 dev_dbg(wcd9xxx_res->dev, "%s: Cleaning up irq %d\n", __func__,
510 wcd9xxx_res->irq);
511
512 if (wcd9xxx_res->irq) {
513 disable_irq_wake(wcd9xxx_res->irq);
514 free_irq(wcd9xxx_res->irq, wcd9xxx_res);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700515 /* Release parent's of node */
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700516 wcd9xxx_irq_put_upstream_irq(wcd9xxx_res);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530517 }
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700518 mutex_destroy(&wcd9xxx_res->irq_lock);
519 mutex_destroy(&wcd9xxx_res->nested_irq_lock);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530520}
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700521
522#ifndef CONFIG_OF
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700523static int phyirq_to_virq(
524 struct wcd9xxx_core_resource *wcd9xxx_res,
525 int offset)
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700526{
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700527 return wcd9xxx_res->irq_base + offset;
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700528}
529
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700530static int virq_to_phyirq(
531 struct wcd9xxx_core_resource *wcd9xxx_res,
532 int virq)
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700533{
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700534 return virq - wcd9xxx_res->irq_base;
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700535}
536
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700537static unsigned int wcd9xxx_irq_get_upstream_irq(
538 struct wcd9xxx_core_resource *wcd9xxx_res)
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700539{
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700540 return wcd9xxx_res->irq;
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700541}
542
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700543static void wcd9xxx_irq_put_upstream_irq(
544 struct wcd9xxx_core_resource *wcd9xxx_res)
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700545{
546 /* Do nothing */
547}
548
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700549static int wcd9xxx_map_irq(
550 struct wcd9xxx_core_resource *wcd9xxx_core_res, int irq)
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700551{
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700552 return phyirq_to_virq(wcd9xxx_core_res, irq);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700553}
554#else
555int __init wcd9xxx_irq_of_init(struct device_node *node,
556 struct device_node *parent)
557{
558 struct wcd9xxx_irq_drv_data *data;
559
560 pr_debug("%s: node %s, node parent %s\n", __func__,
561 node->name, node->parent->name);
562
563 data = kzalloc(sizeof(*data), GFP_KERNEL);
564 if (!data)
565 return -ENOMEM;
566
567 /*
568 * wcd9xxx_intc interrupt controller supports N to N irq mapping with
569 * single cell binding with irq numbers(offsets) only.
570 * Use irq_domain_simple_ops that has irq_domain_simple_map and
571 * irq_domain_xlate_onetwocell.
572 */
573 data->domain = irq_domain_add_linear(node, WCD9XXX_MAX_NUM_IRQS,
574 &irq_domain_simple_ops, data);
575 if (!data->domain) {
576 kfree(data);
577 return -ENOMEM;
578 }
579
580 return 0;
581}
582
583static struct wcd9xxx_irq_drv_data *
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700584wcd9xxx_get_irq_drv_d(const struct wcd9xxx_core_resource *wcd9xxx_res)
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700585{
586 struct device_node *pnode;
587 struct irq_domain *domain;
588
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700589 pnode = of_irq_find_parent(wcd9xxx_res->dev->of_node);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700590 /* Shouldn't happen */
591 if (unlikely(!pnode))
592 return NULL;
593
594 domain = irq_find_host(pnode);
595 return (struct wcd9xxx_irq_drv_data *)domain->host_data;
596}
597
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700598static int phyirq_to_virq(struct wcd9xxx_core_resource *wcd9xxx_res, int offset)
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700599{
600 struct wcd9xxx_irq_drv_data *data;
601
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700602 data = wcd9xxx_get_irq_drv_d(wcd9xxx_res);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700603 if (!data) {
604 pr_warn("%s: not registered to interrupt controller\n",
605 __func__);
606 return -EINVAL;
607 }
608 return irq_linear_revmap(data->domain, offset);
609}
610
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700611static int virq_to_phyirq(struct wcd9xxx_core_resource *wcd9xxx_res, int virq)
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700612{
613 struct irq_data *irq_data = irq_get_irq_data(virq);
614 return irq_data->hwirq;
615}
616
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700617static unsigned int wcd9xxx_irq_get_upstream_irq(
618 struct wcd9xxx_core_resource *wcd9xxx_res)
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700619{
620 struct wcd9xxx_irq_drv_data *data;
621
622 /* Hold parent's of node */
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700623 if (!of_node_get(of_irq_find_parent(wcd9xxx_res->dev->of_node)))
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700624 return -EINVAL;
625
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700626 data = wcd9xxx_get_irq_drv_d(wcd9xxx_res);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700627 if (!data) {
628 pr_err("%s: interrupt controller is not registerd\n", __func__);
629 return 0;
630 }
631
632 rmb();
633 return data->irq;
634}
635
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700636static void wcd9xxx_irq_put_upstream_irq(
637 struct wcd9xxx_core_resource *wcd9xxx_res)
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700638{
639 /* Hold parent's of node */
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700640 of_node_put(of_irq_find_parent(wcd9xxx_res->dev->of_node));
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700641}
642
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700643static int wcd9xxx_map_irq(struct wcd9xxx_core_resource *wcd9xxx_res, int irq)
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700644{
Bhalchandra Gajare9b4eb3b2013-04-30 12:00:41 -0700645 return of_irq_to_resource(wcd9xxx_res->dev->of_node, irq, NULL);
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700646}
647
648static int __devinit wcd9xxx_irq_probe(struct platform_device *pdev)
649{
650 int irq;
651 struct irq_domain *domain;
652 struct wcd9xxx_irq_drv_data *data;
653 int ret = -EINVAL;
654
655 irq = platform_get_irq_byname(pdev, "cdc-int");
656 if (irq < 0) {
657 dev_err(&pdev->dev, "%s: Couldn't find cdc-int node(%d)\n",
658 __func__, irq);
659 return -EINVAL;
660 } else {
661 dev_dbg(&pdev->dev, "%s: virq = %d\n", __func__, irq);
662 domain = irq_find_host(pdev->dev.of_node);
663 data = (struct wcd9xxx_irq_drv_data *)domain->host_data;
664 data->irq = irq;
665 wmb();
666 ret = 0;
667 }
668
669 return ret;
670}
671
672static int wcd9xxx_irq_remove(struct platform_device *pdev)
673{
674 struct irq_domain *domain;
675 struct wcd9xxx_irq_drv_data *data;
676
677 domain = irq_find_host(pdev->dev.of_node);
678 data = (struct wcd9xxx_irq_drv_data *)domain->host_data;
679 data->irq = 0;
680 wmb();
681
682 return 0;
683}
684
685static const struct of_device_id of_match[] = {
686 { .compatible = "qcom,wcd9xxx-irq" },
687 { }
688};
689
690static struct platform_driver wcd9xxx_irq_driver = {
691 .probe = wcd9xxx_irq_probe,
692 .remove = wcd9xxx_irq_remove,
693 .driver = {
694 .name = "wcd9xxx_intc",
695 .owner = THIS_MODULE,
696 .of_match_table = of_match_ptr(of_match),
697 },
698};
699
700static int wcd9xxx_irq_drv_init(void)
701{
702 return platform_driver_register(&wcd9xxx_irq_driver);
703}
704subsys_initcall(wcd9xxx_irq_drv_init);
705
706static void wcd9xxx_irq_drv_exit(void)
707{
708 platform_driver_unregister(&wcd9xxx_irq_driver);
709}
710module_exit(wcd9xxx_irq_drv_exit);
711#endif /* CONFIG_OF */