Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011, Code Aurora Forum. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 15 | |
| 16 | #include <linux/kernel.h> |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 17 | #include <linux/interrupt.h> |
Stephen Boyd | cced354 | 2014-02-26 10:59:20 -0800 | [diff] [blame] | 18 | #include <linux/irqchip/chained_irq.h> |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 19 | #include <linux/irq.h> |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 20 | #include <linux/irqdomain.h> |
Jingoo Han | ef310f4 | 2013-08-20 16:01:26 +0900 | [diff] [blame] | 21 | #include <linux/module.h> |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/slab.h> |
Abhijeet Dharmapurikar | c013f0a | 2011-04-05 14:40:53 -0700 | [diff] [blame] | 24 | #include <linux/err.h> |
David Brown | ce44bf5 | 2013-03-12 11:41:54 -0700 | [diff] [blame] | 25 | #include <linux/ssbi.h> |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 26 | #include <linux/regmap.h> |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 27 | #include <linux/of_platform.h> |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 28 | #include <linux/mfd/core.h> |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 29 | |
| 30 | #define SSBI_REG_ADDR_IRQ_BASE 0x1BB |
| 31 | |
| 32 | #define SSBI_REG_ADDR_IRQ_ROOT (SSBI_REG_ADDR_IRQ_BASE + 0) |
| 33 | #define SSBI_REG_ADDR_IRQ_M_STATUS1 (SSBI_REG_ADDR_IRQ_BASE + 1) |
| 34 | #define SSBI_REG_ADDR_IRQ_M_STATUS2 (SSBI_REG_ADDR_IRQ_BASE + 2) |
| 35 | #define SSBI_REG_ADDR_IRQ_M_STATUS3 (SSBI_REG_ADDR_IRQ_BASE + 3) |
| 36 | #define SSBI_REG_ADDR_IRQ_M_STATUS4 (SSBI_REG_ADDR_IRQ_BASE + 4) |
| 37 | #define SSBI_REG_ADDR_IRQ_BLK_SEL (SSBI_REG_ADDR_IRQ_BASE + 5) |
| 38 | #define SSBI_REG_ADDR_IRQ_IT_STATUS (SSBI_REG_ADDR_IRQ_BASE + 6) |
| 39 | #define SSBI_REG_ADDR_IRQ_CONFIG (SSBI_REG_ADDR_IRQ_BASE + 7) |
| 40 | #define SSBI_REG_ADDR_IRQ_RT_STATUS (SSBI_REG_ADDR_IRQ_BASE + 8) |
| 41 | |
Srinivas Kandagatla | 953f432 | 2016-11-15 12:01:51 +0000 | [diff] [blame] | 42 | #define PM8821_SSBI_REG_ADDR_IRQ_BASE 0x100 |
| 43 | #define PM8821_SSBI_REG_ADDR_IRQ_MASTER0 (PM8821_SSBI_REG_ADDR_IRQ_BASE + 0x30) |
| 44 | #define PM8821_SSBI_REG_ADDR_IRQ_MASTER1 (PM8821_SSBI_REG_ADDR_IRQ_BASE + 0xb0) |
| 45 | #define PM8821_SSBI_REG(m, b, offset) \ |
| 46 | ((m == 0) ? \ |
| 47 | (PM8821_SSBI_REG_ADDR_IRQ_MASTER0 + b + offset) : \ |
| 48 | (PM8821_SSBI_REG_ADDR_IRQ_MASTER1 + b + offset)) |
| 49 | #define PM8821_SSBI_ADDR_IRQ_ROOT(m, b) PM8821_SSBI_REG(m, b, 0x0) |
| 50 | #define PM8821_SSBI_ADDR_IRQ_CLEAR(m, b) PM8821_SSBI_REG(m, b, 0x01) |
| 51 | #define PM8821_SSBI_ADDR_IRQ_MASK(m, b) PM8821_SSBI_REG(m, b, 0x08) |
| 52 | #define PM8821_SSBI_ADDR_IRQ_RT_STATUS(m, b) PM8821_SSBI_REG(m, b, 0x0f) |
| 53 | |
| 54 | #define PM8821_BLOCKS_PER_MASTER 7 |
| 55 | |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 56 | #define PM_IRQF_LVL_SEL 0x01 /* level select */ |
| 57 | #define PM_IRQF_MASK_FE 0x02 /* mask falling edge */ |
| 58 | #define PM_IRQF_MASK_RE 0x04 /* mask rising edge */ |
| 59 | #define PM_IRQF_CLR 0x08 /* clear interrupt */ |
| 60 | #define PM_IRQF_BITS_MASK 0x70 |
| 61 | #define PM_IRQF_BITS_SHIFT 4 |
| 62 | #define PM_IRQF_WRITE 0x80 |
| 63 | |
| 64 | #define PM_IRQF_MASK_ALL (PM_IRQF_MASK_FE | \ |
| 65 | PM_IRQF_MASK_RE) |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 66 | |
| 67 | #define REG_HWREV 0x002 /* PMIC4 revision */ |
| 68 | #define REG_HWREV_2 0x0E8 /* PMIC4 revision 2 */ |
| 69 | |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 70 | #define PM8XXX_NR_IRQS 256 |
Srinivas Kandagatla | 953f432 | 2016-11-15 12:01:51 +0000 | [diff] [blame] | 71 | #define PM8821_NR_IRQS 112 |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 72 | |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 73 | struct pm_irq_chip { |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 74 | struct regmap *regmap; |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 75 | spinlock_t pm_irq_lock; |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 76 | struct irq_domain *irqdomain; |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 77 | unsigned int num_irqs; |
| 78 | unsigned int num_blocks; |
| 79 | unsigned int num_masters; |
| 80 | u8 config[0]; |
| 81 | }; |
| 82 | |
Srinivas Kandagatla | 953f432 | 2016-11-15 12:01:51 +0000 | [diff] [blame] | 83 | struct pm_irq_data { |
| 84 | int num_irqs; |
| 85 | const struct irq_domain_ops *irq_domain_ops; |
| 86 | void (*irq_handler)(struct irq_desc *desc); |
| 87 | }; |
| 88 | |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 89 | static int pm8xxx_read_block_irq(struct pm_irq_chip *chip, unsigned int bp, |
| 90 | unsigned int *ip) |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 91 | { |
| 92 | int rc; |
| 93 | |
| 94 | spin_lock(&chip->pm_irq_lock); |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 95 | rc = regmap_write(chip->regmap, SSBI_REG_ADDR_IRQ_BLK_SEL, bp); |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 96 | if (rc) { |
| 97 | pr_err("Failed Selecting Block %d rc=%d\n", bp, rc); |
| 98 | goto bail; |
| 99 | } |
| 100 | |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 101 | rc = regmap_read(chip->regmap, SSBI_REG_ADDR_IRQ_IT_STATUS, ip); |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 102 | if (rc) |
| 103 | pr_err("Failed Reading Status rc=%d\n", rc); |
| 104 | bail: |
| 105 | spin_unlock(&chip->pm_irq_lock); |
| 106 | return rc; |
| 107 | } |
| 108 | |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 109 | static int |
| 110 | pm8xxx_config_irq(struct pm_irq_chip *chip, unsigned int bp, unsigned int cp) |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 111 | { |
| 112 | int rc; |
| 113 | |
| 114 | spin_lock(&chip->pm_irq_lock); |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 115 | rc = regmap_write(chip->regmap, SSBI_REG_ADDR_IRQ_BLK_SEL, bp); |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 116 | if (rc) { |
| 117 | pr_err("Failed Selecting Block %d rc=%d\n", bp, rc); |
| 118 | goto bail; |
| 119 | } |
| 120 | |
| 121 | cp |= PM_IRQF_WRITE; |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 122 | rc = regmap_write(chip->regmap, SSBI_REG_ADDR_IRQ_CONFIG, cp); |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 123 | if (rc) |
| 124 | pr_err("Failed Configuring IRQ rc=%d\n", rc); |
| 125 | bail: |
| 126 | spin_unlock(&chip->pm_irq_lock); |
| 127 | return rc; |
| 128 | } |
| 129 | |
| 130 | static int pm8xxx_irq_block_handler(struct pm_irq_chip *chip, int block) |
| 131 | { |
| 132 | int pmirq, irq, i, ret = 0; |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 133 | unsigned int bits; |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 134 | |
| 135 | ret = pm8xxx_read_block_irq(chip, block, &bits); |
| 136 | if (ret) { |
| 137 | pr_err("Failed reading %d block ret=%d", block, ret); |
| 138 | return ret; |
| 139 | } |
| 140 | if (!bits) { |
| 141 | pr_err("block bit set in master but no irqs: %d", block); |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | /* Check IRQ bits */ |
| 146 | for (i = 0; i < 8; i++) { |
| 147 | if (bits & (1 << i)) { |
| 148 | pmirq = block * 8 + i; |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 149 | irq = irq_find_mapping(chip->irqdomain, pmirq); |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 150 | generic_handle_irq(irq); |
| 151 | } |
| 152 | } |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static int pm8xxx_irq_master_handler(struct pm_irq_chip *chip, int master) |
| 157 | { |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 158 | unsigned int blockbits; |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 159 | int block_number, i, ret = 0; |
| 160 | |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 161 | ret = regmap_read(chip->regmap, SSBI_REG_ADDR_IRQ_M_STATUS1 + master, |
| 162 | &blockbits); |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 163 | if (ret) { |
| 164 | pr_err("Failed to read master %d ret=%d\n", master, ret); |
| 165 | return ret; |
| 166 | } |
| 167 | if (!blockbits) { |
| 168 | pr_err("master bit set in root but no blocks: %d", master); |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | for (i = 0; i < 8; i++) |
| 173 | if (blockbits & (1 << i)) { |
| 174 | block_number = master * 8 + i; /* block # */ |
| 175 | ret |= pm8xxx_irq_block_handler(chip, block_number); |
| 176 | } |
| 177 | return ret; |
| 178 | } |
| 179 | |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 180 | static void pm8xxx_irq_handler(struct irq_desc *desc) |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 181 | { |
| 182 | struct pm_irq_chip *chip = irq_desc_get_handler_data(desc); |
| 183 | struct irq_chip *irq_chip = irq_desc_get_chip(desc); |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 184 | unsigned int root; |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 185 | int i, ret, masters = 0; |
| 186 | |
Stephen Boyd | cced354 | 2014-02-26 10:59:20 -0800 | [diff] [blame] | 187 | chained_irq_enter(irq_chip, desc); |
| 188 | |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 189 | ret = regmap_read(chip->regmap, SSBI_REG_ADDR_IRQ_ROOT, &root); |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 190 | if (ret) { |
| 191 | pr_err("Can't read root status ret=%d\n", ret); |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | /* on pm8xxx series masters start from bit 1 of the root */ |
| 196 | masters = root >> 1; |
| 197 | |
| 198 | /* Read allowed masters for blocks. */ |
| 199 | for (i = 0; i < chip->num_masters; i++) |
| 200 | if (masters & (1 << i)) |
| 201 | pm8xxx_irq_master_handler(chip, i); |
| 202 | |
Stephen Boyd | cced354 | 2014-02-26 10:59:20 -0800 | [diff] [blame] | 203 | chained_irq_exit(irq_chip, desc); |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Srinivas Kandagatla | 953f432 | 2016-11-15 12:01:51 +0000 | [diff] [blame] | 206 | static void pm8821_irq_block_handler(struct pm_irq_chip *chip, |
| 207 | int master, int block) |
| 208 | { |
| 209 | int pmirq, irq, i, ret; |
| 210 | unsigned int bits; |
| 211 | |
| 212 | ret = regmap_read(chip->regmap, |
| 213 | PM8821_SSBI_ADDR_IRQ_ROOT(master, block), &bits); |
| 214 | if (ret) { |
| 215 | pr_err("Reading block %d failed ret=%d", block, ret); |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | /* Convert block offset to global block number */ |
| 220 | block += (master * PM8821_BLOCKS_PER_MASTER) - 1; |
| 221 | |
| 222 | /* Check IRQ bits */ |
| 223 | for (i = 0; i < 8; i++) { |
| 224 | if (bits & BIT(i)) { |
| 225 | pmirq = block * 8 + i; |
| 226 | irq = irq_find_mapping(chip->irqdomain, pmirq); |
| 227 | generic_handle_irq(irq); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | static inline void pm8821_irq_master_handler(struct pm_irq_chip *chip, |
| 233 | int master, u8 master_val) |
| 234 | { |
| 235 | int block; |
| 236 | |
| 237 | for (block = 1; block < 8; block++) |
| 238 | if (master_val & BIT(block)) |
| 239 | pm8821_irq_block_handler(chip, master, block); |
| 240 | } |
| 241 | |
| 242 | static void pm8821_irq_handler(struct irq_desc *desc) |
| 243 | { |
| 244 | struct pm_irq_chip *chip = irq_desc_get_handler_data(desc); |
| 245 | struct irq_chip *irq_chip = irq_desc_get_chip(desc); |
| 246 | unsigned int master; |
| 247 | int ret; |
| 248 | |
| 249 | chained_irq_enter(irq_chip, desc); |
| 250 | ret = regmap_read(chip->regmap, |
| 251 | PM8821_SSBI_REG_ADDR_IRQ_MASTER0, &master); |
| 252 | if (ret) { |
| 253 | pr_err("Failed to read master 0 ret=%d\n", ret); |
| 254 | goto done; |
| 255 | } |
| 256 | |
| 257 | /* bits 1 through 7 marks the first 7 blocks in master 0 */ |
| 258 | if (master & GENMASK(7, 1)) |
| 259 | pm8821_irq_master_handler(chip, 0, master); |
| 260 | |
| 261 | /* bit 0 marks if master 1 contains any bits */ |
| 262 | if (!(master & BIT(0))) |
| 263 | goto done; |
| 264 | |
| 265 | ret = regmap_read(chip->regmap, |
| 266 | PM8821_SSBI_REG_ADDR_IRQ_MASTER1, &master); |
| 267 | if (ret) { |
| 268 | pr_err("Failed to read master 1 ret=%d\n", ret); |
| 269 | goto done; |
| 270 | } |
| 271 | |
| 272 | pm8821_irq_master_handler(chip, 1, master); |
| 273 | |
| 274 | done: |
| 275 | chained_irq_exit(irq_chip, desc); |
| 276 | } |
| 277 | |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 278 | static void pm8xxx_irq_mask_ack(struct irq_data *d) |
| 279 | { |
| 280 | struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d); |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 281 | unsigned int pmirq = irqd_to_hwirq(d); |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 282 | u8 block, config; |
| 283 | |
| 284 | block = pmirq / 8; |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 285 | |
| 286 | config = chip->config[pmirq] | PM_IRQF_MASK_ALL | PM_IRQF_CLR; |
| 287 | pm8xxx_config_irq(chip, block, config); |
| 288 | } |
| 289 | |
| 290 | static void pm8xxx_irq_unmask(struct irq_data *d) |
| 291 | { |
| 292 | struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d); |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 293 | unsigned int pmirq = irqd_to_hwirq(d); |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 294 | u8 block, config; |
| 295 | |
| 296 | block = pmirq / 8; |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 297 | |
| 298 | config = chip->config[pmirq]; |
| 299 | pm8xxx_config_irq(chip, block, config); |
| 300 | } |
| 301 | |
| 302 | static int pm8xxx_irq_set_type(struct irq_data *d, unsigned int flow_type) |
| 303 | { |
| 304 | struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d); |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 305 | unsigned int pmirq = irqd_to_hwirq(d); |
| 306 | int irq_bit; |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 307 | u8 block, config; |
| 308 | |
| 309 | block = pmirq / 8; |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 310 | irq_bit = pmirq % 8; |
| 311 | |
| 312 | chip->config[pmirq] = (irq_bit << PM_IRQF_BITS_SHIFT) |
| 313 | | PM_IRQF_MASK_ALL; |
| 314 | if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) { |
| 315 | if (flow_type & IRQF_TRIGGER_RISING) |
| 316 | chip->config[pmirq] &= ~PM_IRQF_MASK_RE; |
| 317 | if (flow_type & IRQF_TRIGGER_FALLING) |
| 318 | chip->config[pmirq] &= ~PM_IRQF_MASK_FE; |
| 319 | } else { |
| 320 | chip->config[pmirq] |= PM_IRQF_LVL_SEL; |
| 321 | |
| 322 | if (flow_type & IRQF_TRIGGER_HIGH) |
| 323 | chip->config[pmirq] &= ~PM_IRQF_MASK_RE; |
| 324 | else |
| 325 | chip->config[pmirq] &= ~PM_IRQF_MASK_FE; |
| 326 | } |
| 327 | |
| 328 | config = chip->config[pmirq] | PM_IRQF_CLR; |
| 329 | return pm8xxx_config_irq(chip, block, config); |
| 330 | } |
| 331 | |
Bjorn Andersson | f1837e4 | 2015-07-14 23:40:34 -0700 | [diff] [blame] | 332 | static int pm8xxx_irq_get_irqchip_state(struct irq_data *d, |
| 333 | enum irqchip_irq_state which, |
| 334 | bool *state) |
| 335 | { |
| 336 | struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d); |
| 337 | unsigned int pmirq = irqd_to_hwirq(d); |
| 338 | unsigned int bits; |
| 339 | int irq_bit; |
| 340 | u8 block; |
| 341 | int rc; |
| 342 | |
| 343 | if (which != IRQCHIP_STATE_LINE_LEVEL) |
| 344 | return -EINVAL; |
| 345 | |
| 346 | block = pmirq / 8; |
| 347 | irq_bit = pmirq % 8; |
| 348 | |
| 349 | spin_lock(&chip->pm_irq_lock); |
| 350 | rc = regmap_write(chip->regmap, SSBI_REG_ADDR_IRQ_BLK_SEL, block); |
| 351 | if (rc) { |
| 352 | pr_err("Failed Selecting Block %d rc=%d\n", block, rc); |
| 353 | goto bail; |
| 354 | } |
| 355 | |
| 356 | rc = regmap_read(chip->regmap, SSBI_REG_ADDR_IRQ_RT_STATUS, &bits); |
| 357 | if (rc) { |
| 358 | pr_err("Failed Reading Status rc=%d\n", rc); |
| 359 | goto bail; |
| 360 | } |
| 361 | |
| 362 | *state = !!(bits & BIT(irq_bit)); |
| 363 | bail: |
| 364 | spin_unlock(&chip->pm_irq_lock); |
| 365 | |
| 366 | return rc; |
| 367 | } |
| 368 | |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 369 | static struct irq_chip pm8xxx_irq_chip = { |
| 370 | .name = "pm8xxx", |
| 371 | .irq_mask_ack = pm8xxx_irq_mask_ack, |
| 372 | .irq_unmask = pm8xxx_irq_unmask, |
| 373 | .irq_set_type = pm8xxx_irq_set_type, |
Bjorn Andersson | f1837e4 | 2015-07-14 23:40:34 -0700 | [diff] [blame] | 374 | .irq_get_irqchip_state = pm8xxx_irq_get_irqchip_state, |
Stephen Boyd | d2d24ad | 2014-03-04 10:48:52 -0800 | [diff] [blame] | 375 | .flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_SKIP_SET_WAKE, |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 376 | }; |
| 377 | |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 378 | static int pm8xxx_irq_domain_map(struct irq_domain *d, unsigned int irq, |
| 379 | irq_hw_number_t hwirq) |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 380 | { |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 381 | struct pm_irq_chip *chip = d->host_data; |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 382 | |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 383 | irq_set_chip_and_handler(irq, &pm8xxx_irq_chip, handle_level_irq); |
| 384 | irq_set_chip_data(irq, chip); |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 385 | irq_set_noprobe(irq); |
Rob Herring | 9bd09f3 | 2015-07-27 15:55:20 -0500 | [diff] [blame] | 386 | |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 387 | return 0; |
| 388 | } |
| 389 | |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 390 | static const struct irq_domain_ops pm8xxx_irq_domain_ops = { |
| 391 | .xlate = irq_domain_xlate_twocell, |
| 392 | .map = pm8xxx_irq_domain_map, |
| 393 | }; |
| 394 | |
Srinivas Kandagatla | 953f432 | 2016-11-15 12:01:51 +0000 | [diff] [blame] | 395 | static void pm8821_irq_mask_ack(struct irq_data *d) |
| 396 | { |
| 397 | struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d); |
| 398 | unsigned int pmirq = irqd_to_hwirq(d); |
| 399 | u8 block, master; |
| 400 | int irq_bit, rc; |
| 401 | |
| 402 | block = pmirq / 8; |
| 403 | master = block / PM8821_BLOCKS_PER_MASTER; |
| 404 | irq_bit = pmirq % 8; |
| 405 | block %= PM8821_BLOCKS_PER_MASTER; |
| 406 | |
| 407 | rc = regmap_update_bits(chip->regmap, |
| 408 | PM8821_SSBI_ADDR_IRQ_MASK(master, block), |
| 409 | BIT(irq_bit), BIT(irq_bit)); |
| 410 | if (rc) { |
| 411 | pr_err("Failed to mask IRQ:%d rc=%d\n", pmirq, rc); |
| 412 | return; |
| 413 | } |
| 414 | |
| 415 | rc = regmap_update_bits(chip->regmap, |
| 416 | PM8821_SSBI_ADDR_IRQ_CLEAR(master, block), |
| 417 | BIT(irq_bit), BIT(irq_bit)); |
| 418 | if (rc) |
| 419 | pr_err("Failed to CLEAR IRQ:%d rc=%d\n", pmirq, rc); |
| 420 | } |
| 421 | |
| 422 | static void pm8821_irq_unmask(struct irq_data *d) |
| 423 | { |
| 424 | struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d); |
| 425 | unsigned int pmirq = irqd_to_hwirq(d); |
| 426 | int irq_bit, rc; |
| 427 | u8 block, master; |
| 428 | |
| 429 | block = pmirq / 8; |
| 430 | master = block / PM8821_BLOCKS_PER_MASTER; |
| 431 | irq_bit = pmirq % 8; |
| 432 | block %= PM8821_BLOCKS_PER_MASTER; |
| 433 | |
| 434 | rc = regmap_update_bits(chip->regmap, |
| 435 | PM8821_SSBI_ADDR_IRQ_MASK(master, block), |
| 436 | BIT(irq_bit), ~BIT(irq_bit)); |
| 437 | if (rc) |
| 438 | pr_err("Failed to read/write unmask IRQ:%d rc=%d\n", pmirq, rc); |
| 439 | |
| 440 | } |
| 441 | |
| 442 | static int pm8821_irq_get_irqchip_state(struct irq_data *d, |
| 443 | enum irqchip_irq_state which, |
| 444 | bool *state) |
| 445 | { |
| 446 | struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d); |
| 447 | int rc, pmirq = irqd_to_hwirq(d); |
| 448 | u8 block, irq_bit, master; |
| 449 | unsigned int bits; |
| 450 | |
| 451 | block = pmirq / 8; |
| 452 | master = block / PM8821_BLOCKS_PER_MASTER; |
| 453 | irq_bit = pmirq % 8; |
| 454 | block %= PM8821_BLOCKS_PER_MASTER; |
| 455 | |
| 456 | rc = regmap_read(chip->regmap, |
| 457 | PM8821_SSBI_ADDR_IRQ_RT_STATUS(master, block), &bits); |
| 458 | if (rc) { |
| 459 | pr_err("Reading Status of IRQ %d failed rc=%d\n", pmirq, rc); |
| 460 | return rc; |
| 461 | } |
| 462 | |
| 463 | *state = !!(bits & BIT(irq_bit)); |
| 464 | |
| 465 | return rc; |
| 466 | } |
| 467 | |
| 468 | static struct irq_chip pm8821_irq_chip = { |
| 469 | .name = "pm8821", |
| 470 | .irq_mask_ack = pm8821_irq_mask_ack, |
| 471 | .irq_unmask = pm8821_irq_unmask, |
| 472 | .irq_get_irqchip_state = pm8821_irq_get_irqchip_state, |
| 473 | .flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_SKIP_SET_WAKE, |
| 474 | }; |
| 475 | |
| 476 | static int pm8821_irq_domain_map(struct irq_domain *d, unsigned int irq, |
| 477 | irq_hw_number_t hwirq) |
| 478 | { |
| 479 | struct pm_irq_chip *chip = d->host_data; |
| 480 | |
| 481 | irq_set_chip_and_handler(irq, &pm8821_irq_chip, handle_level_irq); |
| 482 | irq_set_chip_data(irq, chip); |
| 483 | irq_set_noprobe(irq); |
| 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | static const struct irq_domain_ops pm8821_irq_domain_ops = { |
| 489 | .xlate = irq_domain_xlate_twocell, |
| 490 | .map = pm8821_irq_domain_map, |
| 491 | }; |
| 492 | |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 493 | static const struct regmap_config ssbi_regmap_config = { |
| 494 | .reg_bits = 16, |
| 495 | .val_bits = 8, |
| 496 | .max_register = 0x3ff, |
| 497 | .fast_io = true, |
| 498 | .reg_read = ssbi_reg_read, |
| 499 | .reg_write = ssbi_reg_write |
| 500 | }; |
| 501 | |
Srinivas Kandagatla | 953f432 | 2016-11-15 12:01:51 +0000 | [diff] [blame] | 502 | static const struct pm_irq_data pm8xxx_data = { |
| 503 | .num_irqs = PM8XXX_NR_IRQS, |
| 504 | .irq_domain_ops = &pm8xxx_irq_domain_ops, |
| 505 | .irq_handler = pm8xxx_irq_handler, |
| 506 | }; |
| 507 | |
| 508 | static const struct pm_irq_data pm8821_data = { |
| 509 | .num_irqs = PM8821_NR_IRQS, |
| 510 | .irq_domain_ops = &pm8821_irq_domain_ops, |
| 511 | .irq_handler = pm8821_irq_handler, |
| 512 | }; |
| 513 | |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 514 | static const struct of_device_id pm8xxx_id_table[] = { |
Srinivas Kandagatla | 953f432 | 2016-11-15 12:01:51 +0000 | [diff] [blame] | 515 | { .compatible = "qcom,pm8018", .data = &pm8xxx_data}, |
| 516 | { .compatible = "qcom,pm8058", .data = &pm8xxx_data}, |
| 517 | { .compatible = "qcom,pm8821", .data = &pm8821_data}, |
| 518 | { .compatible = "qcom,pm8921", .data = &pm8xxx_data}, |
Stephen Boyd | c5865a5 | 2014-02-26 10:59:24 -0800 | [diff] [blame] | 519 | { } |
| 520 | }; |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 521 | MODULE_DEVICE_TABLE(of, pm8xxx_id_table); |
Stephen Boyd | c5865a5 | 2014-02-26 10:59:24 -0800 | [diff] [blame] | 522 | |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 523 | static int pm8xxx_probe(struct platform_device *pdev) |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 524 | { |
Srinivas Kandagatla | 953f432 | 2016-11-15 12:01:51 +0000 | [diff] [blame] | 525 | const struct pm_irq_data *data; |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 526 | struct regmap *regmap; |
Josh Cartwright | 202f768 | 2014-03-05 13:34:25 -0600 | [diff] [blame] | 527 | int irq, rc; |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 528 | unsigned int val; |
Abhijeet Dharmapurikar | c013f0a | 2011-04-05 14:40:53 -0700 | [diff] [blame] | 529 | u32 rev; |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 530 | struct pm_irq_chip *chip; |
Srinivas Kandagatla | 953f432 | 2016-11-15 12:01:51 +0000 | [diff] [blame] | 531 | |
| 532 | data = of_device_get_match_data(&pdev->dev); |
| 533 | if (!data) { |
| 534 | dev_err(&pdev->dev, "No matching driver data found\n"); |
| 535 | return -EINVAL; |
| 536 | } |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 537 | |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 538 | irq = platform_get_irq(pdev, 0); |
| 539 | if (irq < 0) |
| 540 | return irq; |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 541 | |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 542 | regmap = devm_regmap_init(&pdev->dev, NULL, pdev->dev.parent, |
| 543 | &ssbi_regmap_config); |
| 544 | if (IS_ERR(regmap)) |
| 545 | return PTR_ERR(regmap); |
| 546 | |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 547 | /* Read PMIC chip revision */ |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 548 | rc = regmap_read(regmap, REG_HWREV, &val); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 549 | if (rc) { |
| 550 | pr_err("Failed to read hw rev reg %d:rc=%d\n", REG_HWREV, rc); |
Jingoo Han | b2cdcfa | 2013-08-20 16:02:26 +0900 | [diff] [blame] | 551 | return rc; |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 552 | } |
| 553 | pr_info("PMIC revision 1: %02X\n", val); |
Abhijeet Dharmapurikar | c013f0a | 2011-04-05 14:40:53 -0700 | [diff] [blame] | 554 | rev = val; |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 555 | |
| 556 | /* Read PMIC chip revision 2 */ |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 557 | rc = regmap_read(regmap, REG_HWREV_2, &val); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 558 | if (rc) { |
| 559 | pr_err("Failed to read hw rev 2 reg %d:rc=%d\n", |
| 560 | REG_HWREV_2, rc); |
Jingoo Han | b2cdcfa | 2013-08-20 16:02:26 +0900 | [diff] [blame] | 561 | return rc; |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 562 | } |
| 563 | pr_info("PMIC revision 2: %02X\n", val); |
Abhijeet Dharmapurikar | c013f0a | 2011-04-05 14:40:53 -0700 | [diff] [blame] | 564 | rev |= val << BITS_PER_BYTE; |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 565 | |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 566 | chip = devm_kzalloc(&pdev->dev, sizeof(*chip) + |
Srinivas Kandagatla | 953f432 | 2016-11-15 12:01:51 +0000 | [diff] [blame] | 567 | sizeof(chip->config[0]) * data->num_irqs, |
| 568 | GFP_KERNEL); |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 569 | if (!chip) |
| 570 | return -ENOMEM; |
| 571 | |
Stephen Boyd | 3e87933 | 2014-04-08 17:14:15 -0700 | [diff] [blame] | 572 | platform_set_drvdata(pdev, chip); |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 573 | chip->regmap = regmap; |
Srinivas Kandagatla | 953f432 | 2016-11-15 12:01:51 +0000 | [diff] [blame] | 574 | chip->num_irqs = data->num_irqs; |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 575 | chip->num_blocks = DIV_ROUND_UP(chip->num_irqs, 8); |
| 576 | chip->num_masters = DIV_ROUND_UP(chip->num_blocks, 8); |
| 577 | spin_lock_init(&chip->pm_irq_lock); |
| 578 | |
Srinivas Kandagatla | 953f432 | 2016-11-15 12:01:51 +0000 | [diff] [blame] | 579 | chip->irqdomain = irq_domain_add_linear(pdev->dev.of_node, |
| 580 | data->num_irqs, |
| 581 | data->irq_domain_ops, |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 582 | chip); |
| 583 | if (!chip->irqdomain) |
| 584 | return -ENODEV; |
| 585 | |
Srinivas Kandagatla | 953f432 | 2016-11-15 12:01:51 +0000 | [diff] [blame] | 586 | irq_set_chained_handler_and_data(irq, data->irq_handler, chip); |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 587 | irq_set_irq_wake(irq, 1); |
| 588 | |
| 589 | rc = of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev); |
Abhijeet Dharmapurikar | c013f0a | 2011-04-05 14:40:53 -0700 | [diff] [blame] | 590 | if (rc) { |
Thomas Gleixner | b79f043 | 2015-07-13 20:44:51 +0000 | [diff] [blame] | 591 | irq_set_chained_handler_and_data(irq, NULL, NULL); |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 592 | irq_domain_remove(chip->irqdomain); |
Abhijeet Dharmapurikar | c013f0a | 2011-04-05 14:40:53 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 595 | return rc; |
| 596 | } |
| 597 | |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 598 | static int pm8xxx_remove_child(struct device *dev, void *unused) |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 599 | { |
| 600 | platform_device_unregister(to_platform_device(dev)); |
| 601 | return 0; |
| 602 | } |
| 603 | |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 604 | static int pm8xxx_remove(struct platform_device *pdev) |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 605 | { |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 606 | int irq = platform_get_irq(pdev, 0); |
Stephen Boyd | 3e87933 | 2014-04-08 17:14:15 -0700 | [diff] [blame] | 607 | struct pm_irq_chip *chip = platform_get_drvdata(pdev); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 608 | |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 609 | device_for_each_child(&pdev->dev, NULL, pm8xxx_remove_child); |
Thomas Gleixner | b79f043 | 2015-07-13 20:44:51 +0000 | [diff] [blame] | 610 | irq_set_chained_handler_and_data(irq, NULL, NULL); |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 611 | irq_domain_remove(chip->irqdomain); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 612 | |
| 613 | return 0; |
| 614 | } |
| 615 | |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 616 | static struct platform_driver pm8xxx_driver = { |
| 617 | .probe = pm8xxx_probe, |
| 618 | .remove = pm8xxx_remove, |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 619 | .driver = { |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 620 | .name = "pm8xxx-core", |
| 621 | .of_match_table = pm8xxx_id_table, |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 622 | }, |
| 623 | }; |
| 624 | |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 625 | static int __init pm8xxx_init(void) |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 626 | { |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 627 | return platform_driver_register(&pm8xxx_driver); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 628 | } |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 629 | subsys_initcall(pm8xxx_init); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 630 | |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 631 | static void __exit pm8xxx_exit(void) |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 632 | { |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 633 | platform_driver_unregister(&pm8xxx_driver); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 634 | } |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 635 | module_exit(pm8xxx_exit); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 636 | |
| 637 | MODULE_LICENSE("GPL v2"); |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 638 | MODULE_DESCRIPTION("PMIC 8xxx core driver"); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 639 | MODULE_VERSION("1.0"); |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 640 | MODULE_ALIAS("platform:pm8xxx-core"); |