Duy Truong | 790f06d | 2013-02-13 16:38:12 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2011-2012, 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> |
| 18 | #include <linux/mfd/wcd9xxx/core.h> |
| 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/mfd/wcd9xxx/wcd9xxx-slimslave.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/irqdomain.h> |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 24 | #include <linux/interrupt.h> |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 25 | #include <linux/of.h> |
| 26 | #include <linux/of_irq.h> |
| 27 | #include <linux/slab.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 | |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 42 | static int virq_to_phyirq(struct wcd9xxx *wcd9xxx, int virq); |
| 43 | static int phyirq_to_virq(struct wcd9xxx *wcd9xxx, int irq); |
| 44 | static unsigned int wcd9xxx_irq_get_upstream_irq(struct wcd9xxx *wcd9xxx); |
| 45 | static void wcd9xxx_irq_put_upstream_irq(struct wcd9xxx *wcd9xxx); |
| 46 | static int wcd9xxx_map_irq(struct wcd9xxx *wcd9xxx, int irq); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 47 | |
| 48 | static void wcd9xxx_irq_lock(struct irq_data *data) |
| 49 | { |
| 50 | struct wcd9xxx *wcd9xxx = irq_data_get_irq_chip_data(data); |
| 51 | mutex_lock(&wcd9xxx->irq_lock); |
| 52 | } |
| 53 | |
| 54 | static void wcd9xxx_irq_sync_unlock(struct irq_data *data) |
| 55 | { |
| 56 | struct wcd9xxx *wcd9xxx = irq_data_get_irq_chip_data(data); |
| 57 | int i; |
| 58 | |
Simmi Pateriya | 4fd6993 | 2012-10-26 00:57:06 +0530 | [diff] [blame] | 59 | if (ARRAY_SIZE(wcd9xxx->irq_masks_cur) > WCD9XXX_NUM_IRQ_REGS || |
| 60 | ARRAY_SIZE(wcd9xxx->irq_masks_cache) > WCD9XXX_NUM_IRQ_REGS) { |
| 61 | pr_err("%s: Array Size out of bound\n", __func__); |
| 62 | return; |
| 63 | } |
| 64 | |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 65 | for (i = 0; i < ARRAY_SIZE(wcd9xxx->irq_masks_cur); i++) { |
| 66 | /* If there's been a change in the mask write it back |
| 67 | * to the hardware. |
| 68 | */ |
| 69 | if (wcd9xxx->irq_masks_cur[i] != wcd9xxx->irq_masks_cache[i]) { |
| 70 | wcd9xxx->irq_masks_cache[i] = wcd9xxx->irq_masks_cur[i]; |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 71 | wcd9xxx_reg_write(wcd9xxx, |
| 72 | WCD9XXX_A_INTR_MASK0 + i, |
| 73 | wcd9xxx->irq_masks_cur[i]); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | mutex_unlock(&wcd9xxx->irq_lock); |
| 78 | } |
| 79 | |
| 80 | static void wcd9xxx_irq_enable(struct irq_data *data) |
| 81 | { |
| 82 | struct wcd9xxx *wcd9xxx = irq_data_get_irq_chip_data(data); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 83 | int wcd9xxx_irq = virq_to_phyirq(wcd9xxx, data->irq); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 84 | wcd9xxx->irq_masks_cur[BIT_BYTE(wcd9xxx_irq)] &= |
| 85 | ~(BYTE_BIT_MASK(wcd9xxx_irq)); |
| 86 | } |
| 87 | |
| 88 | static void wcd9xxx_irq_disable(struct irq_data *data) |
| 89 | { |
| 90 | struct wcd9xxx *wcd9xxx = irq_data_get_irq_chip_data(data); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 91 | int wcd9xxx_irq = virq_to_phyirq(wcd9xxx, data->irq); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 92 | wcd9xxx->irq_masks_cur[BIT_BYTE(wcd9xxx_irq)] |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 93 | |= BYTE_BIT_MASK(wcd9xxx_irq); |
| 94 | } |
| 95 | |
| 96 | static void wcd9xxx_irq_mask(struct irq_data *d) |
| 97 | { |
| 98 | /* do nothing but required as linux calls irq_mask without NULL check */ |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static struct irq_chip wcd9xxx_irq_chip = { |
| 102 | .name = "wcd9xxx", |
| 103 | .irq_bus_lock = wcd9xxx_irq_lock, |
| 104 | .irq_bus_sync_unlock = wcd9xxx_irq_sync_unlock, |
| 105 | .irq_disable = wcd9xxx_irq_disable, |
| 106 | .irq_enable = wcd9xxx_irq_enable, |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 107 | .irq_mask = wcd9xxx_irq_mask, |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | enum wcd9xxx_pm_state wcd9xxx_pm_cmpxchg(struct wcd9xxx *wcd9xxx, |
| 111 | enum wcd9xxx_pm_state o, |
| 112 | enum wcd9xxx_pm_state n) |
| 113 | { |
| 114 | enum wcd9xxx_pm_state old; |
| 115 | mutex_lock(&wcd9xxx->pm_lock); |
| 116 | old = wcd9xxx->pm_state; |
| 117 | if (old == o) |
| 118 | wcd9xxx->pm_state = n; |
| 119 | mutex_unlock(&wcd9xxx->pm_lock); |
| 120 | return old; |
| 121 | } |
| 122 | EXPORT_SYMBOL_GPL(wcd9xxx_pm_cmpxchg); |
| 123 | |
Joonwoo Park | d7cf2e9 | 2012-03-19 19:38:23 -0700 | [diff] [blame] | 124 | bool wcd9xxx_lock_sleep(struct wcd9xxx *wcd9xxx) |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 125 | { |
| 126 | enum wcd9xxx_pm_state os; |
| 127 | |
Joonwoo Park | 18383dc | 2012-09-20 17:45:57 -0700 | [diff] [blame] | 128 | /* |
| 129 | * wcd9xxx_{lock/unlock}_sleep will be called by wcd9xxx_irq_thread |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 130 | * and its subroutines only motly. |
| 131 | * but btn0_lpress_fn is not wcd9xxx_irq_thread's subroutine and |
Joonwoo Park | 18383dc | 2012-09-20 17:45:57 -0700 | [diff] [blame] | 132 | * It can race with wcd9xxx_irq_thread. |
| 133 | * So need to embrace wlock_holders with mutex. |
| 134 | * |
| 135 | * If system didn't resume, we can simply return false so codec driver's |
| 136 | * IRQ handler can return without handling IRQ. |
| 137 | * As interrupt line is still active, codec will have another IRQ to |
| 138 | * retry shortly. |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 139 | */ |
| 140 | mutex_lock(&wcd9xxx->pm_lock); |
Joonwoo Park | d7cf2e9 | 2012-03-19 19:38:23 -0700 | [diff] [blame] | 141 | if (wcd9xxx->wlock_holders++ == 0) { |
| 142 | pr_debug("%s: holding wake lock\n", __func__); |
Stephen Boyd | 2fcabf9 | 2012-05-30 10:41:11 -0700 | [diff] [blame] | 143 | pm_qos_update_request(&wcd9xxx->pm_qos_req, |
| 144 | msm_cpuidle_get_deep_idle_latency()); |
Joonwoo Park | d7cf2e9 | 2012-03-19 19:38:23 -0700 | [diff] [blame] | 145 | } |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 146 | mutex_unlock(&wcd9xxx->pm_lock); |
Joonwoo Park | d7cf2e9 | 2012-03-19 19:38:23 -0700 | [diff] [blame] | 147 | if (!wait_event_timeout(wcd9xxx->pm_wq, |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 148 | ((os = wcd9xxx_pm_cmpxchg(wcd9xxx, WCD9XXX_PM_SLEEPABLE, |
| 149 | WCD9XXX_PM_AWAKE)) == |
| 150 | WCD9XXX_PM_SLEEPABLE || |
| 151 | (os == WCD9XXX_PM_AWAKE)), |
Joonwoo Park | 18383dc | 2012-09-20 17:45:57 -0700 | [diff] [blame] | 152 | msecs_to_jiffies(WCD9XXX_SYSTEM_RESUME_TIMEOUT_MS))) { |
| 153 | pr_warn("%s: system didn't resume within %dms, s %d, w %d\n", |
| 154 | __func__, |
| 155 | WCD9XXX_SYSTEM_RESUME_TIMEOUT_MS, wcd9xxx->pm_state, |
| 156 | wcd9xxx->wlock_holders); |
Joonwoo Park | d7cf2e9 | 2012-03-19 19:38:23 -0700 | [diff] [blame] | 157 | wcd9xxx_unlock_sleep(wcd9xxx); |
| 158 | return false; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 159 | } |
| 160 | wake_up_all(&wcd9xxx->pm_wq); |
Joonwoo Park | d7cf2e9 | 2012-03-19 19:38:23 -0700 | [diff] [blame] | 161 | return true; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 162 | } |
| 163 | EXPORT_SYMBOL_GPL(wcd9xxx_lock_sleep); |
| 164 | |
| 165 | void wcd9xxx_unlock_sleep(struct wcd9xxx *wcd9xxx) |
| 166 | { |
| 167 | mutex_lock(&wcd9xxx->pm_lock); |
| 168 | if (--wcd9xxx->wlock_holders == 0) { |
Joonwoo Park | 18383dc | 2012-09-20 17:45:57 -0700 | [diff] [blame] | 169 | pr_debug("%s: releasing wake lock pm_state %d -> %d\n", |
| 170 | __func__, wcd9xxx->pm_state, WCD9XXX_PM_SLEEPABLE); |
| 171 | /* |
| 172 | * if wcd9xxx_lock_sleep failed, pm_state would be still |
| 173 | * WCD9XXX_PM_ASLEEP, don't overwrite |
| 174 | */ |
| 175 | if (likely(wcd9xxx->pm_state == WCD9XXX_PM_AWAKE)) |
| 176 | wcd9xxx->pm_state = WCD9XXX_PM_SLEEPABLE; |
Stephen Boyd | 2fcabf9 | 2012-05-30 10:41:11 -0700 | [diff] [blame] | 177 | pm_qos_update_request(&wcd9xxx->pm_qos_req, |
| 178 | PM_QOS_DEFAULT_VALUE); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 179 | } |
| 180 | mutex_unlock(&wcd9xxx->pm_lock); |
| 181 | wake_up_all(&wcd9xxx->pm_wq); |
| 182 | } |
| 183 | EXPORT_SYMBOL_GPL(wcd9xxx_unlock_sleep); |
| 184 | |
Joonwoo Park | 1f9d7fd | 2013-01-07 12:40:03 -0800 | [diff] [blame] | 185 | void wcd9xxx_nested_irq_lock(struct wcd9xxx *wcd9xxx) |
| 186 | { |
| 187 | mutex_lock(&wcd9xxx->nested_irq_lock); |
| 188 | } |
| 189 | |
| 190 | void wcd9xxx_nested_irq_unlock(struct wcd9xxx *wcd9xxx) |
| 191 | { |
| 192 | mutex_unlock(&wcd9xxx->nested_irq_lock); |
| 193 | } |
| 194 | |
Joonwoo Park | 0332483 | 2012-03-19 19:36:16 -0700 | [diff] [blame] | 195 | static void wcd9xxx_irq_dispatch(struct wcd9xxx *wcd9xxx, int irqbit) |
| 196 | { |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 197 | if ((irqbit <= WCD9XXX_IRQ_MBHC_INSERTION) && |
| 198 | (irqbit >= WCD9XXX_IRQ_MBHC_REMOVAL)) { |
Joonwoo Park | 1f9d7fd | 2013-01-07 12:40:03 -0800 | [diff] [blame] | 199 | wcd9xxx_nested_irq_lock(wcd9xxx); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 200 | wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_CLEAR0 + |
| 201 | BIT_BYTE(irqbit), |
| 202 | BYTE_BIT_MASK(irqbit)); |
Joonwoo Park | 0332483 | 2012-03-19 19:36:16 -0700 | [diff] [blame] | 203 | if (wcd9xxx_get_intf_type() == WCD9XXX_INTERFACE_TYPE_I2C) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 204 | wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_MODE, 0x02); |
| 205 | handle_nested_irq(phyirq_to_virq(wcd9xxx, irqbit)); |
Joonwoo Park | 1f9d7fd | 2013-01-07 12:40:03 -0800 | [diff] [blame] | 206 | wcd9xxx_nested_irq_unlock(wcd9xxx); |
Joonwoo Park | 0332483 | 2012-03-19 19:36:16 -0700 | [diff] [blame] | 207 | } else { |
Joonwoo Park | 1f9d7fd | 2013-01-07 12:40:03 -0800 | [diff] [blame] | 208 | wcd9xxx_nested_irq_lock(wcd9xxx); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 209 | handle_nested_irq(phyirq_to_virq(wcd9xxx, irqbit)); |
| 210 | wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_CLEAR0 + |
| 211 | BIT_BYTE(irqbit), |
| 212 | BYTE_BIT_MASK(irqbit)); |
Joonwoo Park | 0332483 | 2012-03-19 19:36:16 -0700 | [diff] [blame] | 213 | if (wcd9xxx_get_intf_type() == WCD9XXX_INTERFACE_TYPE_I2C) |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 214 | wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_MODE, 0x02); |
Joonwoo Park | 1f9d7fd | 2013-01-07 12:40:03 -0800 | [diff] [blame] | 215 | wcd9xxx_nested_irq_unlock(wcd9xxx); |
Joonwoo Park | 0332483 | 2012-03-19 19:36:16 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 219 | static int wcd9xxx_num_irq_regs(const struct wcd9xxx *wcd9xxx) |
| 220 | { |
| 221 | return (wcd9xxx->num_irqs / 8) + ((wcd9xxx->num_irqs % 8) ? 1 : 0); |
| 222 | } |
| 223 | |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 224 | static irqreturn_t wcd9xxx_irq_thread(int irq, void *data) |
| 225 | { |
| 226 | int ret; |
Joonwoo Park | 0332483 | 2012-03-19 19:36:16 -0700 | [diff] [blame] | 227 | int i; |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 228 | struct wcd9xxx *wcd9xxx = data; |
| 229 | int num_irq_regs = wcd9xxx_num_irq_regs(wcd9xxx); |
| 230 | u8 status[num_irq_regs]; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 231 | |
Joonwoo Park | d7cf2e9 | 2012-03-19 19:38:23 -0700 | [diff] [blame] | 232 | if (unlikely(wcd9xxx_lock_sleep(wcd9xxx) == false)) { |
| 233 | dev_err(wcd9xxx->dev, "Failed to hold suspend\n"); |
| 234 | return IRQ_NONE; |
| 235 | } |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 236 | ret = wcd9xxx_bulk_read(wcd9xxx, WCD9XXX_A_INTR_STATUS0, |
| 237 | num_irq_regs, status); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 238 | if (ret < 0) { |
| 239 | dev_err(wcd9xxx->dev, "Failed to read interrupt status: %d\n", |
| 240 | ret); |
Joonwoo Park | 0732919 | 2012-10-23 13:08:37 -0700 | [diff] [blame] | 241 | dev_err(wcd9xxx->dev, "Disable irq %d\n", wcd9xxx->irq); |
Joonwoo Park | 6183dbd | 2012-10-26 16:39:20 -0700 | [diff] [blame] | 242 | disable_irq_wake(wcd9xxx->irq); |
Joonwoo Park | 0732919 | 2012-10-23 13:08:37 -0700 | [diff] [blame] | 243 | disable_irq_nosync(wcd9xxx->irq); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 244 | wcd9xxx_unlock_sleep(wcd9xxx); |
| 245 | return IRQ_NONE; |
| 246 | } |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 247 | |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 248 | /* Apply masking */ |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 249 | for (i = 0; i < num_irq_regs; i++) |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 250 | status[i] &= ~wcd9xxx->irq_masks_cur[i]; |
| 251 | |
| 252 | /* Find out which interrupt was triggered and call that interrupt's |
| 253 | * handler function |
| 254 | */ |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 255 | if (status[BIT_BYTE(WCD9XXX_IRQ_SLIMBUS)] & |
| 256 | BYTE_BIT_MASK(WCD9XXX_IRQ_SLIMBUS)) |
| 257 | wcd9xxx_irq_dispatch(wcd9xxx, WCD9XXX_IRQ_SLIMBUS); |
Joonwoo Park | 0332483 | 2012-03-19 19:36:16 -0700 | [diff] [blame] | 258 | |
| 259 | /* Since codec has only one hardware irq line which is shared by |
| 260 | * codec's different internal interrupts, so it's possible master irq |
| 261 | * handler dispatches multiple nested irq handlers after breaking |
| 262 | * order. Dispatch MBHC interrupts order to follow MBHC state |
| 263 | * machine's order */ |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 264 | for (i = WCD9XXX_IRQ_MBHC_INSERTION; |
| 265 | i >= WCD9XXX_IRQ_MBHC_REMOVAL; i--) { |
Joonwoo Park | 0332483 | 2012-03-19 19:36:16 -0700 | [diff] [blame] | 266 | if (status[BIT_BYTE(i)] & BYTE_BIT_MASK(i)) |
| 267 | wcd9xxx_irq_dispatch(wcd9xxx, i); |
| 268 | } |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 269 | for (i = WCD9XXX_IRQ_BG_PRECHARGE; i < wcd9xxx->num_irqs; i++) { |
Joonwoo Park | 0332483 | 2012-03-19 19:36:16 -0700 | [diff] [blame] | 270 | if (status[BIT_BYTE(i)] & BYTE_BIT_MASK(i)) |
| 271 | wcd9xxx_irq_dispatch(wcd9xxx, i); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 272 | } |
| 273 | wcd9xxx_unlock_sleep(wcd9xxx); |
| 274 | |
| 275 | return IRQ_HANDLED; |
| 276 | } |
| 277 | |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 278 | void wcd9xxx_free_irq(struct wcd9xxx *wcd9xxx, int irq, void *data) |
| 279 | { |
| 280 | free_irq(phyirq_to_virq(wcd9xxx, irq), data); |
| 281 | } |
| 282 | |
| 283 | void wcd9xxx_enable_irq(struct wcd9xxx *wcd9xxx, int irq) |
| 284 | { |
| 285 | enable_irq(phyirq_to_virq(wcd9xxx, irq)); |
| 286 | } |
| 287 | |
| 288 | void wcd9xxx_disable_irq(struct wcd9xxx *wcd9xxx, int irq) |
| 289 | { |
| 290 | disable_irq_nosync(phyirq_to_virq(wcd9xxx, irq)); |
| 291 | } |
| 292 | |
| 293 | void wcd9xxx_disable_irq_sync(struct wcd9xxx *wcd9xxx, int irq) |
| 294 | { |
| 295 | disable_irq(phyirq_to_virq(wcd9xxx, irq)); |
| 296 | } |
| 297 | |
| 298 | static int wcd9xxx_irq_setup_downstream_irq(struct wcd9xxx *wcd9xxx) |
| 299 | { |
| 300 | int irq, virq, ret; |
| 301 | |
| 302 | pr_debug("%s: enter\n", __func__); |
| 303 | |
| 304 | for (irq = 0; irq < wcd9xxx->num_irqs; irq++) { |
| 305 | /* Map OF irq */ |
| 306 | virq = wcd9xxx_map_irq(wcd9xxx, irq); |
| 307 | pr_debug("%s: irq %d -> %d\n", __func__, irq, virq); |
| 308 | if (virq == NO_IRQ) { |
| 309 | pr_err("%s, No interrupt specifier for irq %d\n", |
| 310 | __func__, irq); |
| 311 | return NO_IRQ; |
| 312 | } |
| 313 | |
| 314 | ret = irq_set_chip_data(virq, wcd9xxx); |
| 315 | if (ret) { |
| 316 | pr_err("%s: Failed to configure irq %d (%d)\n", |
| 317 | __func__, irq, ret); |
| 318 | return ret; |
| 319 | } |
| 320 | |
| 321 | if (wcd9xxx->irq_level_high[irq]) |
| 322 | irq_set_chip_and_handler(virq, &wcd9xxx_irq_chip, |
| 323 | handle_level_irq); |
| 324 | else |
| 325 | irq_set_chip_and_handler(virq, &wcd9xxx_irq_chip, |
| 326 | handle_edge_irq); |
| 327 | |
| 328 | irq_set_nested_thread(virq, 1); |
| 329 | } |
| 330 | |
| 331 | pr_debug("%s: leave\n", __func__); |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 336 | int wcd9xxx_irq_init(struct wcd9xxx *wcd9xxx) |
| 337 | { |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 338 | int i, ret; |
| 339 | u8 irq_level[wcd9xxx_num_irq_regs(wcd9xxx)]; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 340 | |
| 341 | mutex_init(&wcd9xxx->irq_lock); |
Joonwoo Park | 1f9d7fd | 2013-01-07 12:40:03 -0800 | [diff] [blame] | 342 | mutex_init(&wcd9xxx->nested_irq_lock); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 343 | |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 344 | wcd9xxx->irq = wcd9xxx_irq_get_upstream_irq(wcd9xxx); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 345 | if (!wcd9xxx->irq) { |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 346 | pr_warn("%s: irq driver is not yet initialized\n", __func__); |
| 347 | mutex_destroy(&wcd9xxx->irq_lock); |
Joonwoo Park | 1f9d7fd | 2013-01-07 12:40:03 -0800 | [diff] [blame] | 348 | mutex_destroy(&wcd9xxx->nested_irq_lock); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 349 | return -EPROBE_DEFER; |
| 350 | } |
| 351 | pr_debug("%s: probed irq %d\n", __func__, wcd9xxx->irq); |
| 352 | |
| 353 | /* Setup downstream IRQs */ |
| 354 | ret = wcd9xxx_irq_setup_downstream_irq(wcd9xxx); |
| 355 | if (ret) { |
| 356 | pr_err("%s: Failed to setup downstream IRQ\n", __func__); |
| 357 | wcd9xxx_irq_put_upstream_irq(wcd9xxx); |
Joonwoo Park | 1f9d7fd | 2013-01-07 12:40:03 -0800 | [diff] [blame] | 358 | mutex_destroy(&wcd9xxx->irq_lock); |
| 359 | mutex_destroy(&wcd9xxx->nested_irq_lock); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 360 | return ret; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 361 | } |
| 362 | |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 363 | /* All other wcd9xxx interrupts are edge triggered */ |
| 364 | wcd9xxx->irq_level_high[0] = true; |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 365 | |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 366 | /* mask all the interrupts */ |
| 367 | memset(irq_level, 0, wcd9xxx_num_irq_regs(wcd9xxx)); |
| 368 | for (i = 0; i < wcd9xxx->num_irqs; i++) { |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 369 | wcd9xxx->irq_masks_cur[BIT_BYTE(i)] |= BYTE_BIT_MASK(i); |
| 370 | wcd9xxx->irq_masks_cache[BIT_BYTE(i)] |= BYTE_BIT_MASK(i); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 371 | irq_level[BIT_BYTE(i)] |= |
| 372 | wcd9xxx->irq_level_high[i] << (i % BITS_PER_BYTE); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 373 | } |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 374 | |
| 375 | for (i = 0; i < wcd9xxx_num_irq_regs(wcd9xxx); i++) { |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 376 | /* Initialize interrupt mask and level registers */ |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 377 | wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_LEVEL0 + i, |
| 378 | irq_level[i]); |
| 379 | wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_INTR_MASK0 + i, |
| 380 | wcd9xxx->irq_masks_cur[i]); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | ret = request_threaded_irq(wcd9xxx->irq, NULL, wcd9xxx_irq_thread, |
| 384 | IRQF_TRIGGER_HIGH | IRQF_ONESHOT, |
| 385 | "wcd9xxx", wcd9xxx); |
| 386 | if (ret != 0) |
| 387 | dev_err(wcd9xxx->dev, "Failed to request IRQ %d: %d\n", |
| 388 | wcd9xxx->irq, ret); |
| 389 | else { |
| 390 | ret = enable_irq_wake(wcd9xxx->irq); |
| 391 | if (ret == 0) { |
| 392 | ret = device_init_wakeup(wcd9xxx->dev, 1); |
| 393 | if (ret) { |
| 394 | dev_err(wcd9xxx->dev, "Failed to init device" |
| 395 | "wakeup : %d\n", ret); |
| 396 | disable_irq_wake(wcd9xxx->irq); |
| 397 | } |
| 398 | } else |
| 399 | dev_err(wcd9xxx->dev, "Failed to set wake interrupt on" |
| 400 | " IRQ %d: %d\n", wcd9xxx->irq, ret); |
| 401 | if (ret) |
| 402 | free_irq(wcd9xxx->irq, wcd9xxx); |
| 403 | } |
| 404 | |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 405 | if (ret) { |
| 406 | pr_err("%s: Failed to init wcd9xxx irq\n", __func__); |
| 407 | wcd9xxx_irq_put_upstream_irq(wcd9xxx); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 408 | mutex_destroy(&wcd9xxx->irq_lock); |
Joonwoo Park | 1f9d7fd | 2013-01-07 12:40:03 -0800 | [diff] [blame] | 409 | mutex_destroy(&wcd9xxx->nested_irq_lock); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 410 | } |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 411 | |
| 412 | return ret; |
| 413 | } |
| 414 | |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 415 | int wcd9xxx_request_irq(struct wcd9xxx *wcd9xxx, int irq, irq_handler_t handler, |
| 416 | const char *name, void *data) |
| 417 | { |
| 418 | int virq; |
| 419 | |
| 420 | virq = phyirq_to_virq(wcd9xxx, irq); |
| 421 | |
| 422 | /* |
| 423 | * ARM needs us to explicitly flag the IRQ as valid |
| 424 | * and will set them noprobe when we do so. |
| 425 | */ |
| 426 | #ifdef CONFIG_ARM |
| 427 | set_irq_flags(virq, IRQF_VALID); |
| 428 | #else |
| 429 | set_irq_noprobe(virq); |
| 430 | #endif |
| 431 | |
| 432 | return request_threaded_irq(virq, NULL, handler, IRQF_TRIGGER_RISING, |
| 433 | name, data); |
| 434 | } |
| 435 | |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 436 | void wcd9xxx_irq_exit(struct wcd9xxx *wcd9xxx) |
| 437 | { |
| 438 | if (wcd9xxx->irq) { |
| 439 | disable_irq_wake(wcd9xxx->irq); |
| 440 | free_irq(wcd9xxx->irq, wcd9xxx); |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 441 | /* Release parent's of node */ |
| 442 | wcd9xxx_irq_put_upstream_irq(wcd9xxx); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 443 | device_init_wakeup(wcd9xxx->dev, 0); |
| 444 | } |
| 445 | mutex_destroy(&wcd9xxx->irq_lock); |
Joonwoo Park | 1f9d7fd | 2013-01-07 12:40:03 -0800 | [diff] [blame] | 446 | mutex_destroy(&wcd9xxx->nested_irq_lock); |
Asish Bhattacharya | b1aeae2 | 2012-02-15 08:29:28 +0530 | [diff] [blame] | 447 | } |
Joonwoo Park | f6574c7 | 2012-10-10 17:29:57 -0700 | [diff] [blame] | 448 | |
| 449 | #ifndef CONFIG_OF |
| 450 | static int phyirq_to_virq(struct wcd9xxx *wcd9xxx, int offset) |
| 451 | { |
| 452 | return wcd9xxx->irq_base + offset; |
| 453 | } |
| 454 | |
| 455 | static int virq_to_phyirq(struct wcd9xxx *wcd9xxx, int virq) |
| 456 | { |
| 457 | return virq - wcd9xxx->irq_base; |
| 458 | } |
| 459 | |
| 460 | static unsigned int wcd9xxx_irq_get_upstream_irq(struct wcd9xxx *wcd9xxx) |
| 461 | { |
| 462 | return wcd9xxx->irq; |
| 463 | } |
| 464 | |
| 465 | static void wcd9xxx_irq_put_upstream_irq(struct wcd9xxx *wcd9xxx) |
| 466 | { |
| 467 | /* Do nothing */ |
| 468 | } |
| 469 | |
| 470 | static int wcd9xxx_map_irq(struct wcd9xxx *wcd9xxx, int irq) |
| 471 | { |
| 472 | return phyirq_to_virq(wcd9xxx, irq); |
| 473 | } |
| 474 | #else |
| 475 | int __init wcd9xxx_irq_of_init(struct device_node *node, |
| 476 | struct device_node *parent) |
| 477 | { |
| 478 | struct wcd9xxx_irq_drv_data *data; |
| 479 | |
| 480 | pr_debug("%s: node %s, node parent %s\n", __func__, |
| 481 | node->name, node->parent->name); |
| 482 | |
| 483 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 484 | if (!data) |
| 485 | return -ENOMEM; |
| 486 | |
| 487 | /* |
| 488 | * wcd9xxx_intc interrupt controller supports N to N irq mapping with |
| 489 | * single cell binding with irq numbers(offsets) only. |
| 490 | * Use irq_domain_simple_ops that has irq_domain_simple_map and |
| 491 | * irq_domain_xlate_onetwocell. |
| 492 | */ |
| 493 | data->domain = irq_domain_add_linear(node, WCD9XXX_MAX_NUM_IRQS, |
| 494 | &irq_domain_simple_ops, data); |
| 495 | if (!data->domain) { |
| 496 | kfree(data); |
| 497 | return -ENOMEM; |
| 498 | } |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | static struct wcd9xxx_irq_drv_data * |
| 504 | wcd9xxx_get_irq_drv_d(const struct wcd9xxx *wcd9xxx) |
| 505 | { |
| 506 | struct device_node *pnode; |
| 507 | struct irq_domain *domain; |
| 508 | |
| 509 | pnode = of_irq_find_parent(wcd9xxx->dev->of_node); |
| 510 | /* Shouldn't happen */ |
| 511 | if (unlikely(!pnode)) |
| 512 | return NULL; |
| 513 | |
| 514 | domain = irq_find_host(pnode); |
| 515 | return (struct wcd9xxx_irq_drv_data *)domain->host_data; |
| 516 | } |
| 517 | |
| 518 | static int phyirq_to_virq(struct wcd9xxx *wcd9xxx, int offset) |
| 519 | { |
| 520 | struct wcd9xxx_irq_drv_data *data; |
| 521 | |
| 522 | data = wcd9xxx_get_irq_drv_d(wcd9xxx); |
| 523 | if (!data) { |
| 524 | pr_warn("%s: not registered to interrupt controller\n", |
| 525 | __func__); |
| 526 | return -EINVAL; |
| 527 | } |
| 528 | return irq_linear_revmap(data->domain, offset); |
| 529 | } |
| 530 | |
| 531 | static int virq_to_phyirq(struct wcd9xxx *wcd9xxx, int virq) |
| 532 | { |
| 533 | struct irq_data *irq_data = irq_get_irq_data(virq); |
| 534 | return irq_data->hwirq; |
| 535 | } |
| 536 | |
| 537 | static unsigned int wcd9xxx_irq_get_upstream_irq(struct wcd9xxx *wcd9xxx) |
| 538 | { |
| 539 | struct wcd9xxx_irq_drv_data *data; |
| 540 | |
| 541 | /* Hold parent's of node */ |
| 542 | if (!of_node_get(of_irq_find_parent(wcd9xxx->dev->of_node))) |
| 543 | return -EINVAL; |
| 544 | |
| 545 | data = wcd9xxx_get_irq_drv_d(wcd9xxx); |
| 546 | if (!data) { |
| 547 | pr_err("%s: interrupt controller is not registerd\n", __func__); |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | rmb(); |
| 552 | return data->irq; |
| 553 | } |
| 554 | |
| 555 | static void wcd9xxx_irq_put_upstream_irq(struct wcd9xxx *wcd9xxx) |
| 556 | { |
| 557 | /* Hold parent's of node */ |
| 558 | of_node_put(of_irq_find_parent(wcd9xxx->dev->of_node)); |
| 559 | } |
| 560 | |
| 561 | static int wcd9xxx_map_irq(struct wcd9xxx *wcd9xxx, int irq) |
| 562 | { |
| 563 | return of_irq_to_resource(wcd9xxx->dev->of_node, irq, NULL); |
| 564 | } |
| 565 | |
| 566 | static int __devinit wcd9xxx_irq_probe(struct platform_device *pdev) |
| 567 | { |
| 568 | int irq; |
| 569 | struct irq_domain *domain; |
| 570 | struct wcd9xxx_irq_drv_data *data; |
| 571 | int ret = -EINVAL; |
| 572 | |
| 573 | irq = platform_get_irq_byname(pdev, "cdc-int"); |
| 574 | if (irq < 0) { |
| 575 | dev_err(&pdev->dev, "%s: Couldn't find cdc-int node(%d)\n", |
| 576 | __func__, irq); |
| 577 | return -EINVAL; |
| 578 | } else { |
| 579 | dev_dbg(&pdev->dev, "%s: virq = %d\n", __func__, irq); |
| 580 | domain = irq_find_host(pdev->dev.of_node); |
| 581 | data = (struct wcd9xxx_irq_drv_data *)domain->host_data; |
| 582 | data->irq = irq; |
| 583 | wmb(); |
| 584 | ret = 0; |
| 585 | } |
| 586 | |
| 587 | return ret; |
| 588 | } |
| 589 | |
| 590 | static int wcd9xxx_irq_remove(struct platform_device *pdev) |
| 591 | { |
| 592 | struct irq_domain *domain; |
| 593 | struct wcd9xxx_irq_drv_data *data; |
| 594 | |
| 595 | domain = irq_find_host(pdev->dev.of_node); |
| 596 | data = (struct wcd9xxx_irq_drv_data *)domain->host_data; |
| 597 | data->irq = 0; |
| 598 | wmb(); |
| 599 | |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | static const struct of_device_id of_match[] = { |
| 604 | { .compatible = "qcom,wcd9xxx-irq" }, |
| 605 | { } |
| 606 | }; |
| 607 | |
| 608 | static struct platform_driver wcd9xxx_irq_driver = { |
| 609 | .probe = wcd9xxx_irq_probe, |
| 610 | .remove = wcd9xxx_irq_remove, |
| 611 | .driver = { |
| 612 | .name = "wcd9xxx_intc", |
| 613 | .owner = THIS_MODULE, |
| 614 | .of_match_table = of_match_ptr(of_match), |
| 615 | }, |
| 616 | }; |
| 617 | |
| 618 | static int wcd9xxx_irq_drv_init(void) |
| 619 | { |
| 620 | return platform_driver_register(&wcd9xxx_irq_driver); |
| 621 | } |
| 622 | subsys_initcall(wcd9xxx_irq_drv_init); |
| 623 | |
| 624 | static void wcd9xxx_irq_drv_exit(void) |
| 625 | { |
| 626 | platform_driver_unregister(&wcd9xxx_irq_driver); |
| 627 | } |
| 628 | module_exit(wcd9xxx_irq_drv_exit); |
| 629 | #endif /* CONFIG_OF */ |