1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // regmap based irq_chip 4 // 5 // Copyright 2011 Wolfson Microelectronics plc 6 // 7 // Author: Mark Brown <broonie@opensource.wolfsonmicro.com> 8 9 #include <linux/device.h> 10 #include <linux/export.h> 11 #include <linux/interrupt.h> 12 #include <linux/irq.h> 13 #include <linux/irqdomain.h> 14 #include <linux/pm_runtime.h> 15 #include <linux/regmap.h> 16 #include <linux/slab.h> 17 18 #include "internal.h" 19 20 struct regmap_irq_chip_data { 21 struct mutex lock; 22 struct irq_chip irq_chip; 23 24 struct regmap *map; 25 const struct regmap_irq_chip *chip; 26 27 int irq_base; 28 struct irq_domain *domain; 29 30 int irq; 31 int wake_count; 32 33 void *status_reg_buf; 34 unsigned int *main_status_buf; 35 unsigned int *status_buf; 36 unsigned int *mask_buf; 37 unsigned int *mask_buf_def; 38 unsigned int *wake_buf; 39 unsigned int *type_buf; 40 unsigned int *type_buf_def; 41 unsigned int **config_buf; 42 43 unsigned int irq_reg_stride; 44 45 unsigned int (*get_irq_reg)(struct regmap_irq_chip_data *data, 46 unsigned int base, int index); 47 48 unsigned int clear_status:1; 49 }; 50 51 static inline const 52 struct regmap_irq *irq_to_regmap_irq(struct regmap_irq_chip_data *data, 53 int irq) 54 { 55 return &data->chip->irqs[irq]; 56 } 57 58 static bool regmap_irq_can_bulk_read_status(struct regmap_irq_chip_data *data) 59 { 60 struct regmap *map = data->map; 61 62 /* 63 * While possible that a user-defined ->get_irq_reg() callback might 64 * be linear enough to support bulk reads, most of the time it won't. 65 * Therefore only allow them if the default callback is being used. 66 */ 67 return data->irq_reg_stride == 1 && map->reg_stride == 1 && 68 data->get_irq_reg == regmap_irq_get_irq_reg_linear && 69 !map->use_single_read; 70 } 71 72 static void regmap_irq_lock(struct irq_data *data) 73 { 74 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data); 75 76 mutex_lock(&d->lock); 77 } 78 79 static void regmap_irq_sync_unlock(struct irq_data *data) 80 { 81 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data); 82 struct regmap *map = d->map; 83 int i, j, ret; 84 u32 reg; 85 u32 val; 86 87 if (d->chip->runtime_pm) { 88 ret = pm_runtime_get_sync(map->dev); 89 if (ret < 0) 90 dev_err(map->dev, "IRQ sync failed to resume: %d\n", 91 ret); 92 } 93 94 if (d->clear_status) { 95 for (i = 0; i < d->chip->num_regs; i++) { 96 reg = d->get_irq_reg(d, d->chip->status_base, i); 97 98 ret = regmap_read(map, reg, &val); 99 if (ret) 100 dev_err(d->map->dev, 101 "Failed to clear the interrupt status bits\n"); 102 } 103 104 d->clear_status = false; 105 } 106 107 /* 108 * If there's been a change in the mask write it back to the 109 * hardware. We rely on the use of the regmap core cache to 110 * suppress pointless writes. 111 */ 112 for (i = 0; i < d->chip->num_regs; i++) { 113 if (d->chip->handle_mask_sync) 114 d->chip->handle_mask_sync(i, d->mask_buf_def[i], 115 d->mask_buf[i], 116 d->chip->irq_drv_data); 117 118 if (d->chip->mask_base && !d->chip->handle_mask_sync) { 119 reg = d->get_irq_reg(d, d->chip->mask_base, i); 120 ret = regmap_update_bits(d->map, reg, 121 d->mask_buf_def[i], 122 d->mask_buf[i]); 123 if (ret) 124 dev_err(d->map->dev, "Failed to sync masks in %x\n", reg); 125 } 126 127 if (d->chip->unmask_base && !d->chip->handle_mask_sync) { 128 reg = d->get_irq_reg(d, d->chip->unmask_base, i); 129 ret = regmap_update_bits(d->map, reg, 130 d->mask_buf_def[i], ~d->mask_buf[i]); 131 if (ret) 132 dev_err(d->map->dev, "Failed to sync masks in %x\n", 133 reg); 134 } 135 136 reg = d->get_irq_reg(d, d->chip->wake_base, i); 137 if (d->wake_buf) { 138 if (d->chip->wake_invert) 139 ret = regmap_update_bits(d->map, reg, 140 d->mask_buf_def[i], 141 ~d->wake_buf[i]); 142 else 143 ret = regmap_update_bits(d->map, reg, 144 d->mask_buf_def[i], 145 d->wake_buf[i]); 146 if (ret != 0) 147 dev_err(d->map->dev, 148 "Failed to sync wakes in %x: %d\n", 149 reg, ret); 150 } 151 152 if (!d->chip->init_ack_masked) 153 continue; 154 /* 155 * Ack all the masked interrupts unconditionally, 156 * OR if there is masked interrupt which hasn't been Acked, 157 * it'll be ignored in irq handler, then may introduce irq storm 158 */ 159 if (d->mask_buf[i] && (d->chip->ack_base || d->chip->use_ack)) { 160 reg = d->get_irq_reg(d, d->chip->ack_base, i); 161 162 /* some chips ack by write 0 */ 163 if (d->chip->ack_invert) 164 ret = regmap_write(map, reg, ~d->mask_buf[i]); 165 else 166 ret = regmap_write(map, reg, d->mask_buf[i]); 167 if (d->chip->clear_ack) { 168 if (d->chip->ack_invert && !ret) 169 ret = regmap_write(map, reg, UINT_MAX); 170 else if (!ret) 171 ret = regmap_write(map, reg, 0); 172 } 173 if (ret != 0) 174 dev_err(d->map->dev, "Failed to ack 0x%x: %d\n", 175 reg, ret); 176 } 177 } 178 179 for (i = 0; i < d->chip->num_config_bases; i++) { 180 for (j = 0; j < d->chip->num_config_regs; j++) { 181 reg = d->get_irq_reg(d, d->chip->config_base[i], j); 182 ret = regmap_write(map, reg, d->config_buf[i][j]); 183 if (ret) 184 dev_err(d->map->dev, 185 "Failed to write config %x: %d\n", 186 reg, ret); 187 } 188 } 189 190 if (d->chip->runtime_pm) 191 pm_runtime_put(map->dev); 192 193 /* If we've changed our wakeup count propagate it to the parent */ 194 if (d->wake_count < 0) 195 for (i = d->wake_count; i < 0; i++) 196 irq_set_irq_wake(d->irq, 0); 197 else if (d->wake_count > 0) 198 for (i = 0; i < d->wake_count; i++) 199 irq_set_irq_wake(d->irq, 1); 200 201 d->wake_count = 0; 202 203 mutex_unlock(&d->lock); 204 } 205 206 static void regmap_irq_enable(struct irq_data *data) 207 { 208 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data); 209 struct regmap *map = d->map; 210 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq); 211 unsigned int reg = irq_data->reg_offset / map->reg_stride; 212 unsigned int mask; 213 214 /* 215 * The type_in_mask flag means that the underlying hardware uses 216 * separate mask bits for each interrupt trigger type, but we want 217 * to have a single logical interrupt with a configurable type. 218 * 219 * If the interrupt we're enabling defines any supported types 220 * then instead of using the regular mask bits for this interrupt, 221 * use the value previously written to the type buffer at the 222 * corresponding offset in regmap_irq_set_type(). 223 */ 224 if (d->chip->type_in_mask && irq_data->type.types_supported) 225 mask = d->type_buf[reg] & irq_data->mask; 226 else 227 mask = irq_data->mask; 228 229 if (d->chip->clear_on_unmask) 230 d->clear_status = true; 231 232 d->mask_buf[reg] &= ~mask; 233 } 234 235 static void regmap_irq_disable(struct irq_data *data) 236 { 237 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data); 238 struct regmap *map = d->map; 239 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq); 240 241 d->mask_buf[irq_data->reg_offset / map->reg_stride] |= irq_data->mask; 242 } 243 244 static int regmap_irq_set_type(struct irq_data *data, unsigned int type) 245 { 246 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data); 247 struct regmap *map = d->map; 248 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq); 249 int reg, ret; 250 const struct regmap_irq_type *t = &irq_data->type; 251 252 if ((t->types_supported & type) != type) 253 return 0; 254 255 reg = t->type_reg_offset / map->reg_stride; 256 257 if (d->chip->type_in_mask) { 258 ret = regmap_irq_set_type_config_simple(&d->type_buf, type, 259 irq_data, reg, d->chip->irq_drv_data); 260 if (ret) 261 return ret; 262 } 263 264 if (d->chip->set_type_config) { 265 ret = d->chip->set_type_config(d->config_buf, type, irq_data, 266 reg, d->chip->irq_drv_data); 267 if (ret) 268 return ret; 269 } 270 271 return 0; 272 } 273 274 static int regmap_irq_set_wake(struct irq_data *data, unsigned int on) 275 { 276 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data); 277 struct regmap *map = d->map; 278 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq); 279 280 if (on) { 281 if (d->wake_buf) 282 d->wake_buf[irq_data->reg_offset / map->reg_stride] 283 &= ~irq_data->mask; 284 d->wake_count++; 285 } else { 286 if (d->wake_buf) 287 d->wake_buf[irq_data->reg_offset / map->reg_stride] 288 |= irq_data->mask; 289 d->wake_count--; 290 } 291 292 return 0; 293 } 294 295 static const struct irq_chip regmap_irq_chip = { 296 .irq_bus_lock = regmap_irq_lock, 297 .irq_bus_sync_unlock = regmap_irq_sync_unlock, 298 .irq_disable = regmap_irq_disable, 299 .irq_enable = regmap_irq_enable, 300 .irq_set_type = regmap_irq_set_type, 301 .irq_set_wake = regmap_irq_set_wake, 302 }; 303 304 static inline int read_sub_irq_data(struct regmap_irq_chip_data *data, 305 unsigned int b) 306 { 307 const struct regmap_irq_chip *chip = data->chip; 308 struct regmap *map = data->map; 309 struct regmap_irq_sub_irq_map *subreg; 310 unsigned int reg; 311 int i, ret = 0; 312 313 if (!chip->sub_reg_offsets) { 314 reg = data->get_irq_reg(data, chip->status_base, b); 315 ret = regmap_read(map, reg, &data->status_buf[b]); 316 } else { 317 /* 318 * Note we can't use ->get_irq_reg() here because the offsets 319 * in 'subreg' are *not* interchangeable with indices. 320 */ 321 subreg = &chip->sub_reg_offsets[b]; 322 for (i = 0; i < subreg->num_regs; i++) { 323 unsigned int offset = subreg->offset[i]; 324 unsigned int index = offset / map->reg_stride; 325 326 ret = regmap_read(map, chip->status_base + offset, 327 &data->status_buf[index]); 328 if (ret) 329 break; 330 } 331 } 332 return ret; 333 } 334 335 static irqreturn_t regmap_irq_thread(int irq, void *d) 336 { 337 struct regmap_irq_chip_data *data = d; 338 const struct regmap_irq_chip *chip = data->chip; 339 struct regmap *map = data->map; 340 int ret, i; 341 bool handled = false; 342 u32 reg; 343 344 if (chip->handle_pre_irq) 345 chip->handle_pre_irq(chip->irq_drv_data); 346 347 if (chip->runtime_pm) { 348 ret = pm_runtime_get_sync(map->dev); 349 if (ret < 0) { 350 dev_err(map->dev, "IRQ thread failed to resume: %d\n", 351 ret); 352 goto exit; 353 } 354 } 355 356 /* 357 * Read only registers with active IRQs if the chip has 'main status 358 * register'. Else read in the statuses, using a single bulk read if 359 * possible in order to reduce the I/O overheads. 360 */ 361 362 if (chip->no_status) { 363 /* no status register so default to all active */ 364 memset32(data->status_buf, GENMASK(31, 0), chip->num_regs); 365 } else if (chip->num_main_regs) { 366 unsigned int max_main_bits; 367 unsigned long size; 368 369 size = chip->num_regs * sizeof(unsigned int); 370 371 max_main_bits = (chip->num_main_status_bits) ? 372 chip->num_main_status_bits : chip->num_regs; 373 /* Clear the status buf as we don't read all status regs */ 374 memset(data->status_buf, 0, size); 375 376 /* We could support bulk read for main status registers 377 * but I don't expect to see devices with really many main 378 * status registers so let's only support single reads for the 379 * sake of simplicity. and add bulk reads only if needed 380 */ 381 for (i = 0; i < chip->num_main_regs; i++) { 382 reg = data->get_irq_reg(data, chip->main_status, i); 383 ret = regmap_read(map, reg, &data->main_status_buf[i]); 384 if (ret) { 385 dev_err(map->dev, 386 "Failed to read IRQ status %d\n", 387 ret); 388 goto exit; 389 } 390 } 391 392 /* Read sub registers with active IRQs */ 393 for (i = 0; i < chip->num_main_regs; i++) { 394 unsigned int b; 395 const unsigned long mreg = data->main_status_buf[i]; 396 397 for_each_set_bit(b, &mreg, map->format.val_bytes * 8) { 398 if (i * map->format.val_bytes * 8 + b > 399 max_main_bits) 400 break; 401 ret = read_sub_irq_data(data, b); 402 403 if (ret != 0) { 404 dev_err(map->dev, 405 "Failed to read IRQ status %d\n", 406 ret); 407 goto exit; 408 } 409 } 410 411 } 412 } else if (regmap_irq_can_bulk_read_status(data)) { 413 414 u8 *buf8 = data->status_reg_buf; 415 u16 *buf16 = data->status_reg_buf; 416 u32 *buf32 = data->status_reg_buf; 417 418 BUG_ON(!data->status_reg_buf); 419 420 ret = regmap_bulk_read(map, chip->status_base, 421 data->status_reg_buf, 422 chip->num_regs); 423 if (ret != 0) { 424 dev_err(map->dev, "Failed to read IRQ status: %d\n", 425 ret); 426 goto exit; 427 } 428 429 for (i = 0; i < data->chip->num_regs; i++) { 430 switch (map->format.val_bytes) { 431 case 1: 432 data->status_buf[i] = buf8[i]; 433 break; 434 case 2: 435 data->status_buf[i] = buf16[i]; 436 break; 437 case 4: 438 data->status_buf[i] = buf32[i]; 439 break; 440 default: 441 BUG(); 442 goto exit; 443 } 444 } 445 446 } else { 447 for (i = 0; i < data->chip->num_regs; i++) { 448 unsigned int reg = data->get_irq_reg(data, 449 data->chip->status_base, i); 450 ret = regmap_read(map, reg, &data->status_buf[i]); 451 452 if (ret != 0) { 453 dev_err(map->dev, 454 "Failed to read IRQ status: %d\n", 455 ret); 456 goto exit; 457 } 458 } 459 } 460 461 if (chip->status_invert) 462 for (i = 0; i < data->chip->num_regs; i++) 463 data->status_buf[i] = ~data->status_buf[i]; 464 465 /* 466 * Ignore masked IRQs and ack if we need to; we ack early so 467 * there is no race between handling and acknowledging the 468 * interrupt. We assume that typically few of the interrupts 469 * will fire simultaneously so don't worry about overhead from 470 * doing a write per register. 471 */ 472 for (i = 0; i < data->chip->num_regs; i++) { 473 data->status_buf[i] &= ~data->mask_buf[i]; 474 475 if (data->status_buf[i] && (chip->ack_base || chip->use_ack)) { 476 reg = data->get_irq_reg(data, data->chip->ack_base, i); 477 478 if (chip->ack_invert) 479 ret = regmap_write(map, reg, 480 ~data->status_buf[i]); 481 else 482 ret = regmap_write(map, reg, 483 data->status_buf[i]); 484 if (chip->clear_ack) { 485 if (chip->ack_invert && !ret) 486 ret = regmap_write(map, reg, UINT_MAX); 487 else if (!ret) 488 ret = regmap_write(map, reg, 0); 489 } 490 if (ret != 0) 491 dev_err(map->dev, "Failed to ack 0x%x: %d\n", 492 reg, ret); 493 } 494 } 495 496 for (i = 0; i < chip->num_irqs; i++) { 497 if (data->status_buf[chip->irqs[i].reg_offset / 498 map->reg_stride] & chip->irqs[i].mask) { 499 handle_nested_irq(irq_find_mapping(data->domain, i)); 500 handled = true; 501 } 502 } 503 504 exit: 505 if (chip->handle_post_irq) 506 chip->handle_post_irq(chip->irq_drv_data); 507 508 if (chip->runtime_pm) 509 pm_runtime_put(map->dev); 510 511 if (handled) 512 return IRQ_HANDLED; 513 else 514 return IRQ_NONE; 515 } 516 517 static struct lock_class_key regmap_irq_lock_class; 518 static struct lock_class_key regmap_irq_request_class; 519 520 static int regmap_irq_map(struct irq_domain *h, unsigned int virq, 521 irq_hw_number_t hw) 522 { 523 struct regmap_irq_chip_data *data = h->host_data; 524 525 irq_set_chip_data(virq, data); 526 irq_set_lockdep_class(virq, ®map_irq_lock_class, ®map_irq_request_class); 527 irq_set_chip(virq, &data->irq_chip); 528 irq_set_nested_thread(virq, 1); 529 irq_set_parent(virq, data->irq); 530 irq_set_noprobe(virq); 531 532 return 0; 533 } 534 535 static const struct irq_domain_ops regmap_domain_ops = { 536 .map = regmap_irq_map, 537 .xlate = irq_domain_xlate_onetwocell, 538 }; 539 540 /** 541 * regmap_irq_get_irq_reg_linear() - Linear IRQ register mapping callback. 542 * @data: Data for the &struct regmap_irq_chip 543 * @base: Base register 544 * @index: Register index 545 * 546 * Returns the register address corresponding to the given @base and @index 547 * by the formula ``base + index * regmap_stride * irq_reg_stride``. 548 */ 549 unsigned int regmap_irq_get_irq_reg_linear(struct regmap_irq_chip_data *data, 550 unsigned int base, int index) 551 { 552 struct regmap *map = data->map; 553 554 return base + index * map->reg_stride * data->irq_reg_stride; 555 } 556 EXPORT_SYMBOL_GPL(regmap_irq_get_irq_reg_linear); 557 558 /** 559 * regmap_irq_set_type_config_simple() - Simple IRQ type configuration callback. 560 * @buf: Buffer containing configuration register values, this is a 2D array of 561 * `num_config_bases` rows, each of `num_config_regs` elements. 562 * @type: The requested IRQ type. 563 * @irq_data: The IRQ being configured. 564 * @idx: Index of the irq's config registers within each array `buf[i]` 565 * @irq_drv_data: Driver specific IRQ data 566 * 567 * This is a &struct regmap_irq_chip->set_type_config callback suitable for 568 * chips with one config register. Register values are updated according to 569 * the &struct regmap_irq_type data associated with an IRQ. 570 */ 571 int regmap_irq_set_type_config_simple(unsigned int **buf, unsigned int type, 572 const struct regmap_irq *irq_data, 573 int idx, void *irq_drv_data) 574 { 575 const struct regmap_irq_type *t = &irq_data->type; 576 577 if (t->type_reg_mask) 578 buf[0][idx] &= ~t->type_reg_mask; 579 else 580 buf[0][idx] &= ~(t->type_falling_val | 581 t->type_rising_val | 582 t->type_level_low_val | 583 t->type_level_high_val); 584 585 switch (type) { 586 case IRQ_TYPE_EDGE_FALLING: 587 buf[0][idx] |= t->type_falling_val; 588 break; 589 590 case IRQ_TYPE_EDGE_RISING: 591 buf[0][idx] |= t->type_rising_val; 592 break; 593 594 case IRQ_TYPE_EDGE_BOTH: 595 buf[0][idx] |= (t->type_falling_val | 596 t->type_rising_val); 597 break; 598 599 case IRQ_TYPE_LEVEL_HIGH: 600 buf[0][idx] |= t->type_level_high_val; 601 break; 602 603 case IRQ_TYPE_LEVEL_LOW: 604 buf[0][idx] |= t->type_level_low_val; 605 break; 606 607 default: 608 return -EINVAL; 609 } 610 611 return 0; 612 } 613 EXPORT_SYMBOL_GPL(regmap_irq_set_type_config_simple); 614 615 /** 616 * regmap_add_irq_chip_fwnode() - Use standard regmap IRQ controller handling 617 * 618 * @fwnode: The firmware node where the IRQ domain should be added to. 619 * @map: The regmap for the device. 620 * @irq: The IRQ the device uses to signal interrupts. 621 * @irq_flags: The IRQF_ flags to use for the primary interrupt. 622 * @irq_base: Allocate at specific IRQ number if irq_base > 0. 623 * @chip: Configuration for the interrupt controller. 624 * @data: Runtime data structure for the controller, allocated on success. 625 * 626 * Returns 0 on success or an errno on failure. 627 * 628 * In order for this to be efficient the chip really should use a 629 * register cache. The chip driver is responsible for restoring the 630 * register values used by the IRQ controller over suspend and resume. 631 */ 632 int regmap_add_irq_chip_fwnode(struct fwnode_handle *fwnode, 633 struct regmap *map, int irq, 634 int irq_flags, int irq_base, 635 const struct regmap_irq_chip *chip, 636 struct regmap_irq_chip_data **data) 637 { 638 struct regmap_irq_chip_data *d; 639 int i; 640 int ret = -ENOMEM; 641 u32 reg; 642 643 if (chip->num_regs <= 0) 644 return -EINVAL; 645 646 if (chip->clear_on_unmask && (chip->ack_base || chip->use_ack)) 647 return -EINVAL; 648 649 if (chip->mask_base && chip->unmask_base && !chip->mask_unmask_non_inverted) 650 return -EINVAL; 651 652 for (i = 0; i < chip->num_irqs; i++) { 653 if (chip->irqs[i].reg_offset % map->reg_stride) 654 return -EINVAL; 655 if (chip->irqs[i].reg_offset / map->reg_stride >= 656 chip->num_regs) 657 return -EINVAL; 658 } 659 660 if (irq_base) { 661 irq_base = irq_alloc_descs(irq_base, 0, chip->num_irqs, 0); 662 if (irq_base < 0) { 663 dev_warn(map->dev, "Failed to allocate IRQs: %d\n", 664 irq_base); 665 return irq_base; 666 } 667 } 668 669 d = kzalloc(sizeof(*d), GFP_KERNEL); 670 if (!d) 671 return -ENOMEM; 672 673 if (chip->num_main_regs) { 674 d->main_status_buf = kcalloc(chip->num_main_regs, 675 sizeof(*d->main_status_buf), 676 GFP_KERNEL); 677 678 if (!d->main_status_buf) 679 goto err_alloc; 680 } 681 682 d->status_buf = kcalloc(chip->num_regs, sizeof(*d->status_buf), 683 GFP_KERNEL); 684 if (!d->status_buf) 685 goto err_alloc; 686 687 d->mask_buf = kcalloc(chip->num_regs, sizeof(*d->mask_buf), 688 GFP_KERNEL); 689 if (!d->mask_buf) 690 goto err_alloc; 691 692 d->mask_buf_def = kcalloc(chip->num_regs, sizeof(*d->mask_buf_def), 693 GFP_KERNEL); 694 if (!d->mask_buf_def) 695 goto err_alloc; 696 697 if (chip->wake_base) { 698 d->wake_buf = kcalloc(chip->num_regs, sizeof(*d->wake_buf), 699 GFP_KERNEL); 700 if (!d->wake_buf) 701 goto err_alloc; 702 } 703 704 if (chip->type_in_mask) { 705 d->type_buf_def = kcalloc(chip->num_regs, 706 sizeof(*d->type_buf_def), GFP_KERNEL); 707 if (!d->type_buf_def) 708 goto err_alloc; 709 710 d->type_buf = kcalloc(chip->num_regs, sizeof(*d->type_buf), GFP_KERNEL); 711 if (!d->type_buf) 712 goto err_alloc; 713 } 714 715 if (chip->num_config_bases && chip->num_config_regs) { 716 /* 717 * Create config_buf[num_config_bases][num_config_regs] 718 */ 719 d->config_buf = kcalloc(chip->num_config_bases, 720 sizeof(*d->config_buf), GFP_KERNEL); 721 if (!d->config_buf) 722 goto err_alloc; 723 724 for (i = 0; i < chip->num_config_bases; i++) { 725 d->config_buf[i] = kcalloc(chip->num_config_regs, 726 sizeof(**d->config_buf), 727 GFP_KERNEL); 728 if (!d->config_buf[i]) 729 goto err_alloc; 730 } 731 } 732 733 d->irq_chip = regmap_irq_chip; 734 d->irq_chip.name = chip->name; 735 d->irq = irq; 736 d->map = map; 737 d->chip = chip; 738 d->irq_base = irq_base; 739 740 if (chip->irq_reg_stride) 741 d->irq_reg_stride = chip->irq_reg_stride; 742 else 743 d->irq_reg_stride = 1; 744 745 if (chip->get_irq_reg) 746 d->get_irq_reg = chip->get_irq_reg; 747 else 748 d->get_irq_reg = regmap_irq_get_irq_reg_linear; 749 750 if (regmap_irq_can_bulk_read_status(d)) { 751 d->status_reg_buf = kmalloc_array(chip->num_regs, 752 map->format.val_bytes, 753 GFP_KERNEL); 754 if (!d->status_reg_buf) 755 goto err_alloc; 756 } 757 758 mutex_init(&d->lock); 759 760 for (i = 0; i < chip->num_irqs; i++) 761 d->mask_buf_def[chip->irqs[i].reg_offset / map->reg_stride] 762 |= chip->irqs[i].mask; 763 764 /* Mask all the interrupts by default */ 765 for (i = 0; i < chip->num_regs; i++) { 766 d->mask_buf[i] = d->mask_buf_def[i]; 767 768 if (chip->handle_mask_sync) { 769 ret = chip->handle_mask_sync(i, d->mask_buf_def[i], 770 d->mask_buf[i], 771 chip->irq_drv_data); 772 if (ret) 773 goto err_alloc; 774 } 775 776 if (chip->mask_base && !chip->handle_mask_sync) { 777 reg = d->get_irq_reg(d, chip->mask_base, i); 778 ret = regmap_update_bits(d->map, reg, 779 d->mask_buf_def[i], 780 d->mask_buf[i]); 781 if (ret) { 782 dev_err(map->dev, "Failed to set masks in 0x%x: %d\n", 783 reg, ret); 784 goto err_alloc; 785 } 786 } 787 788 if (chip->unmask_base && !chip->handle_mask_sync) { 789 reg = d->get_irq_reg(d, chip->unmask_base, i); 790 ret = regmap_update_bits(d->map, reg, 791 d->mask_buf_def[i], ~d->mask_buf[i]); 792 if (ret) { 793 dev_err(map->dev, "Failed to set masks in 0x%x: %d\n", 794 reg, ret); 795 goto err_alloc; 796 } 797 } 798 799 if (!chip->init_ack_masked) 800 continue; 801 802 /* Ack masked but set interrupts */ 803 if (d->chip->no_status) { 804 /* no status register so default to all active */ 805 d->status_buf[i] = GENMASK(31, 0); 806 } else { 807 reg = d->get_irq_reg(d, d->chip->status_base, i); 808 ret = regmap_read(map, reg, &d->status_buf[i]); 809 if (ret != 0) { 810 dev_err(map->dev, "Failed to read IRQ status: %d\n", 811 ret); 812 goto err_alloc; 813 } 814 } 815 816 if (chip->status_invert) 817 d->status_buf[i] = ~d->status_buf[i]; 818 819 if (d->status_buf[i] && (chip->ack_base || chip->use_ack)) { 820 reg = d->get_irq_reg(d, d->chip->ack_base, i); 821 if (chip->ack_invert) 822 ret = regmap_write(map, reg, 823 ~(d->status_buf[i] & d->mask_buf[i])); 824 else 825 ret = regmap_write(map, reg, 826 d->status_buf[i] & d->mask_buf[i]); 827 if (chip->clear_ack) { 828 if (chip->ack_invert && !ret) 829 ret = regmap_write(map, reg, UINT_MAX); 830 else if (!ret) 831 ret = regmap_write(map, reg, 0); 832 } 833 if (ret != 0) { 834 dev_err(map->dev, "Failed to ack 0x%x: %d\n", 835 reg, ret); 836 goto err_alloc; 837 } 838 } 839 } 840 841 /* Wake is disabled by default */ 842 if (d->wake_buf) { 843 for (i = 0; i < chip->num_regs; i++) { 844 d->wake_buf[i] = d->mask_buf_def[i]; 845 reg = d->get_irq_reg(d, d->chip->wake_base, i); 846 847 if (chip->wake_invert) 848 ret = regmap_update_bits(d->map, reg, 849 d->mask_buf_def[i], 850 0); 851 else 852 ret = regmap_update_bits(d->map, reg, 853 d->mask_buf_def[i], 854 d->wake_buf[i]); 855 if (ret != 0) { 856 dev_err(map->dev, "Failed to set masks in 0x%x: %d\n", 857 reg, ret); 858 goto err_alloc; 859 } 860 } 861 } 862 863 if (irq_base) 864 d->domain = irq_domain_create_legacy(fwnode, chip->num_irqs, 865 irq_base, 0, 866 ®map_domain_ops, d); 867 else 868 d->domain = irq_domain_create_linear(fwnode, chip->num_irqs, 869 ®map_domain_ops, d); 870 if (!d->domain) { 871 dev_err(map->dev, "Failed to create IRQ domain\n"); 872 ret = -ENOMEM; 873 goto err_alloc; 874 } 875 876 ret = request_threaded_irq(irq, NULL, regmap_irq_thread, 877 irq_flags | IRQF_ONESHOT, 878 chip->name, d); 879 if (ret != 0) { 880 dev_err(map->dev, "Failed to request IRQ %d for %s: %d\n", 881 irq, chip->name, ret); 882 goto err_domain; 883 } 884 885 *data = d; 886 887 return 0; 888 889 err_domain: 890 /* Should really dispose of the domain but... */ 891 err_alloc: 892 kfree(d->type_buf); 893 kfree(d->type_buf_def); 894 kfree(d->wake_buf); 895 kfree(d->mask_buf_def); 896 kfree(d->mask_buf); 897 kfree(d->status_buf); 898 kfree(d->status_reg_buf); 899 if (d->config_buf) { 900 for (i = 0; i < chip->num_config_bases; i++) 901 kfree(d->config_buf[i]); 902 kfree(d->config_buf); 903 } 904 kfree(d); 905 return ret; 906 } 907 EXPORT_SYMBOL_GPL(regmap_add_irq_chip_fwnode); 908 909 /** 910 * regmap_add_irq_chip() - Use standard regmap IRQ controller handling 911 * 912 * @map: The regmap for the device. 913 * @irq: The IRQ the device uses to signal interrupts. 914 * @irq_flags: The IRQF_ flags to use for the primary interrupt. 915 * @irq_base: Allocate at specific IRQ number if irq_base > 0. 916 * @chip: Configuration for the interrupt controller. 917 * @data: Runtime data structure for the controller, allocated on success. 918 * 919 * Returns 0 on success or an errno on failure. 920 * 921 * This is the same as regmap_add_irq_chip_fwnode, except that the firmware 922 * node of the regmap is used. 923 */ 924 int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags, 925 int irq_base, const struct regmap_irq_chip *chip, 926 struct regmap_irq_chip_data **data) 927 { 928 return regmap_add_irq_chip_fwnode(dev_fwnode(map->dev), map, irq, 929 irq_flags, irq_base, chip, data); 930 } 931 EXPORT_SYMBOL_GPL(regmap_add_irq_chip); 932 933 /** 934 * regmap_del_irq_chip() - Stop interrupt handling for a regmap IRQ chip 935 * 936 * @irq: Primary IRQ for the device 937 * @d: ®map_irq_chip_data allocated by regmap_add_irq_chip() 938 * 939 * This function also disposes of all mapped IRQs on the chip. 940 */ 941 void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *d) 942 { 943 unsigned int virq; 944 int i, hwirq; 945 946 if (!d) 947 return; 948 949 free_irq(irq, d); 950 951 /* Dispose all virtual irq from irq domain before removing it */ 952 for (hwirq = 0; hwirq < d->chip->num_irqs; hwirq++) { 953 /* Ignore hwirq if holes in the IRQ list */ 954 if (!d->chip->irqs[hwirq].mask) 955 continue; 956 957 /* 958 * Find the virtual irq of hwirq on chip and if it is 959 * there then dispose it 960 */ 961 virq = irq_find_mapping(d->domain, hwirq); 962 if (virq) 963 irq_dispose_mapping(virq); 964 } 965 966 irq_domain_remove(d->domain); 967 kfree(d->type_buf); 968 kfree(d->type_buf_def); 969 kfree(d->wake_buf); 970 kfree(d->mask_buf_def); 971 kfree(d->mask_buf); 972 kfree(d->status_reg_buf); 973 kfree(d->status_buf); 974 if (d->config_buf) { 975 for (i = 0; i < d->chip->num_config_bases; i++) 976 kfree(d->config_buf[i]); 977 kfree(d->config_buf); 978 } 979 kfree(d); 980 } 981 EXPORT_SYMBOL_GPL(regmap_del_irq_chip); 982 983 static void devm_regmap_irq_chip_release(struct device *dev, void *res) 984 { 985 struct regmap_irq_chip_data *d = *(struct regmap_irq_chip_data **)res; 986 987 regmap_del_irq_chip(d->irq, d); 988 } 989 990 static int devm_regmap_irq_chip_match(struct device *dev, void *res, void *data) 991 992 { 993 struct regmap_irq_chip_data **r = res; 994 995 if (!r || !*r) { 996 WARN_ON(!r || !*r); 997 return 0; 998 } 999 return *r == data; 1000 } 1001 1002 /** 1003 * devm_regmap_add_irq_chip_fwnode() - Resource managed regmap_add_irq_chip_fwnode() 1004 * 1005 * @dev: The device pointer on which irq_chip belongs to. 1006 * @fwnode: The firmware node where the IRQ domain should be added to. 1007 * @map: The regmap for the device. 1008 * @irq: The IRQ the device uses to signal interrupts 1009 * @irq_flags: The IRQF_ flags to use for the primary interrupt. 1010 * @irq_base: Allocate at specific IRQ number if irq_base > 0. 1011 * @chip: Configuration for the interrupt controller. 1012 * @data: Runtime data structure for the controller, allocated on success 1013 * 1014 * Returns 0 on success or an errno on failure. 1015 * 1016 * The ®map_irq_chip_data will be automatically released when the device is 1017 * unbound. 1018 */ 1019 int devm_regmap_add_irq_chip_fwnode(struct device *dev, 1020 struct fwnode_handle *fwnode, 1021 struct regmap *map, int irq, 1022 int irq_flags, int irq_base, 1023 const struct regmap_irq_chip *chip, 1024 struct regmap_irq_chip_data **data) 1025 { 1026 struct regmap_irq_chip_data **ptr, *d; 1027 int ret; 1028 1029 ptr = devres_alloc(devm_regmap_irq_chip_release, sizeof(*ptr), 1030 GFP_KERNEL); 1031 if (!ptr) 1032 return -ENOMEM; 1033 1034 ret = regmap_add_irq_chip_fwnode(fwnode, map, irq, irq_flags, irq_base, 1035 chip, &d); 1036 if (ret < 0) { 1037 devres_free(ptr); 1038 return ret; 1039 } 1040 1041 *ptr = d; 1042 devres_add(dev, ptr); 1043 *data = d; 1044 return 0; 1045 } 1046 EXPORT_SYMBOL_GPL(devm_regmap_add_irq_chip_fwnode); 1047 1048 /** 1049 * devm_regmap_add_irq_chip() - Resource managed regmap_add_irq_chip() 1050 * 1051 * @dev: The device pointer on which irq_chip belongs to. 1052 * @map: The regmap for the device. 1053 * @irq: The IRQ the device uses to signal interrupts 1054 * @irq_flags: The IRQF_ flags to use for the primary interrupt. 1055 * @irq_base: Allocate at specific IRQ number if irq_base > 0. 1056 * @chip: Configuration for the interrupt controller. 1057 * @data: Runtime data structure for the controller, allocated on success 1058 * 1059 * Returns 0 on success or an errno on failure. 1060 * 1061 * The ®map_irq_chip_data will be automatically released when the device is 1062 * unbound. 1063 */ 1064 int devm_regmap_add_irq_chip(struct device *dev, struct regmap *map, int irq, 1065 int irq_flags, int irq_base, 1066 const struct regmap_irq_chip *chip, 1067 struct regmap_irq_chip_data **data) 1068 { 1069 return devm_regmap_add_irq_chip_fwnode(dev, dev_fwnode(map->dev), map, 1070 irq, irq_flags, irq_base, chip, 1071 data); 1072 } 1073 EXPORT_SYMBOL_GPL(devm_regmap_add_irq_chip); 1074 1075 /** 1076 * devm_regmap_del_irq_chip() - Resource managed regmap_del_irq_chip() 1077 * 1078 * @dev: Device for which the resource was allocated. 1079 * @irq: Primary IRQ for the device. 1080 * @data: ®map_irq_chip_data allocated by regmap_add_irq_chip(). 1081 * 1082 * A resource managed version of regmap_del_irq_chip(). 1083 */ 1084 void devm_regmap_del_irq_chip(struct device *dev, int irq, 1085 struct regmap_irq_chip_data *data) 1086 { 1087 int rc; 1088 1089 WARN_ON(irq != data->irq); 1090 rc = devres_release(dev, devm_regmap_irq_chip_release, 1091 devm_regmap_irq_chip_match, data); 1092 1093 if (rc != 0) 1094 WARN_ON(rc); 1095 } 1096 EXPORT_SYMBOL_GPL(devm_regmap_del_irq_chip); 1097 1098 /** 1099 * regmap_irq_chip_get_base() - Retrieve interrupt base for a regmap IRQ chip 1100 * 1101 * @data: regmap irq controller to operate on. 1102 * 1103 * Useful for drivers to request their own IRQs. 1104 */ 1105 int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data) 1106 { 1107 WARN_ON(!data->irq_base); 1108 return data->irq_base; 1109 } 1110 EXPORT_SYMBOL_GPL(regmap_irq_chip_get_base); 1111 1112 /** 1113 * regmap_irq_get_virq() - Map an interrupt on a chip to a virtual IRQ 1114 * 1115 * @data: regmap irq controller to operate on. 1116 * @irq: index of the interrupt requested in the chip IRQs. 1117 * 1118 * Useful for drivers to request their own IRQs. 1119 */ 1120 int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq) 1121 { 1122 /* Handle holes in the IRQ list */ 1123 if (!data->chip->irqs[irq].mask) 1124 return -EINVAL; 1125 1126 return irq_create_mapping(data->domain, irq); 1127 } 1128 EXPORT_SYMBOL_GPL(regmap_irq_get_virq); 1129 1130 /** 1131 * regmap_irq_get_domain() - Retrieve the irq_domain for the chip 1132 * 1133 * @data: regmap_irq controller to operate on. 1134 * 1135 * Useful for drivers to request their own IRQs and for integration 1136 * with subsystems. For ease of integration NULL is accepted as a 1137 * domain, allowing devices to just call this even if no domain is 1138 * allocated. 1139 */ 1140 struct irq_domain *regmap_irq_get_domain(struct regmap_irq_chip_data *data) 1141 { 1142 if (data) 1143 return data->domain; 1144 else 1145 return NULL; 1146 } 1147 EXPORT_SYMBOL_GPL(regmap_irq_get_domain); 1148