| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // |
| 3 | // Copyright (c) 2019 MediaTek Inc. |
| 4 | |
| 5 | #include <linux/interrupt.h> |
| 6 | #include <linux/mfd/mt6330/core.h> |
| 7 | #include <linux/mfd/mt6330/registers.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/of.h> |
| 10 | #include <linux/of_device.h> |
| 11 | #include <linux/of_irq.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/regmap.h> |
| 14 | |
| 15 | #undef MT6330_SPMIMST_RCSCLR |
| 16 | #define MT6330_SPMIMST_STARTADDR (0x10029000) |
| 17 | #define MT6330_SPMIMST_ENDADDR (0x100290FF) |
| 18 | #define MT6330_REG_SPMIMST_RCSCLR (0x24) |
| 19 | #define MT6330_MSK_SPMIMST_RCSCLR (0xFF) |
| 20 | |
| 21 | struct irq_top_t { |
| 22 | int hwirq_base; |
| 23 | unsigned int num_int_regs; |
| 24 | unsigned int en_reg; |
| 25 | unsigned int en_reg_shift; |
| 26 | unsigned int sta_reg; |
| 27 | unsigned int sta_reg_shift; |
| 28 | unsigned int top_offset; |
| rjw | 2e8229f | 2022-02-15 21:08:12 +0800 | [diff] [blame] | 29 | unsigned int top_mask_offset; |
| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | struct pmic_irq_data { |
| 33 | unsigned int num_top; |
| 34 | unsigned int num_pmic_irqs; |
| 35 | unsigned int reg_width; |
| 36 | unsigned short top_int_status_reg_l; |
| 37 | unsigned short top_int_status_reg_h; |
| rjw | 2e8229f | 2022-02-15 21:08:12 +0800 | [diff] [blame] | 38 | unsigned short top_int_mask_set_reg; |
| 39 | unsigned short top_int_mask_clr_reg; |
| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 40 | bool *enable_hwirq; |
| 41 | bool *cache_hwirq; |
| 42 | struct irq_top_t *pmic_ints; |
| 43 | }; |
| 44 | |
| 45 | static struct irq_top_t mt6330_ints[] = { |
| 46 | MT6330_TOP_GEN(MISC), |
| 47 | MT6330_TOP_GEN(BUCK), |
| 48 | MT6330_TOP_GEN(LDO), |
| 49 | MT6330_TOP_GEN(PSC), |
| 50 | MT6330_TOP_GEN(SCK), |
| 51 | }; |
| 52 | |
| 53 | #ifdef MT6330_SPMIMST_RCSCLR |
| 54 | static inline void mt6330_clear_spmimst_rcs(struct mt6330_chip *chip) |
| 55 | { |
| 56 | writel(MT6330_MSK_SPMIMST_RCSCLR, |
| 57 | (chip->spmimst_base + MT6330_REG_SPMIMST_RCSCLR)); |
| 58 | } |
| 59 | |
| 60 | static struct resource spmimst_resource = { |
| 61 | .start = MT6330_SPMIMST_STARTADDR, |
| 62 | .end = MT6330_SPMIMST_ENDADDR, |
| 63 | .flags = IORESOURCE_MEM, |
| 64 | .name = "spmimst", |
| 65 | }; |
| 66 | #endif /* MT6362_SPMIMST_RCSCLR */ |
| 67 | |
| 68 | static void pmic_irq_enable(struct irq_data *data) |
| 69 | { |
| 70 | unsigned int hwirq = irqd_to_hwirq(data); |
| 71 | struct mt6330_chip *chip = irq_data_get_irq_chip_data(data); |
| 72 | struct pmic_irq_data *irqd = chip->irq_data; |
| 73 | |
| 74 | irqd->enable_hwirq[hwirq] = true; |
| 75 | } |
| 76 | |
| 77 | static void pmic_irq_disable(struct irq_data *data) |
| 78 | { |
| 79 | unsigned int hwirq = irqd_to_hwirq(data); |
| 80 | struct mt6330_chip *chip = irq_data_get_irq_chip_data(data); |
| 81 | struct pmic_irq_data *irqd = chip->irq_data; |
| 82 | |
| 83 | irqd->enable_hwirq[hwirq] = false; |
| 84 | } |
| 85 | |
| 86 | static void pmic_irq_lock(struct irq_data *data) |
| 87 | { |
| 88 | struct mt6330_chip *chip = irq_data_get_irq_chip_data(data); |
| 89 | |
| 90 | mutex_lock(&chip->irqlock); |
| 91 | } |
| 92 | |
| 93 | static void pmic_irq_sync_unlock(struct irq_data *data) |
| 94 | { |
| 95 | unsigned int i, top_gp, en_reg, int_regs, shift; |
| 96 | struct mt6330_chip *chip = irq_data_get_irq_chip_data(data); |
| 97 | struct pmic_irq_data *irqd = chip->irq_data; |
| 98 | |
| 99 | for (i = 0; i < irqd->num_pmic_irqs; i++) { |
| 100 | if (irqd->enable_hwirq[i] == irqd->cache_hwirq[i]) |
| 101 | continue; |
| 102 | |
| 103 | top_gp = 0; |
| 104 | while ((top_gp + 1) < irqd->num_top && |
| 105 | i >= irqd->pmic_ints[top_gp + 1].hwirq_base) |
| 106 | top_gp++; |
| 107 | |
| 108 | if (top_gp >= irqd->num_top) { |
| 109 | mutex_unlock(&chip->irqlock); |
| 110 | dev_err(chip->dev, |
| 111 | "Failed to get top_group: %d\n", top_gp); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | int_regs = (i - irqd->pmic_ints[top_gp].hwirq_base) / |
| 116 | irqd->reg_width; |
| 117 | en_reg = irqd->pmic_ints[top_gp].en_reg + |
| 118 | irqd->pmic_ints[top_gp].en_reg_shift * int_regs; |
| 119 | shift = (i - irqd->pmic_ints[top_gp].hwirq_base) % |
| 120 | irqd->reg_width; |
| 121 | regmap_update_bits(chip->regmap, en_reg, BIT(shift), |
| 122 | irqd->enable_hwirq[i] << shift); |
| 123 | irqd->cache_hwirq[i] = irqd->enable_hwirq[i]; |
| 124 | } |
| 125 | mutex_unlock(&chip->irqlock); |
| 126 | } |
| 127 | |
| 128 | static struct irq_chip mt6330_irq_chip = { |
| 129 | .name = "mt6330-irq", |
| 130 | .flags = IRQCHIP_SKIP_SET_WAKE, |
| 131 | .irq_enable = pmic_irq_enable, |
| 132 | .irq_disable = pmic_irq_disable, |
| 133 | .irq_bus_lock = pmic_irq_lock, |
| 134 | .irq_bus_sync_unlock = pmic_irq_sync_unlock, |
| 135 | }; |
| 136 | |
| 137 | static void mt6330_irq_sp_handler(struct mt6330_chip *chip, |
| 138 | unsigned int top_gp) |
| 139 | { |
| 140 | unsigned int sta_reg, irq_status = 0; |
| 141 | unsigned int hwirq, virq; |
| 142 | int ret, i, j; |
| 143 | struct pmic_irq_data *irqd = chip->irq_data; |
| 144 | |
| 145 | for (i = 0; i < irqd->pmic_ints[top_gp].num_int_regs; i++) { |
| 146 | sta_reg = irqd->pmic_ints[top_gp].sta_reg + |
| 147 | irqd->pmic_ints[top_gp].sta_reg_shift * i; |
| 148 | ret = regmap_read(chip->regmap, sta_reg, &irq_status); |
| 149 | if (ret) { |
| 150 | dev_err(chip->dev, |
| 151 | "Failed to read irq status: %d\n", ret); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | if (!irq_status) |
| 156 | continue; |
| 157 | |
| 158 | for (j = 0; j < irqd->reg_width ; j++) { |
| 159 | if ((irq_status & BIT(j)) == 0) |
| 160 | continue; |
| 161 | hwirq = irqd->pmic_ints[top_gp].hwirq_base + |
| 162 | irqd->reg_width * i + j; |
| 163 | virq = irq_find_mapping(chip->irq_domain, hwirq); |
| 164 | dev_info(chip->dev, |
| 165 | "Reg[0x%x]=0x%x,hwirq=%d,type=%d\n", |
| 166 | sta_reg, irq_status, hwirq, |
| 167 | irq_get_trigger_type(virq)); |
| 168 | if (virq) |
| 169 | handle_nested_irq(virq); |
| 170 | } |
| 171 | |
| 172 | #ifdef MT6330_SPMIMST_RCSCLR |
| 173 | mt6330_clear_spmimst_rcs(chip); |
| 174 | #endif |
| 175 | regmap_write(chip->regmap, sta_reg, irq_status); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | extern void spmi_dump_spmimst_all_reg(void); |
| 180 | static irqreturn_t mt6330_irq_handler(int irq, void *data) |
| 181 | { |
| 182 | struct mt6330_chip *chip = data; |
| 183 | struct pmic_irq_data *irqd = chip->irq_data; |
| 184 | unsigned int top_irq_status_h = 0, top_irq_status = 0; |
| 185 | unsigned int i; |
| 186 | int ret; |
| 187 | |
| 188 | ret = regmap_read(chip->regmap, |
| 189 | irqd->top_int_status_reg_h, |
| 190 | &top_irq_status_h); |
| 191 | if (ret) { |
| 192 | dev_err(chip->dev, "Can't read TOP_INT_STATUS0 ret=%d\n", ret); |
| 193 | return IRQ_NONE; |
| 194 | } |
| 195 | |
| 196 | ret = regmap_read(chip->regmap, |
| 197 | irqd->top_int_status_reg_l, |
| 198 | &top_irq_status); |
| 199 | if (ret) { |
| 200 | dev_err(chip->dev, "Can't read TOP_INT_STATUS1 ret=%d\n", ret); |
| 201 | return IRQ_NONE; |
| 202 | } |
| 203 | top_irq_status |= (top_irq_status_h << 8); |
| 204 | |
| 205 | dev_info(chip->dev, "%s: top_irq_sts:0x%x\n", __func__, top_irq_status); |
| 206 | if (!top_irq_status) |
| 207 | spmi_dump_spmimst_all_reg(); |
| 208 | for (i = 0; i < irqd->num_top; i++) { |
| rjw | 2e8229f | 2022-02-15 21:08:12 +0800 | [diff] [blame] | 209 | if (top_irq_status & BIT(irqd->pmic_ints[i].top_offset)) { |
| 210 | /* Mask top INT */ |
| 211 | regmap_write(chip->regmap, irqd->top_int_mask_set_reg, |
| 212 | BIT(irqd->pmic_ints[i].top_mask_offset)); |
| 213 | |
| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 214 | mt6330_irq_sp_handler(chip, i); |
| rjw | 2e8229f | 2022-02-15 21:08:12 +0800 | [diff] [blame] | 215 | |
| 216 | /* Umask top INT */ |
| 217 | regmap_write(chip->regmap, irqd->top_int_mask_clr_reg, |
| 218 | BIT(irqd->pmic_ints[i].top_mask_offset)); |
| 219 | } |
| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | return IRQ_HANDLED; |
| 223 | } |
| 224 | |
| 225 | static int pmic_irq_domain_map(struct irq_domain *d, unsigned int irq, |
| 226 | irq_hw_number_t hw) |
| 227 | { |
| 228 | struct mt6330_chip *mt6330 = d->host_data; |
| 229 | |
| 230 | irq_set_chip_data(irq, mt6330); |
| 231 | irq_set_chip_and_handler(irq, &mt6330_irq_chip, handle_level_irq); |
| 232 | irq_set_nested_thread(irq, 1); |
| 233 | irq_set_noprobe(irq); |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static const struct irq_domain_ops mt6330_irq_domain_ops = { |
| 239 | .map = pmic_irq_domain_map, |
| 240 | .xlate = irq_domain_xlate_twocell, |
| 241 | }; |
| 242 | |
| 243 | int mt6330_irq_init(struct mt6330_chip *chip) |
| 244 | { |
| 245 | int i, j, ret; |
| 246 | struct pmic_irq_data *irqd; |
| 247 | |
| 248 | irqd = devm_kzalloc(chip->dev, sizeof(*irqd), GFP_KERNEL); |
| 249 | if (!irqd) |
| 250 | return -ENOMEM; |
| 251 | |
| 252 | chip->irq_data = irqd; |
| 253 | |
| 254 | mutex_init(&chip->irqlock); |
| 255 | irqd->num_top = ARRAY_SIZE(mt6330_ints); |
| 256 | irqd->num_pmic_irqs = MT6330_IRQ_NR; |
| 257 | irqd->reg_width = MT6330_REG_WIDTH; |
| 258 | irqd->top_int_status_reg_l = MT6330_TOP_INT_STATUS0; |
| 259 | irqd->top_int_status_reg_h = MT6330_TOP_INT_STATUS1; |
| rjw | 2e8229f | 2022-02-15 21:08:12 +0800 | [diff] [blame] | 260 | irqd->top_int_mask_set_reg = MT6330_TOP_INT_MASK_CON0_SET; |
| 261 | irqd->top_int_mask_clr_reg = MT6330_TOP_INT_MASK_CON0_CLR; |
| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 262 | irqd->pmic_ints = mt6330_ints; |
| 263 | |
| 264 | dev_info(chip->dev, "mt6330_irq_init +++\n"); |
| 265 | irqd->enable_hwirq = devm_kcalloc(chip->dev, |
| 266 | irqd->num_pmic_irqs, |
| 267 | sizeof(bool), |
| 268 | GFP_KERNEL); |
| 269 | if (!irqd->enable_hwirq) |
| 270 | return -ENOMEM; |
| 271 | |
| 272 | irqd->cache_hwirq = devm_kcalloc(chip->dev, |
| 273 | irqd->num_pmic_irqs, |
| 274 | sizeof(bool), |
| 275 | GFP_KERNEL); |
| 276 | if (!irqd->cache_hwirq) |
| 277 | return -ENOMEM; |
| 278 | |
| 279 | #ifdef MT6330_SPMIMST_RCSCLR |
| 280 | chip->spmimst_base = devm_ioremap(chip->dev, spmimst_resource.start, |
| 281 | resource_size(&spmimst_resource)); |
| 282 | if (!chip->spmimst_base) { |
| 283 | dev_notice(chip->dev, |
| 284 | "Failed to ioremap spmi master address\n"); |
| 285 | return -EINVAL; |
| 286 | } |
| 287 | mt6330_clear_spmimst_rcs(chip); |
| 288 | #endif /* MT6330_SPMIMST_RCSCLR */ |
| 289 | |
| 290 | /* Disable all interrupt for initializing */ |
| 291 | for (i = 0; i < irqd->num_top; i++) { |
| 292 | for (j = 0; j < irqd->pmic_ints[i].num_int_regs; j++) |
| 293 | regmap_write(chip->regmap, |
| 294 | irqd->pmic_ints[i].en_reg + |
| 295 | irqd->pmic_ints[i].en_reg_shift * j, 0); |
| 296 | } |
| 297 | |
| 298 | chip->irq_domain = irq_domain_add_linear(chip->dev->of_node, |
| 299 | irqd->num_pmic_irqs, |
| 300 | &mt6330_irq_domain_ops, chip); |
| 301 | if (!chip->irq_domain) { |
| 302 | dev_err(chip->dev, "could not create irq domain\n"); |
| 303 | return -ENODEV; |
| 304 | } |
| 305 | |
| 306 | ret = devm_request_threaded_irq(chip->dev, chip->irq, NULL, |
| 307 | mt6330_irq_handler, IRQF_ONESHOT, |
| 308 | mt6330_irq_chip.name, chip); |
| 309 | if (ret) { |
| 310 | dev_err(chip->dev, "failed to register irq=%d; err: %d\n", |
| 311 | chip->irq, ret); |
| 312 | return ret; |
| 313 | } |
| 314 | |
| 315 | enable_irq_wake(chip->irq); |
| 316 | dev_info(chip->dev, "mt6330_irq_init ---\n"); |
| 317 | return ret; |
| 318 | } |