Joonwoo Park | 1277cb6 | 2013-03-19 14:16:51 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2011-2013, 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); |
| 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 Park | 18383dc | 2012-09-20 17:45:57 -0700 | [diff] [blame] | 159 | 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 Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 162 | WCD9XXX_SYSTEM_RESUME_TIMEOUT_MS, wcd9xxx_res->pm_state, |
| 163 | wcd9xxx_res->wlock_holders); |
| 164 | wcd9xxx_unlock_sleep(wcd9xxx_res); |
Joonwoo Park | d7cf2e9 | 2012-03-19 19:38:23 -0700 | [diff] [blame] | 165 | return false; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 166 | } |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 167 | wake_up_all(&wcd9xxx_res->pm_wq); |
Joonwoo Park | d7cf2e9 | 2012-03-19 19:38:23 -0700 | [diff] [blame] | 168 | return true; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 169 | } |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 170 | EXPORT_SYMBOL(wcd9xxx_lock_sleep); |
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 | void wcd9xxx_unlock_sleep( |
| 173 | struct wcd9xxx_core_resource *wcd9xxx_res) |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 174 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 175 | mutex_lock(&wcd9xxx_res->pm_lock); |
| 176 | if (--wcd9xxx_res->wlock_holders == 0) { |
Joonwoo Park | 18383dc | 2012-09-20 17:45:57 -0700 | [diff] [blame] | 177 | pr_debug("%s: releasing wake lock pm_state %d -> %d\n", |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 178 | __func__, wcd9xxx_res->pm_state, WCD9XXX_PM_SLEEPABLE); |
Joonwoo Park | 18383dc | 2012-09-20 17:45:57 -0700 | [diff] [blame] | 179 | /* |
| 180 | * if wcd9xxx_lock_sleep failed, pm_state would be still |
| 181 | * WCD9XXX_PM_ASLEEP, don't overwrite |
| 182 | */ |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 183 | 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 Boyd | 2fcabf9 | 2012-05-30 10:41:11 -0700 | [diff] [blame] | 186 | PM_QOS_DEFAULT_VALUE); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 187 | } |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 188 | mutex_unlock(&wcd9xxx_res->pm_lock); |
| 189 | wake_up_all(&wcd9xxx_res->pm_wq); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 190 | } |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 191 | EXPORT_SYMBOL(wcd9xxx_unlock_sleep); |
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 | void wcd9xxx_nested_irq_lock(struct wcd9xxx_core_resource *wcd9xxx_res) |
Joonwoo Park | 1f9d7fd | 2013-01-07 12:40:03 -0800 | [diff] [blame] | 194 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 195 | mutex_lock(&wcd9xxx_res->nested_irq_lock); |
Joonwoo Park | 1f9d7fd | 2013-01-07 12:40:03 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 198 | void wcd9xxx_nested_irq_unlock(struct wcd9xxx_core_resource *wcd9xxx_res) |
Joonwoo Park | 1f9d7fd | 2013-01-07 12:40:03 -0800 | [diff] [blame] | 199 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 200 | mutex_unlock(&wcd9xxx_res->nested_irq_lock); |
Joonwoo Park | 1f9d7fd | 2013-01-07 12:40:03 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Joonwoo Park | 055ae4f | 2013-05-28 19:09:56 -0700 | [diff] [blame] | 203 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 204 | static void wcd9xxx_irq_dispatch(struct wcd9xxx_core_resource *wcd9xxx_res, |
| 205 | struct intr_data *irqdata) |
Joonwoo Park | 055ae4f | 2013-05-28 19:09:56 -0700 | [diff] [blame] | 206 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 207 | 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 Park | 0332483 | 2012-03-19 19:36:16 -0700 | [diff] [blame] | 212 | } |
Joonwoo Park | 0332483 | 2012-03-19 19:36:16 -0700 | [diff] [blame] | 213 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 214 | 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 Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 239 | static irqreturn_t wcd9xxx_irq_thread(int irq, void *data) |
| 240 | { |
| 241 | int ret; |
Joonwoo Park | 0332483 | 2012-03-19 19:36:16 -0700 | [diff] [blame] | 242 | int i; |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 243 | struct intr_data irqdata; |
Joonwoo Park | 1c958f1a | 2013-04-23 19:41:46 -0700 | [diff] [blame] | 244 | char linebuf[128]; |
Joonwoo Park | 1c958f1a | 2013-04-23 19:41:46 -0700 | [diff] [blame] | 245 | static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 1); |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 246 | 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 Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 249 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 250 | if (unlikely(wcd9xxx_lock_sleep(wcd9xxx_res) == false)) { |
| 251 | dev_err(wcd9xxx_res->dev, "Failed to hold suspend\n"); |
Joonwoo Park | d7cf2e9 | 2012-03-19 19:38:23 -0700 | [diff] [blame] | 252 | return IRQ_NONE; |
| 253 | } |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 254 | |
| 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 Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 264 | num_irq_regs, status); |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 265 | |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 266 | if (ret < 0) { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 267 | dev_err(wcd9xxx_res->dev, |
| 268 | "Failed to read interrupt status: %d\n", ret); |
| 269 | goto err_disable_irq; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 270 | } |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 271 | |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 272 | /* Apply masking */ |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 273 | for (i = 0; i < num_irq_regs; i++) |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 274 | status[i] &= ~wcd9xxx_res->irq_masks_cur[i]; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 275 | |
Joonwoo Park | 1c958f1a | 2013-04-23 19:41:46 -0700 | [diff] [blame] | 276 | memcpy(status1, status, sizeof(status1)); |
| 277 | |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 278 | /* Find out which interrupt was triggered and call that interrupt's |
| 279 | * handler function |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 280 | * |
| 281 | * Since codec has only one hardware irq line which is shared by |
Joonwoo Park | 0332483 | 2012-03-19 19:36:16 -0700 | [diff] [blame] | 282 | * codec's different internal interrupts, so it's possible master irq |
| 283 | * handler dispatches multiple nested irq handlers after breaking |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 284 | * 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 Park | 1c958f1a | 2013-04-23 19:41:46 -0700 | [diff] [blame] | 294 | } |
| 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 Park | 66485a9 | 2013-04-25 13:06:34 -0700 | [diff] [blame] | 316 | memset(status, 0xff, num_irq_regs); |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 317 | wcd9xxx_bulk_write(wcd9xxx_res, WCD9XXX_A_INTR_CLEAR0, |
Joonwoo Park | 1c958f1a | 2013-04-23 19:41:46 -0700 | [diff] [blame] | 318 | num_irq_regs, status); |
Joonwoo Park | 66485a9 | 2013-04-25 13:06:34 -0700 | [diff] [blame] | 319 | if (wcd9xxx_get_intf_type() == WCD9XXX_INTERFACE_TYPE_I2C) |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 320 | wcd9xxx_reg_write(wcd9xxx_res, |
| 321 | WCD9XXX_A_INTR_MODE, 0x02); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 322 | } |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 323 | wcd9xxx_unlock_sleep(wcd9xxx_res); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 324 | |
| 325 | return IRQ_HANDLED; |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 326 | |
| 327 | err_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 Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 335 | } |
| 336 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 337 | void wcd9xxx_free_irq(struct wcd9xxx_core_resource *wcd9xxx_res, |
| 338 | int irq, void *data) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 339 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 340 | free_irq(phyirq_to_virq(wcd9xxx_res, irq), data); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 343 | void wcd9xxx_enable_irq(struct wcd9xxx_core_resource *wcd9xxx_res, int irq) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 344 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 345 | enable_irq(phyirq_to_virq(wcd9xxx_res, irq)); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 348 | void wcd9xxx_disable_irq(struct wcd9xxx_core_resource *wcd9xxx_res, int irq) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 349 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 350 | disable_irq_nosync(phyirq_to_virq(wcd9xxx_res, irq)); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 353 | void wcd9xxx_disable_irq_sync( |
| 354 | struct wcd9xxx_core_resource *wcd9xxx_res, int irq) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 355 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 356 | disable_irq(phyirq_to_virq(wcd9xxx_res, irq)); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 359 | static int wcd9xxx_irq_setup_downstream_irq( |
| 360 | struct wcd9xxx_core_resource *wcd9xxx_res) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 361 | { |
| 362 | int irq, virq, ret; |
| 363 | |
| 364 | pr_debug("%s: enter\n", __func__); |
| 365 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 366 | for (irq = 0; irq < wcd9xxx_res->num_irqs; irq++) { |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 367 | /* Map OF irq */ |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 368 | virq = wcd9xxx_map_irq(wcd9xxx_res, irq); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 369 | 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 Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 376 | ret = irq_set_chip_data(virq, wcd9xxx_res); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 377 | if (ret) { |
| 378 | pr_err("%s: Failed to configure irq %d (%d)\n", |
| 379 | __func__, irq, ret); |
| 380 | return ret; |
| 381 | } |
| 382 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 383 | if (wcd9xxx_res->irq_level_high[irq]) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 384 | 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 Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 398 | int wcd9xxx_irq_init(struct wcd9xxx_core_resource *wcd9xxx_res) |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 399 | { |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 400 | int i, ret; |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 401 | u8 irq_level[wcd9xxx_res->num_irq_regs]; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 402 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 403 | mutex_init(&wcd9xxx_res->irq_lock); |
| 404 | mutex_init(&wcd9xxx_res->nested_irq_lock); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 405 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 406 | wcd9xxx_res->irq = wcd9xxx_irq_get_upstream_irq(wcd9xxx_res); |
| 407 | if (!wcd9xxx_res->irq) { |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 408 | pr_warn("%s: irq driver is not yet initialized\n", __func__); |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 409 | mutex_destroy(&wcd9xxx_res->irq_lock); |
| 410 | mutex_destroy(&wcd9xxx_res->nested_irq_lock); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 411 | return -EPROBE_DEFER; |
| 412 | } |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 413 | pr_debug("%s: probed irq %d\n", __func__, wcd9xxx_res->irq); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 414 | |
| 415 | /* Setup downstream IRQs */ |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 416 | ret = wcd9xxx_irq_setup_downstream_irq(wcd9xxx_res); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 417 | if (ret) { |
| 418 | pr_err("%s: Failed to setup downstream IRQ\n", __func__); |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 419 | wcd9xxx_irq_put_upstream_irq(wcd9xxx_res); |
| 420 | mutex_destroy(&wcd9xxx_res->irq_lock); |
| 421 | mutex_destroy(&wcd9xxx_res->nested_irq_lock); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 422 | return ret; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 423 | } |
| 424 | |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 425 | /* All other wcd9xxx interrupts are edge triggered */ |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 426 | wcd9xxx_res->irq_level_high[0] = true; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 427 | |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 428 | /* mask all the interrupts */ |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 429 | 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 Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 433 | irq_level[BIT_BYTE(i)] |= |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 434 | wcd9xxx_res->irq_level_high[i] << (i % BITS_PER_BYTE); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 435 | } |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 436 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 437 | 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 Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 446 | /* Initialize interrupt mask and level registers */ |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 447 | 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 Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 453 | } |
| 454 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 455 | ret = request_threaded_irq(wcd9xxx_res->irq, NULL, wcd9xxx_irq_thread, |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 456 | IRQF_TRIGGER_HIGH | IRQF_ONESHOT, |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 457 | "wcd9xxx", wcd9xxx_res); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 458 | if (ret != 0) |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 459 | dev_err(wcd9xxx_res->dev, "Failed to request IRQ %d: %d\n", |
| 460 | wcd9xxx_res->irq, ret); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 461 | else { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 462 | ret = enable_irq_wake(wcd9xxx_res->irq); |
Joonwoo Park | c98049a | 2013-07-30 16:43:34 -0700 | [diff] [blame] | 463 | if (ret) |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 464 | dev_err(wcd9xxx_res->dev, |
| 465 | "Failed to set wake interrupt on IRQ %d: %d\n", |
| 466 | wcd9xxx_res->irq, ret); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 467 | if (ret) |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 468 | free_irq(wcd9xxx_res->irq, wcd9xxx_res); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 469 | } |
| 470 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 471 | if (ret) |
| 472 | goto fail_irq_init; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 473 | |
| 474 | return ret; |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 475 | |
| 476 | fail_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 Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 483 | } |
| 484 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 485 | int wcd9xxx_request_irq(struct wcd9xxx_core_resource *wcd9xxx_res, |
| 486 | int irq, irq_handler_t handler, |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 487 | const char *name, void *data) |
| 488 | { |
| 489 | int virq; |
| 490 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 491 | virq = phyirq_to_virq(wcd9xxx_res, irq); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 492 | |
| 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 Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 507 | void wcd9xxx_irq_exit(struct wcd9xxx_core_resource *wcd9xxx_res) |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 508 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 509 | 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 Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 515 | /* Release parent's of node */ |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 516 | wcd9xxx_irq_put_upstream_irq(wcd9xxx_res); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 517 | } |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 518 | mutex_destroy(&wcd9xxx_res->irq_lock); |
| 519 | mutex_destroy(&wcd9xxx_res->nested_irq_lock); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 520 | } |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 521 | |
| 522 | #ifndef CONFIG_OF |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 523 | static int phyirq_to_virq( |
| 524 | struct wcd9xxx_core_resource *wcd9xxx_res, |
| 525 | int offset) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 526 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 527 | return wcd9xxx_res->irq_base + offset; |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 530 | static int virq_to_phyirq( |
| 531 | struct wcd9xxx_core_resource *wcd9xxx_res, |
| 532 | int virq) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 533 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 534 | return virq - wcd9xxx_res->irq_base; |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 537 | static unsigned int wcd9xxx_irq_get_upstream_irq( |
| 538 | struct wcd9xxx_core_resource *wcd9xxx_res) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 539 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 540 | return wcd9xxx_res->irq; |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 543 | static void wcd9xxx_irq_put_upstream_irq( |
| 544 | struct wcd9xxx_core_resource *wcd9xxx_res) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 545 | { |
| 546 | /* Do nothing */ |
| 547 | } |
| 548 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 549 | static int wcd9xxx_map_irq( |
| 550 | struct wcd9xxx_core_resource *wcd9xxx_core_res, int irq) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 551 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 552 | return phyirq_to_virq(wcd9xxx_core_res, irq); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 553 | } |
| 554 | #else |
| 555 | int __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 | |
| 583 | static struct wcd9xxx_irq_drv_data * |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 584 | wcd9xxx_get_irq_drv_d(const struct wcd9xxx_core_resource *wcd9xxx_res) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 585 | { |
| 586 | struct device_node *pnode; |
| 587 | struct irq_domain *domain; |
| 588 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 589 | pnode = of_irq_find_parent(wcd9xxx_res->dev->of_node); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 590 | /* 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 Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 598 | 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] | 599 | { |
| 600 | struct wcd9xxx_irq_drv_data *data; |
| 601 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 602 | data = wcd9xxx_get_irq_drv_d(wcd9xxx_res); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 603 | 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 Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 611 | 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] | 612 | { |
| 613 | struct irq_data *irq_data = irq_get_irq_data(virq); |
| 614 | return irq_data->hwirq; |
| 615 | } |
| 616 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 617 | static unsigned int wcd9xxx_irq_get_upstream_irq( |
| 618 | struct wcd9xxx_core_resource *wcd9xxx_res) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 619 | { |
| 620 | struct wcd9xxx_irq_drv_data *data; |
| 621 | |
| 622 | /* Hold parent's of node */ |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 623 | 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] | 624 | return -EINVAL; |
| 625 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 626 | data = wcd9xxx_get_irq_drv_d(wcd9xxx_res); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 627 | 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 Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 636 | static void wcd9xxx_irq_put_upstream_irq( |
| 637 | struct wcd9xxx_core_resource *wcd9xxx_res) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 638 | { |
| 639 | /* Hold parent's of node */ |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 640 | of_node_put(of_irq_find_parent(wcd9xxx_res->dev->of_node)); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 643 | 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] | 644 | { |
Bhalchandra Gajare | 9b4eb3b | 2013-04-30 12:00:41 -0700 | [diff] [blame] | 645 | return of_irq_to_resource(wcd9xxx_res->dev->of_node, irq, NULL); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | static 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 | |
| 672 | static 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 | |
| 685 | static const struct of_device_id of_match[] = { |
| 686 | { .compatible = "qcom,wcd9xxx-irq" }, |
| 687 | { } |
| 688 | }; |
| 689 | |
| 690 | static 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 | |
| 700 | static int wcd9xxx_irq_drv_init(void) |
| 701 | { |
| 702 | return platform_driver_register(&wcd9xxx_irq_driver); |
| 703 | } |
| 704 | subsys_initcall(wcd9xxx_irq_drv_init); |
| 705 | |
| 706 | static void wcd9xxx_irq_drv_exit(void) |
| 707 | { |
| 708 | platform_driver_unregister(&wcd9xxx_irq_driver); |
| 709 | } |
| 710 | module_exit(wcd9xxx_irq_drv_exit); |
| 711 | #endif /* CONFIG_OF */ |