Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [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 | |
| 13 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 14 | |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/list.h> |
| 20 | #include <linux/of.h> |
| 21 | #include <linux/of_address.h> |
| 22 | #include <linux/of_irq.h> |
| 23 | #include <linux/irqdomain.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/spmi.h> |
| 26 | #include <linux/radix-tree.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/printk.h> |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 29 | #include <linux/ratelimit.h> |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 30 | |
| 31 | #include <asm/irq.h> |
| 32 | #include <asm/mach/irq.h> |
| 33 | #include <mach/qpnp-int.h> |
| 34 | |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 35 | /* 16 slave_ids, 256 per_ids per slave, and 8 ints per per_id */ |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 36 | #define QPNPINT_NR_IRQS (16 * 256 * 8) |
| 37 | /* This value is guaranteed not to be valid for private data */ |
| 38 | #define QPNPINT_INVALID_DATA 0x80000000 |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 39 | |
| 40 | enum qpnpint_regs { |
| 41 | QPNPINT_REG_RT_STS = 0x10, |
| 42 | QPNPINT_REG_SET_TYPE = 0x11, |
| 43 | QPNPINT_REG_POLARITY_HIGH = 0x12, |
| 44 | QPNPINT_REG_POLARITY_LOW = 0x13, |
| 45 | QPNPINT_REG_LATCHED_CLR = 0x14, |
| 46 | QPNPINT_REG_EN_SET = 0x15, |
| 47 | QPNPINT_REG_EN_CLR = 0x16, |
| 48 | QPNPINT_REG_LATCHED_STS = 0x18, |
| 49 | }; |
| 50 | |
| 51 | struct q_perip_data { |
| 52 | uint8_t type; /* bitmap */ |
| 53 | uint8_t pol_high; /* bitmap */ |
| 54 | uint8_t pol_low; /* bitmap */ |
| 55 | uint8_t int_en; /* bitmap */ |
| 56 | uint8_t use_count; |
| 57 | }; |
| 58 | |
| 59 | struct q_irq_data { |
| 60 | uint32_t priv_d; /* data to optimize arbiter interactions */ |
| 61 | struct q_chip_data *chip_d; |
| 62 | struct q_perip_data *per_d; |
| 63 | uint8_t mask_shift; |
| 64 | uint8_t spmi_slave; |
| 65 | uint16_t spmi_offset; |
| 66 | }; |
| 67 | |
| 68 | struct q_chip_data { |
| 69 | int bus_nr; |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 70 | struct irq_domain *domain; |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 71 | struct qpnp_local_int *cb; |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 72 | struct spmi_controller *spmi_ctrl; |
| 73 | struct radix_tree_root per_tree; |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 74 | struct list_head list; |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 75 | }; |
| 76 | |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 77 | static LIST_HEAD(qpnpint_chips); |
| 78 | static DEFINE_MUTEX(qpnpint_chips_mutex); |
| 79 | |
| 80 | #define QPNPINT_MAX_BUSSES 4 |
| 81 | struct q_chip_data *chip_lookup[QPNPINT_MAX_BUSSES]; |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * qpnpint_encode_hwirq - translate between qpnp_irq_spec and |
| 85 | * hwirq representation. |
| 86 | * |
| 87 | * slave_offset = (addr->slave * 256 * 8); |
| 88 | * perip_offset = slave_offset + (addr->perip * 8); |
| 89 | * return perip_offset + addr->irq; |
| 90 | */ |
| 91 | static inline int qpnpint_encode_hwirq(struct qpnp_irq_spec *spec) |
| 92 | { |
| 93 | uint32_t hwirq; |
| 94 | |
| 95 | if (spec->slave > 15 || spec->irq > 7) |
| 96 | return -EINVAL; |
| 97 | |
| 98 | hwirq = (spec->slave << 11); |
| 99 | hwirq |= (spec->per << 3); |
| 100 | hwirq |= spec->irq; |
| 101 | |
| 102 | return hwirq; |
| 103 | } |
| 104 | /** |
| 105 | * qpnpint_decode_hwirq - translate between hwirq and |
| 106 | * qpnp_irq_spec representation. |
| 107 | */ |
| 108 | static inline int qpnpint_decode_hwirq(unsigned long hwirq, |
| 109 | struct qpnp_irq_spec *spec) |
| 110 | { |
| 111 | if (hwirq > 65535) |
| 112 | return -EINVAL; |
| 113 | |
| 114 | spec->slave = (hwirq >> 11) & 0xF; |
| 115 | spec->per = (hwirq >> 3) & 0xFF; |
| 116 | spec->irq = hwirq & 0x7; |
| 117 | return 0; |
| 118 | } |
| 119 | |
Jack Pham | 6857691 | 2013-02-28 14:21:14 -0800 | [diff] [blame] | 120 | static int qpnpint_spmi_read(struct q_irq_data *irq_d, uint8_t reg, |
| 121 | void *buf, uint32_t len) |
| 122 | { |
| 123 | struct q_chip_data *chip_d = irq_d->chip_d; |
| 124 | |
| 125 | if (!chip_d->spmi_ctrl) |
| 126 | return -ENODEV; |
| 127 | |
| 128 | return spmi_ext_register_readl(chip_d->spmi_ctrl, irq_d->spmi_slave, |
| 129 | irq_d->spmi_offset + reg, buf, len); |
| 130 | } |
| 131 | |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 132 | static int qpnpint_spmi_write(struct q_irq_data *irq_d, uint8_t reg, |
| 133 | void *buf, uint32_t len) |
| 134 | { |
| 135 | struct q_chip_data *chip_d = irq_d->chip_d; |
| 136 | int rc; |
| 137 | |
| 138 | if (!chip_d->spmi_ctrl) |
| 139 | return -ENODEV; |
| 140 | |
| 141 | rc = spmi_ext_register_writel(chip_d->spmi_ctrl, irq_d->spmi_slave, |
| 142 | irq_d->spmi_offset + reg, buf, len); |
| 143 | return rc; |
| 144 | } |
| 145 | |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 146 | static int qpnpint_arbiter_op(struct irq_data *d, |
| 147 | struct q_irq_data *irq_d, |
| 148 | int (*arb_op)(struct spmi_controller *, |
| 149 | struct qpnp_irq_spec *, |
| 150 | uint32_t)) |
| 151 | |
| 152 | { |
| 153 | struct q_chip_data *chip_d = irq_d->chip_d; |
| 154 | struct qpnp_irq_spec q_spec; |
| 155 | int rc; |
| 156 | |
| 157 | if (!arb_op) |
| 158 | return 0; |
| 159 | |
| 160 | if (!chip_d->cb->register_priv_data) { |
| 161 | pr_warn_ratelimited("No ability to register arbiter registration data\n"); |
| 162 | return -ENODEV; |
| 163 | } |
| 164 | |
| 165 | rc = qpnpint_decode_hwirq(d->hwirq, &q_spec); |
| 166 | if (rc) { |
| 167 | pr_err_ratelimited("%s: decode failed on hwirq %lu\n", |
| 168 | __func__, d->hwirq); |
| 169 | return rc; |
| 170 | } else { |
| 171 | if (irq_d->priv_d == QPNPINT_INVALID_DATA) { |
| 172 | rc = chip_d->cb->register_priv_data(chip_d->spmi_ctrl, |
| 173 | &q_spec, &irq_d->priv_d); |
| 174 | if (rc) { |
| 175 | pr_err_ratelimited( |
| 176 | "%s: decode failed on hwirq %lu\n", |
| 177 | __func__, d->hwirq); |
| 178 | return rc; |
| 179 | } |
| 180 | |
| 181 | } |
| 182 | arb_op(chip_d->spmi_ctrl, &q_spec, irq_d->priv_d); |
| 183 | } |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 188 | static void qpnpint_irq_mask(struct irq_data *d) |
| 189 | { |
| 190 | struct q_irq_data *irq_d = irq_data_get_irq_chip_data(d); |
| 191 | struct q_chip_data *chip_d = irq_d->chip_d; |
| 192 | struct q_perip_data *per_d = irq_d->per_d; |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 193 | int rc; |
| 194 | |
| 195 | pr_debug("hwirq %lu irq: %d\n", d->hwirq, d->irq); |
| 196 | |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 197 | if (!chip_d->cb) { |
| 198 | pr_warn_ratelimited("No arbiter on bus=%u slave=%u offset=%u\n", |
| 199 | chip_d->bus_nr, irq_d->spmi_slave, |
| 200 | irq_d->spmi_offset); |
| 201 | return; |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 204 | qpnpint_arbiter_op(d, irq_d, chip_d->cb->mask); |
| 205 | |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 206 | per_d->int_en &= ~irq_d->mask_shift; |
| 207 | |
| 208 | rc = qpnpint_spmi_write(irq_d, QPNPINT_REG_EN_CLR, |
| 209 | (u8 *)&irq_d->mask_shift, 1); |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 210 | if (rc) { |
| 211 | pr_err_ratelimited("spmi failure on irq %d\n", d->irq); |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | pr_debug("done hwirq %lu irq: %d\n", d->hwirq, d->irq); |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | static void qpnpint_irq_mask_ack(struct irq_data *d) |
| 219 | { |
| 220 | struct q_irq_data *irq_d = irq_data_get_irq_chip_data(d); |
| 221 | struct q_chip_data *chip_d = irq_d->chip_d; |
| 222 | struct q_perip_data *per_d = irq_d->per_d; |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 223 | int rc; |
| 224 | |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 225 | pr_debug("hwirq %lu irq: %d\n", d->hwirq, d->irq); |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 226 | |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 227 | if (!chip_d->cb) { |
| 228 | pr_warn_ratelimited("No arbiter on bus=%u slave=%u offset=%u\n", |
| 229 | chip_d->bus_nr, irq_d->spmi_slave, |
| 230 | irq_d->spmi_offset); |
| 231 | return; |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 234 | qpnpint_arbiter_op(d, irq_d, chip_d->cb->mask); |
| 235 | |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 236 | per_d->int_en &= ~irq_d->mask_shift; |
| 237 | |
| 238 | rc = qpnpint_spmi_write(irq_d, QPNPINT_REG_EN_CLR, |
| 239 | &irq_d->mask_shift, 1); |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 240 | if (rc) { |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 241 | pr_err("spmi failure on irq %d\n", d->irq); |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 242 | return; |
| 243 | } |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 244 | |
| 245 | rc = qpnpint_spmi_write(irq_d, QPNPINT_REG_LATCHED_CLR, |
| 246 | &irq_d->mask_shift, 1); |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 247 | if (rc) { |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 248 | pr_err("spmi failure on irq %d\n", d->irq); |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 249 | return; |
| 250 | } |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static void qpnpint_irq_unmask(struct irq_data *d) |
| 254 | { |
| 255 | struct q_irq_data *irq_d = irq_data_get_irq_chip_data(d); |
| 256 | struct q_chip_data *chip_d = irq_d->chip_d; |
| 257 | struct q_perip_data *per_d = irq_d->per_d; |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 258 | int rc; |
| 259 | |
| 260 | pr_debug("hwirq %lu irq: %d\n", d->hwirq, d->irq); |
| 261 | |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 262 | if (!chip_d->cb) { |
| 263 | pr_warn_ratelimited("No arbiter on bus=%u slave=%u offset=%u\n", |
| 264 | chip_d->bus_nr, irq_d->spmi_slave, |
| 265 | irq_d->spmi_offset); |
| 266 | return; |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 269 | qpnpint_arbiter_op(d, irq_d, chip_d->cb->unmask); |
| 270 | |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 271 | per_d->int_en |= irq_d->mask_shift; |
| 272 | rc = qpnpint_spmi_write(irq_d, QPNPINT_REG_EN_SET, |
| 273 | &irq_d->mask_shift, 1); |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 274 | if (rc) { |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 275 | pr_err("spmi failure on irq %d\n", d->irq); |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 276 | return; |
| 277 | } |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static int qpnpint_irq_set_type(struct irq_data *d, unsigned int flow_type) |
| 281 | { |
| 282 | struct q_irq_data *irq_d = irq_data_get_irq_chip_data(d); |
| 283 | struct q_perip_data *per_d = irq_d->per_d; |
| 284 | int rc; |
| 285 | u8 buf[3]; |
| 286 | |
| 287 | pr_debug("hwirq %lu irq: %d flow: 0x%x\n", d->hwirq, |
| 288 | d->irq, flow_type); |
| 289 | |
| 290 | per_d->pol_high &= ~irq_d->mask_shift; |
| 291 | per_d->pol_low &= ~irq_d->mask_shift; |
| 292 | if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) { |
| 293 | per_d->type |= irq_d->mask_shift; /* edge trig */ |
| 294 | if (flow_type & IRQF_TRIGGER_RISING) |
| 295 | per_d->pol_high |= irq_d->mask_shift; |
| 296 | if (flow_type & IRQF_TRIGGER_FALLING) |
| 297 | per_d->pol_low |= irq_d->mask_shift; |
| 298 | } else { |
| 299 | if ((flow_type & IRQF_TRIGGER_HIGH) && |
| 300 | (flow_type & IRQF_TRIGGER_LOW)) |
| 301 | return -EINVAL; |
| 302 | per_d->type &= ~irq_d->mask_shift; /* level trig */ |
| 303 | if (flow_type & IRQF_TRIGGER_HIGH) |
| 304 | per_d->pol_high |= irq_d->mask_shift; |
| 305 | else |
Michael Bohan | 69701d3 | 2012-06-07 17:05:41 -0700 | [diff] [blame] | 306 | per_d->pol_low |= irq_d->mask_shift; |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | buf[0] = per_d->type; |
| 310 | buf[1] = per_d->pol_high; |
| 311 | buf[2] = per_d->pol_low; |
| 312 | |
| 313 | rc = qpnpint_spmi_write(irq_d, QPNPINT_REG_SET_TYPE, &buf, 3); |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 314 | if (rc) { |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 315 | pr_err("spmi failure on irq %d\n", d->irq); |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 316 | return rc; |
| 317 | } |
| 318 | |
| 319 | return 0; |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Jack Pham | 6857691 | 2013-02-28 14:21:14 -0800 | [diff] [blame] | 322 | static int qpnpint_irq_read_line(struct irq_data *d) |
| 323 | { |
| 324 | struct q_irq_data *irq_d = irq_data_get_irq_chip_data(d); |
| 325 | int rc; |
| 326 | u8 buf; |
| 327 | |
| 328 | pr_debug("hwirq %lu irq: %d\n", d->hwirq, d->irq); |
| 329 | |
| 330 | rc = qpnpint_spmi_read(irq_d, QPNPINT_REG_RT_STS, &buf, 1); |
| 331 | if (rc) { |
| 332 | pr_err("spmi failure on irq %d\n", d->irq); |
| 333 | return rc; |
| 334 | } |
| 335 | |
| 336 | return (buf & irq_d->mask_shift) ? 1 : 0; |
| 337 | } |
| 338 | |
Michael Bohan | c86e2b7 | 2012-05-29 16:57:52 -0700 | [diff] [blame] | 339 | static int qpnpint_irq_set_wake(struct irq_data *d, unsigned int on) |
| 340 | { |
| 341 | return 0; |
| 342 | } |
| 343 | |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 344 | static struct irq_chip qpnpint_chip = { |
| 345 | .name = "qpnp-int", |
| 346 | .irq_mask = qpnpint_irq_mask, |
| 347 | .irq_mask_ack = qpnpint_irq_mask_ack, |
| 348 | .irq_unmask = qpnpint_irq_unmask, |
| 349 | .irq_set_type = qpnpint_irq_set_type, |
Jack Pham | 6857691 | 2013-02-28 14:21:14 -0800 | [diff] [blame] | 350 | .irq_read_line = qpnpint_irq_read_line, |
Michael Bohan | c86e2b7 | 2012-05-29 16:57:52 -0700 | [diff] [blame] | 351 | .irq_set_wake = qpnpint_irq_set_wake, |
| 352 | .flags = IRQCHIP_MASK_ON_SUSPEND, |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 353 | }; |
| 354 | |
| 355 | static int qpnpint_init_irq_data(struct q_chip_data *chip_d, |
| 356 | struct q_irq_data *irq_d, |
| 357 | unsigned long hwirq) |
| 358 | { |
| 359 | struct qpnp_irq_spec q_spec; |
| 360 | int rc; |
| 361 | |
| 362 | irq_d->mask_shift = 1 << (hwirq & 0x7); |
| 363 | rc = qpnpint_decode_hwirq(hwirq, &q_spec); |
| 364 | if (rc < 0) |
| 365 | return rc; |
| 366 | irq_d->spmi_slave = q_spec.slave; |
| 367 | irq_d->spmi_offset = q_spec.per << 8; |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 368 | irq_d->chip_d = chip_d; |
| 369 | |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 370 | irq_d->priv_d = QPNPINT_INVALID_DATA; |
| 371 | |
| 372 | if (chip_d->cb && chip_d->cb->register_priv_data) { |
| 373 | rc = chip_d->cb->register_priv_data(chip_d->spmi_ctrl, &q_spec, |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 374 | &irq_d->priv_d); |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 375 | if (rc) |
| 376 | return rc; |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 377 | } |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 378 | |
| 379 | irq_d->per_d->use_count++; |
| 380 | return 0; |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | static struct q_irq_data *qpnpint_alloc_irq_data( |
| 384 | struct q_chip_data *chip_d, |
| 385 | unsigned long hwirq) |
| 386 | { |
| 387 | struct q_irq_data *irq_d; |
| 388 | struct q_perip_data *per_d; |
Michael Bohan | 392006f | 2013-01-25 14:29:41 -0800 | [diff] [blame] | 389 | int rc; |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 390 | |
| 391 | irq_d = kzalloc(sizeof(struct q_irq_data), GFP_KERNEL); |
| 392 | if (!irq_d) |
| 393 | return ERR_PTR(-ENOMEM); |
| 394 | |
| 395 | /** |
| 396 | * The Peripheral Tree is keyed from the slave + per_id. We're |
| 397 | * ignoring the irq bits here since this peripheral structure |
| 398 | * should be common for all irqs on the same peripheral. |
| 399 | */ |
| 400 | per_d = radix_tree_lookup(&chip_d->per_tree, (hwirq & ~0x7)); |
| 401 | if (!per_d) { |
| 402 | per_d = kzalloc(sizeof(struct q_perip_data), GFP_KERNEL); |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 403 | if (!per_d) { |
Michael Bohan | 392006f | 2013-01-25 14:29:41 -0800 | [diff] [blame] | 404 | rc = -ENOMEM; |
| 405 | goto alloc_fail; |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 406 | } |
Michael Bohan | 392006f | 2013-01-25 14:29:41 -0800 | [diff] [blame] | 407 | rc = radix_tree_preload(GFP_KERNEL); |
| 408 | if (rc) |
| 409 | goto alloc_fail; |
| 410 | rc = radix_tree_insert(&chip_d->per_tree, |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 411 | (hwirq & ~0x7), per_d); |
Michael Bohan | 392006f | 2013-01-25 14:29:41 -0800 | [diff] [blame] | 412 | if (rc) |
| 413 | goto alloc_fail; |
| 414 | radix_tree_preload_end(); |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 415 | } |
| 416 | irq_d->per_d = per_d; |
| 417 | |
| 418 | return irq_d; |
Michael Bohan | 392006f | 2013-01-25 14:29:41 -0800 | [diff] [blame] | 419 | |
| 420 | alloc_fail: |
| 421 | kfree(per_d); |
| 422 | kfree(irq_d); |
| 423 | return ERR_PTR(rc); |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 426 | static int qpnpint_irq_domain_dt_translate(struct irq_domain *d, |
| 427 | struct device_node *controller, |
| 428 | const u32 *intspec, unsigned int intsize, |
| 429 | unsigned long *out_hwirq, |
| 430 | unsigned int *out_type) |
| 431 | { |
| 432 | struct qpnp_irq_spec addr; |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 433 | int ret; |
| 434 | |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 435 | pr_debug("intspec[0] 0x%x intspec[1] 0x%x intspec[2] 0x%x\n", |
| 436 | intspec[0], intspec[1], intspec[2]); |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 437 | |
| 438 | if (d->of_node != controller) |
| 439 | return -EINVAL; |
| 440 | if (intsize != 3) |
| 441 | return -EINVAL; |
| 442 | |
| 443 | addr.irq = intspec[2] & 0x7; |
| 444 | addr.per = intspec[1] & 0xFF; |
| 445 | addr.slave = intspec[0] & 0xF; |
| 446 | |
| 447 | ret = qpnpint_encode_hwirq(&addr); |
| 448 | if (ret < 0) { |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 449 | pr_err("invalid intspec\n"); |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 450 | return ret; |
| 451 | } |
| 452 | *out_hwirq = ret; |
| 453 | *out_type = IRQ_TYPE_NONE; |
| 454 | |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 455 | pr_debug("out_hwirq = %lu\n", *out_hwirq); |
| 456 | |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 457 | return 0; |
| 458 | } |
| 459 | |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 460 | static void qpnpint_free_irq_data(struct q_irq_data *irq_d) |
| 461 | { |
| 462 | if (irq_d->per_d->use_count == 1) |
| 463 | kfree(irq_d->per_d); |
| 464 | else |
| 465 | irq_d->per_d->use_count--; |
| 466 | kfree(irq_d); |
| 467 | } |
| 468 | |
| 469 | static int qpnpint_irq_domain_map(struct irq_domain *d, |
| 470 | unsigned int virq, irq_hw_number_t hwirq) |
| 471 | { |
| 472 | struct q_chip_data *chip_d = d->host_data; |
| 473 | struct q_irq_data *irq_d; |
| 474 | int rc; |
| 475 | |
| 476 | pr_debug("hwirq = %lu\n", hwirq); |
| 477 | |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 478 | if (hwirq < 0 || hwirq >= QPNPINT_NR_IRQS) { |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 479 | pr_err("hwirq %lu out of bounds\n", hwirq); |
| 480 | return -EINVAL; |
| 481 | } |
| 482 | |
| 483 | irq_radix_revmap_insert(d, virq, hwirq); |
| 484 | |
| 485 | irq_d = qpnpint_alloc_irq_data(chip_d, hwirq); |
| 486 | if (IS_ERR(irq_d)) { |
| 487 | pr_err("failed to alloc irq data for hwirq %lu\n", hwirq); |
| 488 | return PTR_ERR(irq_d); |
| 489 | } |
| 490 | |
| 491 | rc = qpnpint_init_irq_data(chip_d, irq_d, hwirq); |
| 492 | if (rc) { |
| 493 | pr_err("failed to init irq data for hwirq %lu\n", hwirq); |
| 494 | goto map_err; |
| 495 | } |
| 496 | |
| 497 | irq_set_chip_and_handler(virq, |
| 498 | &qpnpint_chip, |
| 499 | handle_level_irq); |
| 500 | irq_set_chip_data(virq, irq_d); |
| 501 | #ifdef CONFIG_ARM |
| 502 | set_irq_flags(virq, IRQF_VALID); |
| 503 | #else |
| 504 | irq_set_noprobe(virq); |
| 505 | #endif |
| 506 | return 0; |
| 507 | |
| 508 | map_err: |
| 509 | qpnpint_free_irq_data(irq_d); |
| 510 | return rc; |
| 511 | } |
| 512 | |
| 513 | void qpnpint_irq_domain_unmap(struct irq_domain *d, unsigned int virq) |
| 514 | { |
| 515 | struct q_irq_data *irq_d = irq_get_chip_data(virq); |
| 516 | |
| 517 | if (WARN_ON(!irq_d)) |
| 518 | return; |
| 519 | |
| 520 | qpnpint_free_irq_data(irq_d); |
| 521 | } |
| 522 | |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 523 | const struct irq_domain_ops qpnpint_irq_domain_ops = { |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 524 | .map = qpnpint_irq_domain_map, |
| 525 | .unmap = qpnpint_irq_domain_unmap, |
| 526 | .xlate = qpnpint_irq_domain_dt_translate, |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 527 | }; |
| 528 | |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 529 | int qpnpint_register_controller(struct device_node *node, |
| 530 | struct spmi_controller *ctrl, |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 531 | struct qpnp_local_int *li_cb) |
| 532 | { |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 533 | struct q_chip_data *chip_d; |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 534 | |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 535 | if (!node || !ctrl || ctrl->nr >= QPNPINT_MAX_BUSSES) |
| 536 | return -EINVAL; |
| 537 | |
| 538 | list_for_each_entry(chip_d, &qpnpint_chips, list) |
| 539 | if (node == chip_d->domain->of_node) { |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 540 | chip_d->cb = kmemdup(li_cb, |
| 541 | sizeof(*li_cb), GFP_ATOMIC); |
| 542 | if (!chip_d->cb) |
| 543 | return -ENOMEM; |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 544 | chip_d->spmi_ctrl = ctrl; |
| 545 | chip_lookup[ctrl->nr] = chip_d; |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | return -ENOENT; |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 550 | } |
| 551 | EXPORT_SYMBOL(qpnpint_register_controller); |
| 552 | |
Michael Bohan | 0435bd6 | 2013-02-26 15:29:22 -0800 | [diff] [blame] | 553 | int qpnpint_unregister_controller(struct device_node *node) |
| 554 | { |
| 555 | struct q_chip_data *chip_d; |
| 556 | |
| 557 | if (!node) |
| 558 | return -EINVAL; |
| 559 | |
| 560 | list_for_each_entry(chip_d, &qpnpint_chips, list) |
| 561 | if (node == chip_d->domain->of_node) { |
| 562 | kfree(chip_d->cb); |
| 563 | chip_d->cb = NULL; |
| 564 | if (chip_d->spmi_ctrl) |
| 565 | chip_lookup[chip_d->spmi_ctrl->nr] = NULL; |
| 566 | chip_d->spmi_ctrl = NULL; |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | return -ENOENT; |
| 571 | } |
| 572 | EXPORT_SYMBOL(qpnpint_unregister_controller); |
| 573 | |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 574 | int qpnpint_handle_irq(struct spmi_controller *spmi_ctrl, |
| 575 | struct qpnp_irq_spec *spec) |
| 576 | { |
| 577 | struct irq_domain *domain; |
| 578 | unsigned long hwirq, busno; |
| 579 | int irq; |
| 580 | |
| 581 | pr_debug("spec slave = %u per = %u irq = %u\n", |
| 582 | spec->slave, spec->per, spec->irq); |
| 583 | |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 584 | busno = spmi_ctrl->nr; |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 585 | if (!spec || !spmi_ctrl || busno >= QPNPINT_MAX_BUSSES) |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 586 | return -EINVAL; |
| 587 | |
| 588 | hwirq = qpnpint_encode_hwirq(spec); |
| 589 | if (hwirq < 0) { |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 590 | pr_err("invalid irq spec passed\n"); |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 591 | return -EINVAL; |
| 592 | } |
| 593 | |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 594 | domain = chip_lookup[busno]->domain; |
| 595 | irq = irq_radix_revmap_lookup(domain, hwirq); |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 596 | |
| 597 | generic_handle_irq(irq); |
| 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | EXPORT_SYMBOL(qpnpint_handle_irq); |
| 602 | |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 603 | int __init qpnpint_of_init(struct device_node *node, struct device_node *parent) |
| 604 | { |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 605 | struct q_chip_data *chip_d; |
| 606 | |
| 607 | chip_d = kzalloc(sizeof(struct q_chip_data), GFP_KERNEL); |
| 608 | if (!chip_d) |
| 609 | return -ENOMEM; |
| 610 | |
| 611 | chip_d->domain = irq_domain_add_tree(node, |
| 612 | &qpnpint_irq_domain_ops, chip_d); |
| 613 | if (!chip_d->domain) { |
| 614 | pr_err("Unable to allocate irq_domain\n"); |
| 615 | kfree(chip_d); |
| 616 | return -ENOMEM; |
| 617 | } |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 618 | |
| 619 | INIT_RADIX_TREE(&chip_d->per_tree, GFP_ATOMIC); |
Michael Bohan | bb6b30f | 2012-06-01 13:33:51 -0700 | [diff] [blame] | 620 | list_add(&chip_d->list, &qpnpint_chips); |
Michael Bohan | 115cf65 | 2012-01-05 14:32:59 -0800 | [diff] [blame] | 621 | |
| 622 | return 0; |
| 623 | } |
| 624 | EXPORT_SYMBOL(qpnpint_of_init); |