Yeleswarapu, Nagaradhesh | bbba6e3 | 2013-12-20 18:09:17 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2011-2014, The Linux Foundation. All rights reserved. |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 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> |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 18 | #include <linux/mfd/wcd9xxx/core-resource.h> |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 19 | #include <linux/mfd/wcd9xxx/wcd9xxx_registers.h> |
| 20 | #include <linux/mfd/wcd9xxx/wcd9310_registers.h> |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 21 | #include <linux/delay.h> |
| 22 | #include <linux/irqdomain.h> |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 23 | #include <linux/interrupt.h> |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 24 | #include <linux/of.h> |
| 25 | #include <linux/of_irq.h> |
| 26 | #include <linux/slab.h> |
Joonwoo Park | 1c958f1a | 2013-04-23 19:41:46 -0700 | [diff] [blame] | 27 | #include <linux/ratelimit.h> |
Stephen Boyd | 2fcabf9 | 2012-05-30 10:41:11 -0700 | [diff] [blame] | 28 | #include <mach/cpuidle.h> |
| 29 | |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 30 | #define BYTE_BIT_MASK(nr) (1UL << ((nr) % BITS_PER_BYTE)) |
| 31 | #define BIT_BYTE(nr) ((nr) / BITS_PER_BYTE) |
| 32 | |
Joonwoo Park | 18383dc | 2012-09-20 17:45:57 -0700 | [diff] [blame] | 33 | #define WCD9XXX_SYSTEM_RESUME_TIMEOUT_MS 100 |
| 34 | |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 35 | #ifdef CONFIG_OF |
| 36 | struct wcd9xxx_irq_drv_data { |
| 37 | struct irq_domain *domain; |
| 38 | int irq; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 39 | }; |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 40 | #endif |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 41 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 42 | static int virq_to_phyirq( |
| 43 | struct wcd9xxx_core_resource *wcd9xxx_res, int virq); |
| 44 | static int phyirq_to_virq( |
| 45 | struct wcd9xxx_core_resource *wcd9xxx_res, int irq); |
| 46 | static unsigned int wcd9xxx_irq_get_upstream_irq( |
| 47 | struct wcd9xxx_core_resource *wcd9xxx_res); |
| 48 | static void wcd9xxx_irq_put_upstream_irq( |
| 49 | struct wcd9xxx_core_resource *wcd9xxx_res); |
| 50 | static int wcd9xxx_map_irq( |
| 51 | struct wcd9xxx_core_resource *wcd9xxx_res, int irq); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 52 | |
| 53 | static void wcd9xxx_irq_lock(struct irq_data *data) |
| 54 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 55 | struct wcd9xxx_core_resource *wcd9xxx_res = |
| 56 | irq_data_get_irq_chip_data(data); |
| 57 | mutex_lock(&wcd9xxx_res->irq_lock); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static void wcd9xxx_irq_sync_unlock(struct irq_data *data) |
| 61 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 62 | struct wcd9xxx_core_resource *wcd9xxx_res = |
| 63 | irq_data_get_irq_chip_data(data); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 64 | int i; |
| 65 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 66 | 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 Pateriya | 4fd6993 | 2012-10-26 00:57:06 +0530 | [diff] [blame] | 70 | pr_err("%s: Array Size out of bound\n", __func__); |
| 71 | return; |
| 72 | } |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 73 | if (!wcd9xxx_res->codec_reg_write) { |
| 74 | pr_err("%s: Codec reg write callback function not defined\n", |
| 75 | __func__); |
| 76 | return; |
| 77 | } |
Simmi Pateriya | 4fd6993 | 2012-10-26 00:57:06 +0530 | [diff] [blame] | 78 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 79 | for (i = 0; i < ARRAY_SIZE(wcd9xxx_res->irq_masks_cur); i++) { |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 80 | /* If there's been a change in the mask write it back |
| 81 | * to the hardware. |
| 82 | */ |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 83 | 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 Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 89 | WCD9XXX_A_INTR_MASK0 + i, |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 90 | wcd9xxx_res->irq_masks_cur[i]); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 94 | mutex_unlock(&wcd9xxx_res->irq_lock); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static void wcd9xxx_irq_enable(struct irq_data *data) |
| 98 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 99 | 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 Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 103 | ~(BYTE_BIT_MASK(wcd9xxx_irq)); |
| 104 | } |
| 105 | |
| 106 | static void wcd9xxx_irq_disable(struct irq_data *data) |
| 107 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 108 | 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 Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 112 | |= BYTE_BIT_MASK(wcd9xxx_irq); |
| 113 | } |
| 114 | |
| 115 | static void wcd9xxx_irq_mask(struct irq_data *d) |
| 116 | { |
| 117 | /* do nothing but required as linux calls irq_mask without NULL check */ |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static 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 Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 126 | .irq_mask = wcd9xxx_irq_mask, |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 127 | }; |
| 128 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 129 | bool wcd9xxx_lock_sleep( |
| 130 | struct wcd9xxx_core_resource *wcd9xxx_res) |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 131 | { |
| 132 | enum wcd9xxx_pm_state os; |
| 133 | |
Joonwoo Park | 18383dc | 2012-09-20 17:45:57 -0700 | [diff] [blame] | 134 | /* |
| 135 | * wcd9xxx_{lock/unlock}_sleep will be called by wcd9xxx_irq_thread |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 136 | * and its subroutines only motly. |
| 137 | * but btn0_lpress_fn is not wcd9xxx_irq_thread's subroutine and |
Joonwoo Park | 18383dc | 2012-09-20 17:45:57 -0700 | [diff] [blame] | 138 | * 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 Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 145 | */ |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 146 | mutex_lock(&wcd9xxx_res->pm_lock); |
| 147 | if (wcd9xxx_res->wlock_holders++ == 0) { |
Joonwoo Park | d7cf2e9 | 2012-03-19 19:38:23 -0700 | [diff] [blame] | 148 | pr_debug("%s: holding wake lock\n", __func__); |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 149 | pm_qos_update_request(&wcd9xxx_res->pm_qos_req, |
Stephen Boyd | 2fcabf9 | 2012-05-30 10:41:11 -0700 | [diff] [blame] | 150 | msm_cpuidle_get_deep_idle_latency()); |
Joonwoo Park | d7cf2e9 | 2012-03-19 19:38:23 -0700 | [diff] [blame] | 151 | } |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 152 | mutex_unlock(&wcd9xxx_res->pm_lock); |
Bhalchandra Gajare | ea7bd6f | 2013-10-17 15:21:25 -0700 | [diff] [blame] | 153 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 154 | if (!wait_event_timeout(wcd9xxx_res->pm_wq, |
Bhalchandra Gajare | ea7bd6f | 2013-10-17 15:21:25 -0700 | [diff] [blame] | 155 | ((os = wcd9xxx_pm_cmpxchg(wcd9xxx_res, |
| 156 | WCD9XXX_PM_SLEEPABLE, |
| 157 | WCD9XXX_PM_AWAKE)) == |
| 158 | WCD9XXX_PM_SLEEPABLE || |
| 159 | (os == WCD9XXX_PM_AWAKE)), |
| 160 | msecs_to_jiffies( |
| 161 | WCD9XXX_SYSTEM_RESUME_TIMEOUT_MS))) { |
Joonwoo Park | 18383dc | 2012-09-20 17:45:57 -0700 | [diff] [blame] | 162 | pr_warn("%s: system didn't resume within %dms, s %d, w %d\n", |
| 163 | __func__, |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 164 | WCD9XXX_SYSTEM_RESUME_TIMEOUT_MS, wcd9xxx_res->pm_state, |
| 165 | wcd9xxx_res->wlock_holders); |
| 166 | wcd9xxx_unlock_sleep(wcd9xxx_res); |
Joonwoo Park | d7cf2e9 | 2012-03-19 19:38:23 -0700 | [diff] [blame] | 167 | return false; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 168 | } |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 169 | wake_up_all(&wcd9xxx_res->pm_wq); |
Joonwoo Park | d7cf2e9 | 2012-03-19 19:38:23 -0700 | [diff] [blame] | 170 | return true; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 171 | } |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 172 | EXPORT_SYMBOL(wcd9xxx_lock_sleep); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 173 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 174 | void wcd9xxx_unlock_sleep( |
| 175 | struct wcd9xxx_core_resource *wcd9xxx_res) |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 176 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 177 | mutex_lock(&wcd9xxx_res->pm_lock); |
| 178 | if (--wcd9xxx_res->wlock_holders == 0) { |
Joonwoo Park | 18383dc | 2012-09-20 17:45:57 -0700 | [diff] [blame] | 179 | pr_debug("%s: releasing wake lock pm_state %d -> %d\n", |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 180 | __func__, wcd9xxx_res->pm_state, WCD9XXX_PM_SLEEPABLE); |
Joonwoo Park | 18383dc | 2012-09-20 17:45:57 -0700 | [diff] [blame] | 181 | /* |
| 182 | * if wcd9xxx_lock_sleep failed, pm_state would be still |
| 183 | * WCD9XXX_PM_ASLEEP, don't overwrite |
| 184 | */ |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 185 | if (likely(wcd9xxx_res->pm_state == WCD9XXX_PM_AWAKE)) |
| 186 | wcd9xxx_res->pm_state = WCD9XXX_PM_SLEEPABLE; |
| 187 | pm_qos_update_request(&wcd9xxx_res->pm_qos_req, |
Stephen Boyd | 2fcabf9 | 2012-05-30 10:41:11 -0700 | [diff] [blame] | 188 | PM_QOS_DEFAULT_VALUE); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 189 | } |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 190 | mutex_unlock(&wcd9xxx_res->pm_lock); |
| 191 | wake_up_all(&wcd9xxx_res->pm_wq); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 192 | } |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 193 | EXPORT_SYMBOL(wcd9xxx_unlock_sleep); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 194 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 195 | void wcd9xxx_nested_irq_lock(struct wcd9xxx_core_resource *wcd9xxx_res) |
Joonwoo Park | 1f9d7fd | 2013-01-07 12:40:03 -0800 | [diff] [blame] | 196 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 197 | mutex_lock(&wcd9xxx_res->nested_irq_lock); |
Joonwoo Park | 1f9d7fd | 2013-01-07 12:40:03 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 200 | void wcd9xxx_nested_irq_unlock(struct wcd9xxx_core_resource *wcd9xxx_res) |
Joonwoo Park | 1f9d7fd | 2013-01-07 12:40:03 -0800 | [diff] [blame] | 201 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 202 | mutex_unlock(&wcd9xxx_res->nested_irq_lock); |
Joonwoo Park | 1f9d7fd | 2013-01-07 12:40:03 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Joonwoo Park | 055ae4f | 2013-05-28 19:09:56 -0700 | [diff] [blame] | 205 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 206 | static void wcd9xxx_irq_dispatch(struct wcd9xxx_core_resource *wcd9xxx_res, |
| 207 | struct intr_data *irqdata) |
Joonwoo Park | 055ae4f | 2013-05-28 19:09:56 -0700 | [diff] [blame] | 208 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 209 | int irqbit = irqdata->intr_num; |
| 210 | if (!wcd9xxx_res->codec_reg_write) { |
| 211 | pr_err("%s: codec read/write callback not defined\n", |
| 212 | __func__); |
| 213 | return; |
Joonwoo Park | 0332483 | 2012-03-19 19:36:16 -0700 | [diff] [blame] | 214 | } |
Joonwoo Park | 0332483 | 2012-03-19 19:36:16 -0700 | [diff] [blame] | 215 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 216 | if (irqdata->clear_first) { |
| 217 | wcd9xxx_nested_irq_lock(wcd9xxx_res); |
| 218 | wcd9xxx_res->codec_reg_write(wcd9xxx_res, |
| 219 | WCD9XXX_A_INTR_CLEAR0 + BIT_BYTE(irqbit), |
| 220 | BYTE_BIT_MASK(irqbit)); |
| 221 | |
| 222 | if (wcd9xxx_get_intf_type() == WCD9XXX_INTERFACE_TYPE_I2C) |
| 223 | wcd9xxx_res->codec_reg_write(wcd9xxx_res, |
| 224 | WCD9XXX_A_INTR_MODE, 0x02); |
| 225 | handle_nested_irq(phyirq_to_virq(wcd9xxx_res, irqbit)); |
| 226 | wcd9xxx_nested_irq_unlock(wcd9xxx_res); |
| 227 | } else { |
| 228 | wcd9xxx_nested_irq_lock(wcd9xxx_res); |
| 229 | handle_nested_irq(phyirq_to_virq(wcd9xxx_res, irqbit)); |
| 230 | wcd9xxx_res->codec_reg_write(wcd9xxx_res, |
| 231 | WCD9XXX_A_INTR_CLEAR0 + BIT_BYTE(irqbit), |
| 232 | BYTE_BIT_MASK(irqbit)); |
| 233 | if (wcd9xxx_get_intf_type() == WCD9XXX_INTERFACE_TYPE_I2C) |
| 234 | wcd9xxx_res->codec_reg_write(wcd9xxx_res, |
| 235 | WCD9XXX_A_INTR_MODE, 0x02); |
| 236 | |
| 237 | wcd9xxx_nested_irq_unlock(wcd9xxx_res); |
| 238 | } |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 241 | static irqreturn_t wcd9xxx_irq_thread(int irq, void *data) |
| 242 | { |
| 243 | int ret; |
Joonwoo Park | 0332483 | 2012-03-19 19:36:16 -0700 | [diff] [blame] | 244 | int i; |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 245 | struct intr_data irqdata; |
Joonwoo Park | 1c958f1a | 2013-04-23 19:41:46 -0700 | [diff] [blame] | 246 | char linebuf[128]; |
Joonwoo Park | 1c958f1a | 2013-04-23 19:41:46 -0700 | [diff] [blame] | 247 | static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 1); |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 248 | struct wcd9xxx_core_resource *wcd9xxx_res = data; |
| 249 | int num_irq_regs = wcd9xxx_res->num_irq_regs; |
| 250 | u8 status[num_irq_regs], status1[num_irq_regs]; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 251 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 252 | if (unlikely(wcd9xxx_lock_sleep(wcd9xxx_res) == false)) { |
| 253 | dev_err(wcd9xxx_res->dev, "Failed to hold suspend\n"); |
Joonwoo Park | d7cf2e9 | 2012-03-19 19:38:23 -0700 | [diff] [blame] | 254 | return IRQ_NONE; |
| 255 | } |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 256 | |
| 257 | if (!wcd9xxx_res->codec_bulk_read) { |
| 258 | dev_err(wcd9xxx_res->dev, |
| 259 | "%s: Codec Bulk Register read callback not supplied\n", |
| 260 | __func__); |
| 261 | goto err_disable_irq; |
| 262 | } |
| 263 | |
| 264 | ret = wcd9xxx_res->codec_bulk_read(wcd9xxx_res, |
| 265 | WCD9XXX_A_INTR_STATUS0, |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 266 | num_irq_regs, status); |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 267 | |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 268 | if (ret < 0) { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 269 | dev_err(wcd9xxx_res->dev, |
| 270 | "Failed to read interrupt status: %d\n", ret); |
| 271 | goto err_disable_irq; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 272 | } |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 273 | |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 274 | /* Apply masking */ |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 275 | for (i = 0; i < num_irq_regs; i++) |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 276 | status[i] &= ~wcd9xxx_res->irq_masks_cur[i]; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 277 | |
Joonwoo Park | 1c958f1a | 2013-04-23 19:41:46 -0700 | [diff] [blame] | 278 | memcpy(status1, status, sizeof(status1)); |
| 279 | |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 280 | /* Find out which interrupt was triggered and call that interrupt's |
| 281 | * handler function |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 282 | * |
| 283 | * Since codec has only one hardware irq line which is shared by |
Joonwoo Park | 0332483 | 2012-03-19 19:36:16 -0700 | [diff] [blame] | 284 | * codec's different internal interrupts, so it's possible master irq |
| 285 | * handler dispatches multiple nested irq handlers after breaking |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 286 | * order. Dispatch interrupts in the order that is maintained by |
| 287 | * the interrupt table. |
| 288 | */ |
| 289 | for (i = 0; i < wcd9xxx_res->intr_table_size; i++) { |
| 290 | irqdata = wcd9xxx_res->intr_table[i]; |
| 291 | if (status[BIT_BYTE(irqdata.intr_num)] & |
| 292 | BYTE_BIT_MASK(irqdata.intr_num)) { |
| 293 | wcd9xxx_irq_dispatch(wcd9xxx_res, &irqdata); |
| 294 | status1[BIT_BYTE(irqdata.intr_num)] &= |
| 295 | ~BYTE_BIT_MASK(irqdata.intr_num); |
Joonwoo Park | 1c958f1a | 2013-04-23 19:41:46 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * As a failsafe if unhandled irq is found, clear it to prevent |
| 301 | * interrupt storm. |
| 302 | * Note that we can say there was an unhandled irq only when no irq |
| 303 | * handled by nested irq handler since Taiko supports qdsp as irqs' |
| 304 | * destination for few irqs. Therefore driver shouldn't clear pending |
| 305 | * irqs when few handled while few others not. |
| 306 | */ |
| 307 | if (unlikely(!memcmp(status, status1, sizeof(status)))) { |
| 308 | if (__ratelimit(&ratelimit)) { |
| 309 | pr_warn("%s: Unhandled irq found\n", __func__); |
| 310 | hex_dump_to_buffer(status, sizeof(status), 16, 1, |
| 311 | linebuf, sizeof(linebuf), false); |
| 312 | pr_warn("%s: status0 : %s\n", __func__, linebuf); |
| 313 | hex_dump_to_buffer(status1, sizeof(status1), 16, 1, |
| 314 | linebuf, sizeof(linebuf), false); |
| 315 | pr_warn("%s: status1 : %s\n", __func__, linebuf); |
| 316 | } |
| 317 | |
Joonwoo Park | 66485a9 | 2013-04-25 13:06:34 -0700 | [diff] [blame] | 318 | memset(status, 0xff, num_irq_regs); |
Yeleswarapu, Nagaradhesh | bbba6e3 | 2013-12-20 18:09:17 +0530 | [diff] [blame] | 319 | |
| 320 | ret = wcd9xxx_res->codec_bulk_write(wcd9xxx_res, |
| 321 | WCD9XXX_A_INTR_CLEAR0, |
| 322 | num_irq_regs, status); |
Joonwoo Park | 66485a9 | 2013-04-25 13:06:34 -0700 | [diff] [blame] | 323 | if (wcd9xxx_get_intf_type() == WCD9XXX_INTERFACE_TYPE_I2C) |
Yeleswarapu, Nagaradhesh | bbba6e3 | 2013-12-20 18:09:17 +0530 | [diff] [blame] | 324 | wcd9xxx_res->codec_reg_write(wcd9xxx_res, |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 325 | WCD9XXX_A_INTR_MODE, 0x02); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 326 | } |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 327 | wcd9xxx_unlock_sleep(wcd9xxx_res); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 328 | |
| 329 | return IRQ_HANDLED; |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 330 | |
| 331 | err_disable_irq: |
| 332 | dev_err(wcd9xxx_res->dev, |
| 333 | "Disable irq %d\n", wcd9xxx_res->irq); |
| 334 | |
| 335 | disable_irq_wake(wcd9xxx_res->irq); |
| 336 | disable_irq_nosync(wcd9xxx_res->irq); |
| 337 | wcd9xxx_unlock_sleep(wcd9xxx_res); |
| 338 | return IRQ_NONE; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 339 | } |
| 340 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 341 | void wcd9xxx_free_irq(struct wcd9xxx_core_resource *wcd9xxx_res, |
| 342 | int irq, void *data) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 343 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 344 | free_irq(phyirq_to_virq(wcd9xxx_res, irq), data); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 347 | void wcd9xxx_enable_irq(struct wcd9xxx_core_resource *wcd9xxx_res, int irq) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 348 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 349 | enable_irq(phyirq_to_virq(wcd9xxx_res, irq)); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 352 | void wcd9xxx_disable_irq(struct wcd9xxx_core_resource *wcd9xxx_res, int irq) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 353 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 354 | disable_irq_nosync(phyirq_to_virq(wcd9xxx_res, irq)); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 357 | void wcd9xxx_disable_irq_sync( |
| 358 | struct wcd9xxx_core_resource *wcd9xxx_res, int irq) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 359 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 360 | disable_irq(phyirq_to_virq(wcd9xxx_res, irq)); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 363 | static int wcd9xxx_irq_setup_downstream_irq( |
| 364 | struct wcd9xxx_core_resource *wcd9xxx_res) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 365 | { |
| 366 | int irq, virq, ret; |
| 367 | |
| 368 | pr_debug("%s: enter\n", __func__); |
| 369 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 370 | for (irq = 0; irq < wcd9xxx_res->num_irqs; irq++) { |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 371 | /* Map OF irq */ |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 372 | virq = wcd9xxx_map_irq(wcd9xxx_res, irq); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 373 | pr_debug("%s: irq %d -> %d\n", __func__, irq, virq); |
| 374 | if (virq == NO_IRQ) { |
| 375 | pr_err("%s, No interrupt specifier for irq %d\n", |
| 376 | __func__, irq); |
| 377 | return NO_IRQ; |
| 378 | } |
| 379 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 380 | ret = irq_set_chip_data(virq, wcd9xxx_res); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 381 | if (ret) { |
| 382 | pr_err("%s: Failed to configure irq %d (%d)\n", |
| 383 | __func__, irq, ret); |
| 384 | return ret; |
| 385 | } |
| 386 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 387 | if (wcd9xxx_res->irq_level_high[irq]) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 388 | irq_set_chip_and_handler(virq, &wcd9xxx_irq_chip, |
| 389 | handle_level_irq); |
| 390 | else |
| 391 | irq_set_chip_and_handler(virq, &wcd9xxx_irq_chip, |
| 392 | handle_edge_irq); |
| 393 | |
| 394 | irq_set_nested_thread(virq, 1); |
| 395 | } |
| 396 | |
| 397 | pr_debug("%s: leave\n", __func__); |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 402 | int wcd9xxx_irq_init(struct wcd9xxx_core_resource *wcd9xxx_res) |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 403 | { |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 404 | int i, ret; |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 405 | u8 irq_level[wcd9xxx_res->num_irq_regs]; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 406 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 407 | mutex_init(&wcd9xxx_res->irq_lock); |
| 408 | mutex_init(&wcd9xxx_res->nested_irq_lock); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 409 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 410 | wcd9xxx_res->irq = wcd9xxx_irq_get_upstream_irq(wcd9xxx_res); |
| 411 | if (!wcd9xxx_res->irq) { |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 412 | pr_warn("%s: irq driver is not yet initialized\n", __func__); |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 413 | mutex_destroy(&wcd9xxx_res->irq_lock); |
| 414 | mutex_destroy(&wcd9xxx_res->nested_irq_lock); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 415 | return -EPROBE_DEFER; |
| 416 | } |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 417 | pr_debug("%s: probed irq %d\n", __func__, wcd9xxx_res->irq); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 418 | |
| 419 | /* Setup downstream IRQs */ |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 420 | ret = wcd9xxx_irq_setup_downstream_irq(wcd9xxx_res); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 421 | if (ret) { |
| 422 | pr_err("%s: Failed to setup downstream IRQ\n", __func__); |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 423 | wcd9xxx_irq_put_upstream_irq(wcd9xxx_res); |
| 424 | mutex_destroy(&wcd9xxx_res->irq_lock); |
| 425 | mutex_destroy(&wcd9xxx_res->nested_irq_lock); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 426 | return ret; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 427 | } |
| 428 | |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 429 | /* All other wcd9xxx interrupts are edge triggered */ |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 430 | wcd9xxx_res->irq_level_high[0] = true; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 431 | |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 432 | /* mask all the interrupts */ |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 433 | memset(irq_level, 0, wcd9xxx_res->num_irq_regs); |
| 434 | for (i = 0; i < wcd9xxx_res->num_irqs; i++) { |
| 435 | wcd9xxx_res->irq_masks_cur[BIT_BYTE(i)] |= BYTE_BIT_MASK(i); |
| 436 | wcd9xxx_res->irq_masks_cache[BIT_BYTE(i)] |= BYTE_BIT_MASK(i); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 437 | irq_level[BIT_BYTE(i)] |= |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 438 | wcd9xxx_res->irq_level_high[i] << (i % BITS_PER_BYTE); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 439 | } |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 440 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 441 | if (!wcd9xxx_res->codec_reg_write) { |
| 442 | dev_err(wcd9xxx_res->dev, |
| 443 | "%s: Codec Register write callback not defined\n", |
| 444 | __func__); |
| 445 | ret = -EINVAL; |
| 446 | goto fail_irq_init; |
| 447 | } |
| 448 | |
| 449 | for (i = 0; i < wcd9xxx_res->num_irq_regs; i++) { |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 450 | /* Initialize interrupt mask and level registers */ |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 451 | wcd9xxx_res->codec_reg_write(wcd9xxx_res, |
| 452 | WCD9XXX_A_INTR_LEVEL0 + i, |
| 453 | irq_level[i]); |
| 454 | wcd9xxx_res->codec_reg_write(wcd9xxx_res, |
| 455 | WCD9XXX_A_INTR_MASK0 + i, |
| 456 | wcd9xxx_res->irq_masks_cur[i]); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 457 | } |
| 458 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 459 | ret = request_threaded_irq(wcd9xxx_res->irq, NULL, wcd9xxx_irq_thread, |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 460 | IRQF_TRIGGER_HIGH | IRQF_ONESHOT, |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 461 | "wcd9xxx", wcd9xxx_res); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 462 | if (ret != 0) |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 463 | dev_err(wcd9xxx_res->dev, "Failed to request IRQ %d: %d\n", |
| 464 | wcd9xxx_res->irq, ret); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 465 | else { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 466 | ret = enable_irq_wake(wcd9xxx_res->irq); |
Joonwoo Park | c98049a | 2013-07-30 16:43:34 -0700 | [diff] [blame] | 467 | if (ret) |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 468 | dev_err(wcd9xxx_res->dev, |
| 469 | "Failed to set wake interrupt on IRQ %d: %d\n", |
| 470 | wcd9xxx_res->irq, ret); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 471 | if (ret) |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 472 | free_irq(wcd9xxx_res->irq, wcd9xxx_res); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 473 | } |
| 474 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 475 | if (ret) |
| 476 | goto fail_irq_init; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 477 | |
| 478 | return ret; |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 479 | |
| 480 | fail_irq_init: |
| 481 | dev_err(wcd9xxx_res->dev, |
| 482 | "%s: Failed to init wcd9xxx irq\n", __func__); |
| 483 | wcd9xxx_irq_put_upstream_irq(wcd9xxx_res); |
| 484 | mutex_destroy(&wcd9xxx_res->irq_lock); |
| 485 | mutex_destroy(&wcd9xxx_res->nested_irq_lock); |
| 486 | return ret; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 487 | } |
| 488 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 489 | int wcd9xxx_request_irq(struct wcd9xxx_core_resource *wcd9xxx_res, |
| 490 | int irq, irq_handler_t handler, |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 491 | const char *name, void *data) |
| 492 | { |
| 493 | int virq; |
| 494 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 495 | virq = phyirq_to_virq(wcd9xxx_res, irq); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 496 | |
| 497 | /* |
| 498 | * ARM needs us to explicitly flag the IRQ as valid |
| 499 | * and will set them noprobe when we do so. |
| 500 | */ |
| 501 | #ifdef CONFIG_ARM |
| 502 | set_irq_flags(virq, IRQF_VALID); |
| 503 | #else |
| 504 | set_irq_noprobe(virq); |
| 505 | #endif |
| 506 | |
| 507 | return request_threaded_irq(virq, NULL, handler, IRQF_TRIGGER_RISING, |
| 508 | name, data); |
| 509 | } |
| 510 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 511 | void wcd9xxx_irq_exit(struct wcd9xxx_core_resource *wcd9xxx_res) |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 512 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 513 | dev_dbg(wcd9xxx_res->dev, "%s: Cleaning up irq %d\n", __func__, |
| 514 | wcd9xxx_res->irq); |
| 515 | |
| 516 | if (wcd9xxx_res->irq) { |
| 517 | disable_irq_wake(wcd9xxx_res->irq); |
| 518 | free_irq(wcd9xxx_res->irq, wcd9xxx_res); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 519 | /* Release parent's of node */ |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 520 | wcd9xxx_irq_put_upstream_irq(wcd9xxx_res); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 521 | } |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 522 | mutex_destroy(&wcd9xxx_res->irq_lock); |
| 523 | mutex_destroy(&wcd9xxx_res->nested_irq_lock); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 524 | } |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 525 | |
| 526 | #ifndef CONFIG_OF |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 527 | static int phyirq_to_virq( |
| 528 | struct wcd9xxx_core_resource *wcd9xxx_res, |
| 529 | int offset) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 530 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 531 | return wcd9xxx_res->irq_base + offset; |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 534 | static int virq_to_phyirq( |
| 535 | struct wcd9xxx_core_resource *wcd9xxx_res, |
| 536 | int virq) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 537 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 538 | return virq - wcd9xxx_res->irq_base; |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 541 | static unsigned int wcd9xxx_irq_get_upstream_irq( |
| 542 | struct wcd9xxx_core_resource *wcd9xxx_res) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 543 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 544 | return wcd9xxx_res->irq; |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 547 | static void wcd9xxx_irq_put_upstream_irq( |
| 548 | struct wcd9xxx_core_resource *wcd9xxx_res) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 549 | { |
| 550 | /* Do nothing */ |
| 551 | } |
| 552 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 553 | static int wcd9xxx_map_irq( |
| 554 | struct wcd9xxx_core_resource *wcd9xxx_core_res, int irq) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 555 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 556 | return phyirq_to_virq(wcd9xxx_core_res, irq); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 557 | } |
| 558 | #else |
| 559 | int __init wcd9xxx_irq_of_init(struct device_node *node, |
| 560 | struct device_node *parent) |
| 561 | { |
| 562 | struct wcd9xxx_irq_drv_data *data; |
| 563 | |
| 564 | pr_debug("%s: node %s, node parent %s\n", __func__, |
| 565 | node->name, node->parent->name); |
| 566 | |
| 567 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 568 | if (!data) |
| 569 | return -ENOMEM; |
| 570 | |
| 571 | /* |
| 572 | * wcd9xxx_intc interrupt controller supports N to N irq mapping with |
| 573 | * single cell binding with irq numbers(offsets) only. |
| 574 | * Use irq_domain_simple_ops that has irq_domain_simple_map and |
| 575 | * irq_domain_xlate_onetwocell. |
| 576 | */ |
| 577 | data->domain = irq_domain_add_linear(node, WCD9XXX_MAX_NUM_IRQS, |
| 578 | &irq_domain_simple_ops, data); |
| 579 | if (!data->domain) { |
| 580 | kfree(data); |
| 581 | return -ENOMEM; |
| 582 | } |
| 583 | |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | static struct wcd9xxx_irq_drv_data * |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 588 | wcd9xxx_get_irq_drv_d(const struct wcd9xxx_core_resource *wcd9xxx_res) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 589 | { |
| 590 | struct device_node *pnode; |
| 591 | struct irq_domain *domain; |
| 592 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 593 | pnode = of_irq_find_parent(wcd9xxx_res->dev->of_node); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 594 | /* Shouldn't happen */ |
| 595 | if (unlikely(!pnode)) |
| 596 | return NULL; |
| 597 | |
| 598 | domain = irq_find_host(pnode); |
| 599 | return (struct wcd9xxx_irq_drv_data *)domain->host_data; |
| 600 | } |
| 601 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 602 | static int phyirq_to_virq(struct wcd9xxx_core_resource *wcd9xxx_res, int offset) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 603 | { |
| 604 | struct wcd9xxx_irq_drv_data *data; |
| 605 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 606 | data = wcd9xxx_get_irq_drv_d(wcd9xxx_res); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 607 | if (!data) { |
| 608 | pr_warn("%s: not registered to interrupt controller\n", |
| 609 | __func__); |
| 610 | return -EINVAL; |
| 611 | } |
| 612 | return irq_linear_revmap(data->domain, offset); |
| 613 | } |
| 614 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 615 | static int virq_to_phyirq(struct wcd9xxx_core_resource *wcd9xxx_res, int virq) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 616 | { |
| 617 | struct irq_data *irq_data = irq_get_irq_data(virq); |
Aravind Kumar | 320ef3a | 2014-03-21 12:32:34 +0530 | [diff] [blame] | 618 | if (unlikely(!irq_data)) { |
| 619 | pr_err("%s: irq_data is NULL", __func__); |
| 620 | return -EINVAL; |
| 621 | } |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 622 | return irq_data->hwirq; |
| 623 | } |
| 624 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 625 | static unsigned int wcd9xxx_irq_get_upstream_irq( |
| 626 | struct wcd9xxx_core_resource *wcd9xxx_res) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 627 | { |
| 628 | struct wcd9xxx_irq_drv_data *data; |
| 629 | |
| 630 | /* Hold parent's of node */ |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 631 | if (!of_node_get(of_irq_find_parent(wcd9xxx_res->dev->of_node))) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 632 | return -EINVAL; |
| 633 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 634 | data = wcd9xxx_get_irq_drv_d(wcd9xxx_res); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 635 | if (!data) { |
| 636 | pr_err("%s: interrupt controller is not registerd\n", __func__); |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | rmb(); |
| 641 | return data->irq; |
| 642 | } |
| 643 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 644 | static void wcd9xxx_irq_put_upstream_irq( |
| 645 | struct wcd9xxx_core_resource *wcd9xxx_res) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 646 | { |
| 647 | /* Hold parent's of node */ |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 648 | of_node_put(of_irq_find_parent(wcd9xxx_res->dev->of_node)); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 651 | static int wcd9xxx_map_irq(struct wcd9xxx_core_resource *wcd9xxx_res, int irq) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 652 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 653 | return of_irq_to_resource(wcd9xxx_res->dev->of_node, irq, NULL); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | static int __devinit wcd9xxx_irq_probe(struct platform_device *pdev) |
| 657 | { |
| 658 | int irq; |
| 659 | struct irq_domain *domain; |
| 660 | struct wcd9xxx_irq_drv_data *data; |
| 661 | int ret = -EINVAL; |
| 662 | |
| 663 | irq = platform_get_irq_byname(pdev, "cdc-int"); |
| 664 | if (irq < 0) { |
| 665 | dev_err(&pdev->dev, "%s: Couldn't find cdc-int node(%d)\n", |
| 666 | __func__, irq); |
| 667 | return -EINVAL; |
| 668 | } else { |
| 669 | dev_dbg(&pdev->dev, "%s: virq = %d\n", __func__, irq); |
| 670 | domain = irq_find_host(pdev->dev.of_node); |
Aravind Kumar | 320ef3a | 2014-03-21 12:32:34 +0530 | [diff] [blame] | 671 | if (unlikely(!domain)) { |
| 672 | pr_err("%s: domain is NULL", __func__); |
| 673 | return -EINVAL; |
| 674 | } |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 675 | data = (struct wcd9xxx_irq_drv_data *)domain->host_data; |
| 676 | data->irq = irq; |
| 677 | wmb(); |
| 678 | ret = 0; |
| 679 | } |
| 680 | |
| 681 | return ret; |
| 682 | } |
| 683 | |
| 684 | static int wcd9xxx_irq_remove(struct platform_device *pdev) |
| 685 | { |
| 686 | struct irq_domain *domain; |
| 687 | struct wcd9xxx_irq_drv_data *data; |
| 688 | |
| 689 | domain = irq_find_host(pdev->dev.of_node); |
Aravind Kumar | 320ef3a | 2014-03-21 12:32:34 +0530 | [diff] [blame] | 690 | if (unlikely(!domain)) { |
| 691 | pr_err("%s: domain is NULL", __func__); |
| 692 | return -EINVAL; |
| 693 | } |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 694 | data = (struct wcd9xxx_irq_drv_data *)domain->host_data; |
| 695 | data->irq = 0; |
| 696 | wmb(); |
| 697 | |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | static const struct of_device_id of_match[] = { |
| 702 | { .compatible = "qcom,wcd9xxx-irq" }, |
| 703 | { } |
| 704 | }; |
| 705 | |
| 706 | static struct platform_driver wcd9xxx_irq_driver = { |
| 707 | .probe = wcd9xxx_irq_probe, |
| 708 | .remove = wcd9xxx_irq_remove, |
| 709 | .driver = { |
| 710 | .name = "wcd9xxx_intc", |
| 711 | .owner = THIS_MODULE, |
| 712 | .of_match_table = of_match_ptr(of_match), |
| 713 | }, |
| 714 | }; |
| 715 | |
| 716 | static int wcd9xxx_irq_drv_init(void) |
| 717 | { |
| 718 | return platform_driver_register(&wcd9xxx_irq_driver); |
| 719 | } |
| 720 | subsys_initcall(wcd9xxx_irq_drv_init); |
| 721 | |
| 722 | static void wcd9xxx_irq_drv_exit(void) |
| 723 | { |
| 724 | platform_driver_unregister(&wcd9xxx_irq_driver); |
| 725 | } |
| 726 | module_exit(wcd9xxx_irq_drv_exit); |
| 727 | #endif /* CONFIG_OF */ |