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 | |
| 42 | #define PM_IRQF_LVL_SEL 0x01 /* level select */ |
| 43 | #define PM_IRQF_MASK_FE 0x02 /* mask falling edge */ |
| 44 | #define PM_IRQF_MASK_RE 0x04 /* mask rising edge */ |
| 45 | #define PM_IRQF_CLR 0x08 /* clear interrupt */ |
| 46 | #define PM_IRQF_BITS_MASK 0x70 |
| 47 | #define PM_IRQF_BITS_SHIFT 4 |
| 48 | #define PM_IRQF_WRITE 0x80 |
| 49 | |
| 50 | #define PM_IRQF_MASK_ALL (PM_IRQF_MASK_FE | \ |
| 51 | PM_IRQF_MASK_RE) |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 52 | |
| 53 | #define REG_HWREV 0x002 /* PMIC4 revision */ |
| 54 | #define REG_HWREV_2 0x0E8 /* PMIC4 revision 2 */ |
| 55 | |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 56 | #define PM8XXX_NR_IRQS 256 |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 57 | |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 58 | struct pm_irq_chip { |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 59 | struct regmap *regmap; |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 60 | spinlock_t pm_irq_lock; |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 61 | struct irq_domain *irqdomain; |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 62 | unsigned int num_irqs; |
| 63 | unsigned int num_blocks; |
| 64 | unsigned int num_masters; |
| 65 | u8 config[0]; |
| 66 | }; |
| 67 | |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 68 | static int pm8xxx_read_block_irq(struct pm_irq_chip *chip, unsigned int bp, |
| 69 | unsigned int *ip) |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 70 | { |
| 71 | int rc; |
| 72 | |
| 73 | spin_lock(&chip->pm_irq_lock); |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 74 | rc = regmap_write(chip->regmap, SSBI_REG_ADDR_IRQ_BLK_SEL, bp); |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 75 | if (rc) { |
| 76 | pr_err("Failed Selecting Block %d rc=%d\n", bp, rc); |
| 77 | goto bail; |
| 78 | } |
| 79 | |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 80 | rc = regmap_read(chip->regmap, SSBI_REG_ADDR_IRQ_IT_STATUS, ip); |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 81 | if (rc) |
| 82 | pr_err("Failed Reading Status rc=%d\n", rc); |
| 83 | bail: |
| 84 | spin_unlock(&chip->pm_irq_lock); |
| 85 | return rc; |
| 86 | } |
| 87 | |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 88 | static int |
| 89 | 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] | 90 | { |
| 91 | int rc; |
| 92 | |
| 93 | spin_lock(&chip->pm_irq_lock); |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 94 | rc = regmap_write(chip->regmap, SSBI_REG_ADDR_IRQ_BLK_SEL, bp); |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 95 | if (rc) { |
| 96 | pr_err("Failed Selecting Block %d rc=%d\n", bp, rc); |
| 97 | goto bail; |
| 98 | } |
| 99 | |
| 100 | cp |= PM_IRQF_WRITE; |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 101 | rc = regmap_write(chip->regmap, SSBI_REG_ADDR_IRQ_CONFIG, cp); |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 102 | if (rc) |
| 103 | pr_err("Failed Configuring IRQ rc=%d\n", rc); |
| 104 | bail: |
| 105 | spin_unlock(&chip->pm_irq_lock); |
| 106 | return rc; |
| 107 | } |
| 108 | |
| 109 | static int pm8xxx_irq_block_handler(struct pm_irq_chip *chip, int block) |
| 110 | { |
| 111 | int pmirq, irq, i, ret = 0; |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 112 | unsigned int bits; |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 113 | |
| 114 | ret = pm8xxx_read_block_irq(chip, block, &bits); |
| 115 | if (ret) { |
| 116 | pr_err("Failed reading %d block ret=%d", block, ret); |
| 117 | return ret; |
| 118 | } |
| 119 | if (!bits) { |
| 120 | pr_err("block bit set in master but no irqs: %d", block); |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | /* Check IRQ bits */ |
| 125 | for (i = 0; i < 8; i++) { |
| 126 | if (bits & (1 << i)) { |
| 127 | pmirq = block * 8 + i; |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 128 | irq = irq_find_mapping(chip->irqdomain, pmirq); |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 129 | generic_handle_irq(irq); |
| 130 | } |
| 131 | } |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static int pm8xxx_irq_master_handler(struct pm_irq_chip *chip, int master) |
| 136 | { |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 137 | unsigned int blockbits; |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 138 | int block_number, i, ret = 0; |
| 139 | |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 140 | ret = regmap_read(chip->regmap, SSBI_REG_ADDR_IRQ_M_STATUS1 + master, |
| 141 | &blockbits); |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 142 | if (ret) { |
| 143 | pr_err("Failed to read master %d ret=%d\n", master, ret); |
| 144 | return ret; |
| 145 | } |
| 146 | if (!blockbits) { |
| 147 | pr_err("master bit set in root but no blocks: %d", master); |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | for (i = 0; i < 8; i++) |
| 152 | if (blockbits & (1 << i)) { |
| 153 | block_number = master * 8 + i; /* block # */ |
| 154 | ret |= pm8xxx_irq_block_handler(chip, block_number); |
| 155 | } |
| 156 | return ret; |
| 157 | } |
| 158 | |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 159 | static void pm8xxx_irq_handler(struct irq_desc *desc) |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 160 | { |
| 161 | struct pm_irq_chip *chip = irq_desc_get_handler_data(desc); |
| 162 | struct irq_chip *irq_chip = irq_desc_get_chip(desc); |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 163 | unsigned int root; |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 164 | int i, ret, masters = 0; |
| 165 | |
Stephen Boyd | cced354 | 2014-02-26 10:59:20 -0800 | [diff] [blame] | 166 | chained_irq_enter(irq_chip, desc); |
| 167 | |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 168 | ret = regmap_read(chip->regmap, SSBI_REG_ADDR_IRQ_ROOT, &root); |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 169 | if (ret) { |
| 170 | pr_err("Can't read root status ret=%d\n", ret); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | /* on pm8xxx series masters start from bit 1 of the root */ |
| 175 | masters = root >> 1; |
| 176 | |
| 177 | /* Read allowed masters for blocks. */ |
| 178 | for (i = 0; i < chip->num_masters; i++) |
| 179 | if (masters & (1 << i)) |
| 180 | pm8xxx_irq_master_handler(chip, i); |
| 181 | |
Stephen Boyd | cced354 | 2014-02-26 10:59:20 -0800 | [diff] [blame] | 182 | chained_irq_exit(irq_chip, desc); |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static void pm8xxx_irq_mask_ack(struct irq_data *d) |
| 186 | { |
| 187 | struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d); |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 188 | unsigned int pmirq = irqd_to_hwirq(d); |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 189 | u8 block, config; |
| 190 | |
| 191 | block = pmirq / 8; |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 192 | |
| 193 | config = chip->config[pmirq] | PM_IRQF_MASK_ALL | PM_IRQF_CLR; |
| 194 | pm8xxx_config_irq(chip, block, config); |
| 195 | } |
| 196 | |
| 197 | static void pm8xxx_irq_unmask(struct irq_data *d) |
| 198 | { |
| 199 | struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d); |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 200 | unsigned int pmirq = irqd_to_hwirq(d); |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 201 | u8 block, config; |
| 202 | |
| 203 | block = pmirq / 8; |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 204 | |
| 205 | config = chip->config[pmirq]; |
| 206 | pm8xxx_config_irq(chip, block, config); |
| 207 | } |
| 208 | |
| 209 | static int pm8xxx_irq_set_type(struct irq_data *d, unsigned int flow_type) |
| 210 | { |
| 211 | struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d); |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 212 | unsigned int pmirq = irqd_to_hwirq(d); |
| 213 | int irq_bit; |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 214 | u8 block, config; |
| 215 | |
| 216 | block = pmirq / 8; |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 217 | irq_bit = pmirq % 8; |
| 218 | |
| 219 | chip->config[pmirq] = (irq_bit << PM_IRQF_BITS_SHIFT) |
| 220 | | PM_IRQF_MASK_ALL; |
| 221 | if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) { |
| 222 | if (flow_type & IRQF_TRIGGER_RISING) |
| 223 | chip->config[pmirq] &= ~PM_IRQF_MASK_RE; |
| 224 | if (flow_type & IRQF_TRIGGER_FALLING) |
| 225 | chip->config[pmirq] &= ~PM_IRQF_MASK_FE; |
| 226 | } else { |
| 227 | chip->config[pmirq] |= PM_IRQF_LVL_SEL; |
| 228 | |
| 229 | if (flow_type & IRQF_TRIGGER_HIGH) |
| 230 | chip->config[pmirq] &= ~PM_IRQF_MASK_RE; |
| 231 | else |
| 232 | chip->config[pmirq] &= ~PM_IRQF_MASK_FE; |
| 233 | } |
| 234 | |
| 235 | config = chip->config[pmirq] | PM_IRQF_CLR; |
| 236 | return pm8xxx_config_irq(chip, block, config); |
| 237 | } |
| 238 | |
Bjorn Andersson | f1837e4 | 2015-07-14 23:40:34 -0700 | [diff] [blame] | 239 | static int pm8xxx_irq_get_irqchip_state(struct irq_data *d, |
| 240 | enum irqchip_irq_state which, |
| 241 | bool *state) |
| 242 | { |
| 243 | struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d); |
| 244 | unsigned int pmirq = irqd_to_hwirq(d); |
| 245 | unsigned int bits; |
| 246 | int irq_bit; |
| 247 | u8 block; |
| 248 | int rc; |
| 249 | |
| 250 | if (which != IRQCHIP_STATE_LINE_LEVEL) |
| 251 | return -EINVAL; |
| 252 | |
| 253 | block = pmirq / 8; |
| 254 | irq_bit = pmirq % 8; |
| 255 | |
| 256 | spin_lock(&chip->pm_irq_lock); |
| 257 | rc = regmap_write(chip->regmap, SSBI_REG_ADDR_IRQ_BLK_SEL, block); |
| 258 | if (rc) { |
| 259 | pr_err("Failed Selecting Block %d rc=%d\n", block, rc); |
| 260 | goto bail; |
| 261 | } |
| 262 | |
| 263 | rc = regmap_read(chip->regmap, SSBI_REG_ADDR_IRQ_RT_STATUS, &bits); |
| 264 | if (rc) { |
| 265 | pr_err("Failed Reading Status rc=%d\n", rc); |
| 266 | goto bail; |
| 267 | } |
| 268 | |
| 269 | *state = !!(bits & BIT(irq_bit)); |
| 270 | bail: |
| 271 | spin_unlock(&chip->pm_irq_lock); |
| 272 | |
| 273 | return rc; |
| 274 | } |
| 275 | |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 276 | static struct irq_chip pm8xxx_irq_chip = { |
| 277 | .name = "pm8xxx", |
| 278 | .irq_mask_ack = pm8xxx_irq_mask_ack, |
| 279 | .irq_unmask = pm8xxx_irq_unmask, |
| 280 | .irq_set_type = pm8xxx_irq_set_type, |
Bjorn Andersson | f1837e4 | 2015-07-14 23:40:34 -0700 | [diff] [blame] | 281 | .irq_get_irqchip_state = pm8xxx_irq_get_irqchip_state, |
Stephen Boyd | d2d24ad | 2014-03-04 10:48:52 -0800 | [diff] [blame] | 282 | .flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_SKIP_SET_WAKE, |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 283 | }; |
| 284 | |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 285 | static int pm8xxx_irq_domain_map(struct irq_domain *d, unsigned int irq, |
| 286 | irq_hw_number_t hwirq) |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 287 | { |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 288 | struct pm_irq_chip *chip = d->host_data; |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 289 | |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 290 | irq_set_chip_and_handler(irq, &pm8xxx_irq_chip, handle_level_irq); |
| 291 | irq_set_chip_data(irq, chip); |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 292 | irq_set_noprobe(irq); |
Rob Herring | 9bd09f3 | 2015-07-27 15:55:20 -0500 | [diff] [blame] | 293 | |
Stephen Boyd | bc866fc | 2014-02-26 10:59:19 -0800 | [diff] [blame] | 294 | return 0; |
| 295 | } |
| 296 | |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 297 | static const struct irq_domain_ops pm8xxx_irq_domain_ops = { |
| 298 | .xlate = irq_domain_xlate_twocell, |
| 299 | .map = pm8xxx_irq_domain_map, |
| 300 | }; |
| 301 | |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 302 | static const struct regmap_config ssbi_regmap_config = { |
| 303 | .reg_bits = 16, |
| 304 | .val_bits = 8, |
| 305 | .max_register = 0x3ff, |
| 306 | .fast_io = true, |
| 307 | .reg_read = ssbi_reg_read, |
| 308 | .reg_write = ssbi_reg_write |
| 309 | }; |
| 310 | |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 311 | static const struct of_device_id pm8xxx_id_table[] = { |
Neil Armstrong | c7ef587 | 2016-08-11 15:16:43 +0200 | [diff] [blame] | 312 | { .compatible = "qcom,pm8018", }, |
Stephen Boyd | c5865a5 | 2014-02-26 10:59:24 -0800 | [diff] [blame] | 313 | { .compatible = "qcom,pm8058", }, |
| 314 | { .compatible = "qcom,pm8921", }, |
| 315 | { } |
| 316 | }; |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 317 | MODULE_DEVICE_TABLE(of, pm8xxx_id_table); |
Stephen Boyd | c5865a5 | 2014-02-26 10:59:24 -0800 | [diff] [blame] | 318 | |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 319 | static int pm8xxx_probe(struct platform_device *pdev) |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 320 | { |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 321 | struct regmap *regmap; |
Josh Cartwright | 202f768 | 2014-03-05 13:34:25 -0600 | [diff] [blame] | 322 | int irq, rc; |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 323 | unsigned int val; |
Abhijeet Dharmapurikar | c013f0a | 2011-04-05 14:40:53 -0700 | [diff] [blame] | 324 | u32 rev; |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 325 | struct pm_irq_chip *chip; |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 326 | unsigned int nirqs = PM8XXX_NR_IRQS; |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 327 | |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 328 | irq = platform_get_irq(pdev, 0); |
| 329 | if (irq < 0) |
| 330 | return irq; |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 331 | |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 332 | regmap = devm_regmap_init(&pdev->dev, NULL, pdev->dev.parent, |
| 333 | &ssbi_regmap_config); |
| 334 | if (IS_ERR(regmap)) |
| 335 | return PTR_ERR(regmap); |
| 336 | |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 337 | /* Read PMIC chip revision */ |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 338 | rc = regmap_read(regmap, REG_HWREV, &val); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 339 | if (rc) { |
| 340 | 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] | 341 | return rc; |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 342 | } |
| 343 | pr_info("PMIC revision 1: %02X\n", val); |
Abhijeet Dharmapurikar | c013f0a | 2011-04-05 14:40:53 -0700 | [diff] [blame] | 344 | rev = val; |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 345 | |
| 346 | /* Read PMIC chip revision 2 */ |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 347 | rc = regmap_read(regmap, REG_HWREV_2, &val); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 348 | if (rc) { |
| 349 | pr_err("Failed to read hw rev 2 reg %d:rc=%d\n", |
| 350 | REG_HWREV_2, rc); |
Jingoo Han | b2cdcfa | 2013-08-20 16:02:26 +0900 | [diff] [blame] | 351 | return rc; |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 352 | } |
| 353 | pr_info("PMIC revision 2: %02X\n", val); |
Abhijeet Dharmapurikar | c013f0a | 2011-04-05 14:40:53 -0700 | [diff] [blame] | 354 | rev |= val << BITS_PER_BYTE; |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 355 | |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 356 | chip = devm_kzalloc(&pdev->dev, sizeof(*chip) + |
| 357 | sizeof(chip->config[0]) * nirqs, |
| 358 | GFP_KERNEL); |
| 359 | if (!chip) |
| 360 | return -ENOMEM; |
| 361 | |
Stephen Boyd | 3e87933 | 2014-04-08 17:14:15 -0700 | [diff] [blame] | 362 | platform_set_drvdata(pdev, chip); |
Stephen Boyd | e7b81fca | 2014-02-26 10:59:23 -0800 | [diff] [blame] | 363 | chip->regmap = regmap; |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 364 | chip->num_irqs = nirqs; |
| 365 | chip->num_blocks = DIV_ROUND_UP(chip->num_irqs, 8); |
| 366 | chip->num_masters = DIV_ROUND_UP(chip->num_blocks, 8); |
| 367 | spin_lock_init(&chip->pm_irq_lock); |
| 368 | |
| 369 | chip->irqdomain = irq_domain_add_linear(pdev->dev.of_node, nirqs, |
| 370 | &pm8xxx_irq_domain_ops, |
| 371 | chip); |
| 372 | if (!chip->irqdomain) |
| 373 | return -ENODEV; |
| 374 | |
Thomas Gleixner | b79f043 | 2015-07-13 20:44:51 +0000 | [diff] [blame] | 375 | irq_set_chained_handler_and_data(irq, pm8xxx_irq_handler, chip); |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 376 | irq_set_irq_wake(irq, 1); |
| 377 | |
| 378 | rc = of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev); |
Abhijeet Dharmapurikar | c013f0a | 2011-04-05 14:40:53 -0700 | [diff] [blame] | 379 | if (rc) { |
Thomas Gleixner | b79f043 | 2015-07-13 20:44:51 +0000 | [diff] [blame] | 380 | irq_set_chained_handler_and_data(irq, NULL, NULL); |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 381 | irq_domain_remove(chip->irqdomain); |
Abhijeet Dharmapurikar | c013f0a | 2011-04-05 14:40:53 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 384 | return rc; |
| 385 | } |
| 386 | |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 387 | static int pm8xxx_remove_child(struct device *dev, void *unused) |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 388 | { |
| 389 | platform_device_unregister(to_platform_device(dev)); |
| 390 | return 0; |
| 391 | } |
| 392 | |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 393 | static int pm8xxx_remove(struct platform_device *pdev) |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 394 | { |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 395 | int irq = platform_get_irq(pdev, 0); |
Stephen Boyd | 3e87933 | 2014-04-08 17:14:15 -0700 | [diff] [blame] | 396 | struct pm_irq_chip *chip = platform_get_drvdata(pdev); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 397 | |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 398 | device_for_each_child(&pdev->dev, NULL, pm8xxx_remove_child); |
Thomas Gleixner | b79f043 | 2015-07-13 20:44:51 +0000 | [diff] [blame] | 399 | irq_set_chained_handler_and_data(irq, NULL, NULL); |
Stephen Boyd | dc1a95c | 2014-02-26 10:59:21 -0800 | [diff] [blame] | 400 | irq_domain_remove(chip->irqdomain); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 401 | |
| 402 | return 0; |
| 403 | } |
| 404 | |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 405 | static struct platform_driver pm8xxx_driver = { |
| 406 | .probe = pm8xxx_probe, |
| 407 | .remove = pm8xxx_remove, |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 408 | .driver = { |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 409 | .name = "pm8xxx-core", |
| 410 | .of_match_table = pm8xxx_id_table, |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 411 | }, |
| 412 | }; |
| 413 | |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 414 | static int __init pm8xxx_init(void) |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 415 | { |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 416 | return platform_driver_register(&pm8xxx_driver); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 417 | } |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 418 | subsys_initcall(pm8xxx_init); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 419 | |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 420 | static void __exit pm8xxx_exit(void) |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 421 | { |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 422 | platform_driver_unregister(&pm8xxx_driver); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 423 | } |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 424 | module_exit(pm8xxx_exit); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 425 | |
| 426 | MODULE_LICENSE("GPL v2"); |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 427 | MODULE_DESCRIPTION("PMIC 8xxx core driver"); |
Abhijeet Dharmapurikar | cbdb53e | 2011-04-05 14:40:52 -0700 | [diff] [blame] | 428 | MODULE_VERSION("1.0"); |
Linus Walleij | 40a3a0f | 2016-11-19 16:15:18 +0100 | [diff] [blame] | 429 | MODULE_ALIAS("platform:pm8xxx-core"); |