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