Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

regmap-irq cleanups and refactoring

Merge series from Aidan MacDonald <aidanmacdonald.0x0@gmail.com>:

This series is an attempt at cleaning up the regmap-irq API in order
to simplify things and consolidate existing features, while at the
same time generalizing it to support a wider range of hardware.

There is a new system for IRQ type configuration, some tweaks to
unmask registers so they're more intuitive and useful, and a new
callback for calculating register addresses. There's also a few
minor code cleanups in here.

In v2 I've taken the approach of adding new features and deprecating
existing ones rather than removing them aggressively. Warnings will
be issued for any drivers that use deprecated features, but they'll
otherwise continue to function normally.

One important caveat: not all of these changes are tested beyond
compile testing, since I don't have hardware to exercise all of
the features.

+369 -171
+297 -139
drivers/base/regmap/regmap-irq.c
··· 30 30 int irq; 31 31 int wake_count; 32 32 33 + unsigned int mask_base; 34 + unsigned int unmask_base; 35 + 33 36 void *status_reg_buf; 34 37 unsigned int *main_status_buf; 35 38 unsigned int *status_buf; ··· 42 39 unsigned int *type_buf; 43 40 unsigned int *type_buf_def; 44 41 unsigned int **virt_buf; 42 + unsigned int **config_buf; 45 43 46 44 unsigned int irq_reg_stride; 47 - unsigned int type_reg_stride; 48 45 49 - bool clear_status:1; 46 + unsigned int (*get_irq_reg)(struct regmap_irq_chip_data *data, 47 + unsigned int base, int index); 48 + 49 + unsigned int clear_status:1; 50 50 }; 51 - 52 - static int sub_irq_reg(struct regmap_irq_chip_data *data, 53 - unsigned int base_reg, int i) 54 - { 55 - const struct regmap_irq_chip *chip = data->chip; 56 - struct regmap *map = data->map; 57 - struct regmap_irq_sub_irq_map *subreg; 58 - unsigned int offset; 59 - int reg = 0; 60 - 61 - if (!chip->sub_reg_offsets || !chip->not_fixed_stride) { 62 - /* Assume linear mapping */ 63 - reg = base_reg + (i * map->reg_stride * data->irq_reg_stride); 64 - } else { 65 - subreg = &chip->sub_reg_offsets[i]; 66 - offset = subreg->offset[0]; 67 - reg = base_reg + offset; 68 - } 69 - 70 - return reg; 71 - } 72 51 73 52 static inline const 74 53 struct regmap_irq *irq_to_regmap_irq(struct regmap_irq_chip_data *data, 75 54 int irq) 76 55 { 77 56 return &data->chip->irqs[irq]; 57 + } 58 + 59 + static bool regmap_irq_can_bulk_read_status(struct regmap_irq_chip_data *data) 60 + { 61 + struct regmap *map = data->map; 62 + 63 + /* 64 + * While possible that a user-defined ->get_irq_reg() callback might 65 + * be linear enough to support bulk reads, most of the time it won't. 66 + * Therefore only allow them if the default callback is being used. 67 + */ 68 + return data->irq_reg_stride == 1 && map->reg_stride == 1 && 69 + data->get_irq_reg == regmap_irq_get_irq_reg_linear && 70 + !map->use_single_read; 78 71 } 79 72 80 73 static void regmap_irq_lock(struct irq_data *data) ··· 80 81 mutex_lock(&d->lock); 81 82 } 82 83 83 - static int regmap_irq_update_bits(struct regmap_irq_chip_data *d, 84 - unsigned int reg, unsigned int mask, 85 - unsigned int val) 86 - { 87 - if (d->chip->mask_writeonly) 88 - return regmap_write_bits(d->map, reg, mask, val); 89 - else 90 - return regmap_update_bits(d->map, reg, mask, val); 91 - } 92 - 93 84 static void regmap_irq_sync_unlock(struct irq_data *data) 94 85 { 95 86 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data); 96 87 struct regmap *map = d->map; 97 88 int i, j, ret; 98 89 u32 reg; 99 - u32 unmask_offset; 100 90 u32 val; 101 91 102 92 if (d->chip->runtime_pm) { ··· 97 109 98 110 if (d->clear_status) { 99 111 for (i = 0; i < d->chip->num_regs; i++) { 100 - reg = sub_irq_reg(d, d->chip->status_base, i); 112 + reg = d->get_irq_reg(d, d->chip->status_base, i); 101 113 102 114 ret = regmap_read(map, reg, &val); 103 115 if (ret) ··· 114 126 * suppress pointless writes. 115 127 */ 116 128 for (i = 0; i < d->chip->num_regs; i++) { 117 - if (!d->chip->mask_base) 118 - continue; 119 - 120 - reg = sub_irq_reg(d, d->chip->mask_base, i); 121 - if (d->chip->mask_invert) { 122 - ret = regmap_irq_update_bits(d, reg, 123 - d->mask_buf_def[i], ~d->mask_buf[i]); 124 - } else if (d->chip->unmask_base) { 125 - /* set mask with mask_base register */ 126 - ret = regmap_irq_update_bits(d, reg, 127 - d->mask_buf_def[i], ~d->mask_buf[i]); 128 - if (ret < 0) 129 - dev_err(d->map->dev, 130 - "Failed to sync unmasks in %x\n", 129 + if (d->mask_base) { 130 + reg = d->get_irq_reg(d, d->mask_base, i); 131 + ret = regmap_update_bits(d->map, reg, 132 + d->mask_buf_def[i], d->mask_buf[i]); 133 + if (ret) 134 + dev_err(d->map->dev, "Failed to sync masks in %x\n", 131 135 reg); 132 - unmask_offset = d->chip->unmask_base - 133 - d->chip->mask_base; 134 - /* clear mask with unmask_base register */ 135 - ret = regmap_irq_update_bits(d, 136 - reg + unmask_offset, 137 - d->mask_buf_def[i], 138 - d->mask_buf[i]); 139 - } else { 140 - ret = regmap_irq_update_bits(d, reg, 141 - d->mask_buf_def[i], d->mask_buf[i]); 142 136 } 143 - if (ret != 0) 144 - dev_err(d->map->dev, "Failed to sync masks in %x\n", 145 - reg); 146 137 147 - reg = sub_irq_reg(d, d->chip->wake_base, i); 138 + if (d->unmask_base) { 139 + reg = d->get_irq_reg(d, d->unmask_base, i); 140 + ret = regmap_update_bits(d->map, reg, 141 + d->mask_buf_def[i], ~d->mask_buf[i]); 142 + if (ret) 143 + dev_err(d->map->dev, "Failed to sync masks in %x\n", 144 + reg); 145 + } 146 + 147 + reg = d->get_irq_reg(d, d->chip->wake_base, i); 148 148 if (d->wake_buf) { 149 149 if (d->chip->wake_invert) 150 - ret = regmap_irq_update_bits(d, reg, 150 + ret = regmap_update_bits(d->map, reg, 151 151 d->mask_buf_def[i], 152 152 ~d->wake_buf[i]); 153 153 else 154 - ret = regmap_irq_update_bits(d, reg, 154 + ret = regmap_update_bits(d->map, reg, 155 155 d->mask_buf_def[i], 156 156 d->wake_buf[i]); 157 157 if (ret != 0) ··· 156 180 * it'll be ignored in irq handler, then may introduce irq storm 157 181 */ 158 182 if (d->mask_buf[i] && (d->chip->ack_base || d->chip->use_ack)) { 159 - reg = sub_irq_reg(d, d->chip->ack_base, i); 183 + reg = d->get_irq_reg(d, d->chip->ack_base, i); 160 184 161 185 /* some chips ack by write 0 */ 162 186 if (d->chip->ack_invert) ··· 180 204 for (i = 0; i < d->chip->num_type_reg; i++) { 181 205 if (!d->type_buf_def[i]) 182 206 continue; 183 - reg = sub_irq_reg(d, d->chip->type_base, i); 207 + reg = d->get_irq_reg(d, d->chip->type_base, i); 184 208 if (d->chip->type_invert) 185 - ret = regmap_irq_update_bits(d, reg, 209 + ret = regmap_update_bits(d->map, reg, 186 210 d->type_buf_def[i], ~d->type_buf[i]); 187 211 else 188 - ret = regmap_irq_update_bits(d, reg, 212 + ret = regmap_update_bits(d->map, reg, 189 213 d->type_buf_def[i], d->type_buf[i]); 190 214 if (ret != 0) 191 215 dev_err(d->map->dev, "Failed to sync type in %x\n", ··· 196 220 if (d->chip->num_virt_regs) { 197 221 for (i = 0; i < d->chip->num_virt_regs; i++) { 198 222 for (j = 0; j < d->chip->num_regs; j++) { 199 - reg = sub_irq_reg(d, d->chip->virt_reg_base[i], 200 - j); 223 + reg = d->get_irq_reg(d, d->chip->virt_reg_base[i], 224 + j); 201 225 ret = regmap_write(map, reg, d->virt_buf[i][j]); 202 226 if (ret != 0) 203 227 dev_err(d->map->dev, 204 228 "Failed to write virt 0x%x: %d\n", 205 229 reg, ret); 206 230 } 231 + } 232 + } 233 + 234 + for (i = 0; i < d->chip->num_config_bases; i++) { 235 + for (j = 0; j < d->chip->num_config_regs; j++) { 236 + reg = d->get_irq_reg(d, d->chip->config_base[i], j); 237 + ret = regmap_write(map, reg, d->config_buf[i][j]); 238 + if (ret) 239 + dev_err(d->map->dev, 240 + "Failed to write config %x: %d\n", 241 + reg, ret); 207 242 } 208 243 } 209 244 ··· 240 253 struct regmap *map = d->map; 241 254 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq); 242 255 unsigned int reg = irq_data->reg_offset / map->reg_stride; 243 - unsigned int mask, type; 244 - 245 - type = irq_data->type.type_falling_val | irq_data->type.type_rising_val; 256 + unsigned int mask; 246 257 247 258 /* 248 259 * The type_in_mask flag means that the underlying hardware uses 249 - * separate mask bits for rising and falling edge interrupts, but 250 - * we want to make them into a single virtual interrupt with 251 - * configurable edge. 260 + * separate mask bits for each interrupt trigger type, but we want 261 + * to have a single logical interrupt with a configurable type. 252 262 * 253 - * If the interrupt we're enabling defines the falling or rising 254 - * masks then instead of using the regular mask bits for this 255 - * interrupt, use the value previously written to the type buffer 256 - * at the corresponding offset in regmap_irq_set_type(). 263 + * If the interrupt we're enabling defines any supported types 264 + * then instead of using the regular mask bits for this interrupt, 265 + * use the value previously written to the type buffer at the 266 + * corresponding offset in regmap_irq_set_type(). 257 267 */ 258 - if (d->chip->type_in_mask && type) 268 + if (d->chip->type_in_mask && irq_data->type.types_supported) 259 269 mask = d->type_buf[reg] & irq_data->mask; 260 270 else 261 271 mask = irq_data->mask; ··· 277 293 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data); 278 294 struct regmap *map = d->map; 279 295 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq); 280 - int reg; 296 + int reg, ret; 281 297 const struct regmap_irq_type *t = &irq_data->type; 282 298 283 299 if ((t->types_supported & type) != type) ··· 317 333 return -EINVAL; 318 334 } 319 335 320 - if (d->chip->set_type_virt) 321 - return d->chip->set_type_virt(d->virt_buf, type, data->hwirq, 322 - reg); 336 + if (d->chip->set_type_virt) { 337 + ret = d->chip->set_type_virt(d->virt_buf, type, data->hwirq, 338 + reg); 339 + if (ret) 340 + return ret; 341 + } 342 + 343 + if (d->chip->set_type_config) { 344 + ret = d->chip->set_type_config(d->config_buf, type, 345 + irq_data, reg); 346 + if (ret) 347 + return ret; 348 + } 323 349 324 350 return 0; 325 351 } ··· 370 376 const struct regmap_irq_chip *chip = data->chip; 371 377 struct regmap *map = data->map; 372 378 struct regmap_irq_sub_irq_map *subreg; 379 + unsigned int reg; 373 380 int i, ret = 0; 374 381 375 382 if (!chip->sub_reg_offsets) { 376 - /* Assume linear mapping */ 377 - ret = regmap_read(map, chip->status_base + 378 - (b * map->reg_stride * data->irq_reg_stride), 379 - &data->status_buf[b]); 383 + reg = data->get_irq_reg(data, chip->status_base, b); 384 + ret = regmap_read(map, reg, &data->status_buf[b]); 380 385 } else { 386 + /* 387 + * Note we can't use ->get_irq_reg() here because the offsets 388 + * in 'subreg' are *not* interchangeable with indices. 389 + */ 381 390 subreg = &chip->sub_reg_offsets[b]; 382 391 for (i = 0; i < subreg->num_regs; i++) { 383 392 unsigned int offset = subreg->offset[i]; ··· 446 449 * sake of simplicity. and add bulk reads only if needed 447 450 */ 448 451 for (i = 0; i < chip->num_main_regs; i++) { 449 - ret = regmap_read(map, chip->main_status + 450 - (i * map->reg_stride 451 - * data->irq_reg_stride), 452 - &data->main_status_buf[i]); 452 + /* 453 + * For not_fixed_stride, don't use ->get_irq_reg(). 454 + * It would produce an incorrect result. 455 + */ 456 + if (data->chip->not_fixed_stride) 457 + reg = chip->main_status + 458 + i * map->reg_stride * data->irq_reg_stride; 459 + else 460 + reg = data->get_irq_reg(data, 461 + chip->main_status, i); 462 + 463 + ret = regmap_read(map, reg, &data->main_status_buf[i]); 453 464 if (ret) { 454 465 dev_err(map->dev, 455 466 "Failed to read IRQ status %d\n", ··· 486 481 } 487 482 488 483 } 489 - } else if (!map->use_single_read && map->reg_stride == 1 && 490 - data->irq_reg_stride == 1) { 484 + } else if (regmap_irq_can_bulk_read_status(data)) { 491 485 492 486 u8 *buf8 = data->status_reg_buf; 493 487 u16 *buf16 = data->status_reg_buf; ··· 522 518 523 519 } else { 524 520 for (i = 0; i < data->chip->num_regs; i++) { 525 - unsigned int reg = sub_irq_reg(data, 521 + unsigned int reg = data->get_irq_reg(data, 526 522 data->chip->status_base, i); 527 523 ret = regmap_read(map, reg, &data->status_buf[i]); 528 524 ··· 550 546 data->status_buf[i] &= ~data->mask_buf[i]; 551 547 552 548 if (data->status_buf[i] && (chip->ack_base || chip->use_ack)) { 553 - reg = sub_irq_reg(data, data->chip->ack_base, i); 549 + reg = data->get_irq_reg(data, data->chip->ack_base, i); 554 550 555 551 if (chip->ack_invert) 556 552 ret = regmap_write(map, reg, ··· 611 607 }; 612 608 613 609 /** 610 + * regmap_irq_get_irq_reg_linear() - Linear IRQ register mapping callback. 611 + * @data: Data for the &struct regmap_irq_chip 612 + * @base: Base register 613 + * @index: Register index 614 + * 615 + * Returns the register address corresponding to the given @base and @index 616 + * by the formula ``base + index * regmap_stride * irq_reg_stride``. 617 + */ 618 + unsigned int regmap_irq_get_irq_reg_linear(struct regmap_irq_chip_data *data, 619 + unsigned int base, int index) 620 + { 621 + const struct regmap_irq_chip *chip = data->chip; 622 + struct regmap *map = data->map; 623 + 624 + /* 625 + * FIXME: This is for backward compatibility and should be removed 626 + * when not_fixed_stride is dropped (it's only used by qcom-pm8008). 627 + */ 628 + if (chip->not_fixed_stride && chip->sub_reg_offsets) { 629 + struct regmap_irq_sub_irq_map *subreg; 630 + 631 + subreg = &chip->sub_reg_offsets[0]; 632 + return base + subreg->offset[0]; 633 + } 634 + 635 + return base + index * map->reg_stride * chip->irq_reg_stride; 636 + } 637 + EXPORT_SYMBOL_GPL(regmap_irq_get_irq_reg_linear); 638 + 639 + /** 640 + * regmap_irq_set_type_config_simple() - Simple IRQ type configuration callback. 641 + * @buf: Buffer containing configuration register values, this is a 2D array of 642 + * `num_config_bases` rows, each of `num_config_regs` elements. 643 + * @type: The requested IRQ type. 644 + * @irq_data: The IRQ being configured. 645 + * @idx: Index of the irq's config registers within each array `buf[i]` 646 + * 647 + * This is a &struct regmap_irq_chip->set_type_config callback suitable for 648 + * chips with one config register. Register values are updated according to 649 + * the &struct regmap_irq_type data associated with an IRQ. 650 + */ 651 + int regmap_irq_set_type_config_simple(unsigned int **buf, unsigned int type, 652 + const struct regmap_irq *irq_data, int idx) 653 + { 654 + const struct regmap_irq_type *t = &irq_data->type; 655 + 656 + if (t->type_reg_mask) 657 + buf[0][idx] &= ~t->type_reg_mask; 658 + else 659 + buf[0][idx] &= ~(t->type_falling_val | 660 + t->type_rising_val | 661 + t->type_level_low_val | 662 + t->type_level_high_val); 663 + 664 + switch (type) { 665 + case IRQ_TYPE_EDGE_FALLING: 666 + buf[0][idx] |= t->type_falling_val; 667 + break; 668 + 669 + case IRQ_TYPE_EDGE_RISING: 670 + buf[0][idx] |= t->type_rising_val; 671 + break; 672 + 673 + case IRQ_TYPE_EDGE_BOTH: 674 + buf[0][idx] |= (t->type_falling_val | 675 + t->type_rising_val); 676 + break; 677 + 678 + case IRQ_TYPE_LEVEL_HIGH: 679 + buf[0][idx] |= t->type_level_high_val; 680 + break; 681 + 682 + case IRQ_TYPE_LEVEL_LOW: 683 + buf[0][idx] |= t->type_level_low_val; 684 + break; 685 + 686 + default: 687 + return -EINVAL; 688 + } 689 + 690 + return 0; 691 + } 692 + EXPORT_SYMBOL_GPL(regmap_irq_set_type_config_simple); 693 + 694 + /** 614 695 * regmap_add_irq_chip_fwnode() - Use standard regmap IRQ controller handling 615 696 * 616 697 * @fwnode: The firmware node where the IRQ domain should be added to. ··· 723 634 int ret = -ENOMEM; 724 635 int num_type_reg; 725 636 u32 reg; 726 - u32 unmask_offset; 727 637 728 638 if (chip->num_regs <= 0) 729 639 return -EINVAL; ··· 739 651 } 740 652 741 653 if (chip->not_fixed_stride) { 654 + dev_warn(map->dev, "not_fixed_stride is deprecated; use ->get_irq_reg() instead"); 655 + 742 656 for (i = 0; i < chip->num_regs; i++) 743 657 if (chip->sub_reg_offsets[i].num_regs != 1) 744 658 return -EINVAL; 745 659 } 660 + 661 + if (chip->num_type_reg) 662 + dev_warn(map->dev, "type registers are deprecated; use config registers instead"); 663 + 664 + if (chip->num_virt_regs || chip->virt_reg_base || chip->set_type_virt) 665 + dev_warn(map->dev, "virtual registers are deprecated; use config registers instead"); 746 666 747 667 if (irq_base) { 748 668 irq_base = irq_alloc_descs(irq_base, 0, chip->num_irqs, 0); ··· 767 671 768 672 if (chip->num_main_regs) { 769 673 d->main_status_buf = kcalloc(chip->num_main_regs, 770 - sizeof(unsigned int), 674 + sizeof(*d->main_status_buf), 771 675 GFP_KERNEL); 772 676 773 677 if (!d->main_status_buf) 774 678 goto err_alloc; 775 679 } 776 680 777 - d->status_buf = kcalloc(chip->num_regs, sizeof(unsigned int), 681 + d->status_buf = kcalloc(chip->num_regs, sizeof(*d->status_buf), 778 682 GFP_KERNEL); 779 683 if (!d->status_buf) 780 684 goto err_alloc; 781 685 782 - d->mask_buf = kcalloc(chip->num_regs, sizeof(unsigned int), 686 + d->mask_buf = kcalloc(chip->num_regs, sizeof(*d->mask_buf), 783 687 GFP_KERNEL); 784 688 if (!d->mask_buf) 785 689 goto err_alloc; 786 690 787 - d->mask_buf_def = kcalloc(chip->num_regs, sizeof(unsigned int), 691 + d->mask_buf_def = kcalloc(chip->num_regs, sizeof(*d->mask_buf_def), 788 692 GFP_KERNEL); 789 693 if (!d->mask_buf_def) 790 694 goto err_alloc; 791 695 792 696 if (chip->wake_base) { 793 - d->wake_buf = kcalloc(chip->num_regs, sizeof(unsigned int), 697 + d->wake_buf = kcalloc(chip->num_regs, sizeof(*d->wake_buf), 794 698 GFP_KERNEL); 795 699 if (!d->wake_buf) 796 700 goto err_alloc; ··· 799 703 num_type_reg = chip->type_in_mask ? chip->num_regs : chip->num_type_reg; 800 704 if (num_type_reg) { 801 705 d->type_buf_def = kcalloc(num_type_reg, 802 - sizeof(unsigned int), GFP_KERNEL); 706 + sizeof(*d->type_buf_def), GFP_KERNEL); 803 707 if (!d->type_buf_def) 804 708 goto err_alloc; 805 709 806 - d->type_buf = kcalloc(num_type_reg, sizeof(unsigned int), 710 + d->type_buf = kcalloc(num_type_reg, sizeof(*d->type_buf), 807 711 GFP_KERNEL); 808 712 if (!d->type_buf) 809 713 goto err_alloc; ··· 820 724 821 725 for (i = 0; i < chip->num_virt_regs; i++) { 822 726 d->virt_buf[i] = kcalloc(chip->num_regs, 823 - sizeof(unsigned int), 727 + sizeof(**d->virt_buf), 824 728 GFP_KERNEL); 825 729 if (!d->virt_buf[i]) 730 + goto err_alloc; 731 + } 732 + } 733 + 734 + if (chip->num_config_bases && chip->num_config_regs) { 735 + /* 736 + * Create config_buf[num_config_bases][num_config_regs] 737 + */ 738 + d->config_buf = kcalloc(chip->num_config_bases, 739 + sizeof(*d->config_buf), GFP_KERNEL); 740 + if (!d->config_buf) 741 + goto err_alloc; 742 + 743 + for (i = 0; i < chip->num_config_regs; i++) { 744 + d->config_buf[i] = kcalloc(chip->num_config_regs, 745 + sizeof(**d->config_buf), 746 + GFP_KERNEL); 747 + if (!d->config_buf[i]) 826 748 goto err_alloc; 827 749 } 828 750 } ··· 852 738 d->chip = chip; 853 739 d->irq_base = irq_base; 854 740 741 + if (chip->mask_base && chip->unmask_base && 742 + !chip->mask_unmask_non_inverted) { 743 + /* 744 + * Chips that specify both mask_base and unmask_base used to 745 + * get inverted mask behavior by default, with no way to ask 746 + * for the normal, non-inverted behavior. This "inverted by 747 + * default" behavior is deprecated, but we have to support it 748 + * until existing drivers have been fixed. 749 + * 750 + * Existing drivers should be updated by swapping mask_base 751 + * and unmask_base and setting mask_unmask_non_inverted=true. 752 + * New drivers should always set the flag. 753 + */ 754 + dev_warn(map->dev, "mask_base and unmask_base are inverted, please fix it"); 755 + 756 + /* Might as well warn about mask_invert while we're at it... */ 757 + if (chip->mask_invert) 758 + dev_warn(map->dev, "mask_invert=true ignored"); 759 + 760 + d->mask_base = chip->unmask_base; 761 + d->unmask_base = chip->mask_base; 762 + } else if (chip->mask_invert) { 763 + /* 764 + * Swap the roles of mask_base and unmask_base if the bits are 765 + * inverted. This is deprecated, drivers should use unmask_base 766 + * directly. 767 + */ 768 + dev_warn(map->dev, "mask_invert=true is deprecated; please switch to unmask_base"); 769 + 770 + d->mask_base = chip->unmask_base; 771 + d->unmask_base = chip->mask_base; 772 + } else { 773 + d->mask_base = chip->mask_base; 774 + d->unmask_base = chip->unmask_base; 775 + } 776 + 855 777 if (chip->irq_reg_stride) 856 778 d->irq_reg_stride = chip->irq_reg_stride; 857 779 else 858 780 d->irq_reg_stride = 1; 859 781 860 - if (chip->type_reg_stride) 861 - d->type_reg_stride = chip->type_reg_stride; 782 + if (chip->get_irq_reg) 783 + d->get_irq_reg = chip->get_irq_reg; 862 784 else 863 - d->type_reg_stride = 1; 785 + d->get_irq_reg = regmap_irq_get_irq_reg_linear; 864 786 865 - if (!map->use_single_read && map->reg_stride == 1 && 866 - d->irq_reg_stride == 1) { 787 + if (regmap_irq_can_bulk_read_status(d)) { 867 788 d->status_reg_buf = kmalloc_array(chip->num_regs, 868 789 map->format.val_bytes, 869 790 GFP_KERNEL); ··· 915 766 /* Mask all the interrupts by default */ 916 767 for (i = 0; i < chip->num_regs; i++) { 917 768 d->mask_buf[i] = d->mask_buf_def[i]; 918 - if (!chip->mask_base) 919 - continue; 920 769 921 - reg = sub_irq_reg(d, d->chip->mask_base, i); 770 + if (d->mask_base) { 771 + reg = d->get_irq_reg(d, d->mask_base, i); 772 + ret = regmap_update_bits(d->map, reg, 773 + d->mask_buf_def[i], d->mask_buf[i]); 774 + if (ret) { 775 + dev_err(map->dev, "Failed to set masks in 0x%x: %d\n", 776 + reg, ret); 777 + goto err_alloc; 778 + } 779 + } 922 780 923 - if (chip->mask_invert) 924 - ret = regmap_irq_update_bits(d, reg, 925 - d->mask_buf[i], ~d->mask_buf[i]); 926 - else if (d->chip->unmask_base) { 927 - unmask_offset = d->chip->unmask_base - 928 - d->chip->mask_base; 929 - ret = regmap_irq_update_bits(d, 930 - reg + unmask_offset, 931 - d->mask_buf[i], 932 - d->mask_buf[i]); 933 - } else 934 - ret = regmap_irq_update_bits(d, reg, 935 - d->mask_buf[i], d->mask_buf[i]); 936 - if (ret != 0) { 937 - dev_err(map->dev, "Failed to set masks in 0x%x: %d\n", 938 - reg, ret); 939 - goto err_alloc; 781 + if (d->unmask_base) { 782 + reg = d->get_irq_reg(d, d->unmask_base, i); 783 + ret = regmap_update_bits(d->map, reg, 784 + d->mask_buf_def[i], ~d->mask_buf[i]); 785 + if (ret) { 786 + dev_err(map->dev, "Failed to set masks in 0x%x: %d\n", 787 + reg, ret); 788 + goto err_alloc; 789 + } 940 790 } 941 791 942 792 if (!chip->init_ack_masked) 943 793 continue; 944 794 945 795 /* Ack masked but set interrupts */ 946 - reg = sub_irq_reg(d, d->chip->status_base, i); 796 + reg = d->get_irq_reg(d, d->chip->status_base, i); 947 797 ret = regmap_read(map, reg, &d->status_buf[i]); 948 798 if (ret != 0) { 949 799 dev_err(map->dev, "Failed to read IRQ status: %d\n", ··· 954 806 d->status_buf[i] = ~d->status_buf[i]; 955 807 956 808 if (d->status_buf[i] && (chip->ack_base || chip->use_ack)) { 957 - reg = sub_irq_reg(d, d->chip->ack_base, i); 809 + reg = d->get_irq_reg(d, d->chip->ack_base, i); 958 810 if (chip->ack_invert) 959 811 ret = regmap_write(map, reg, 960 812 ~(d->status_buf[i] & d->mask_buf[i])); ··· 979 831 if (d->wake_buf) { 980 832 for (i = 0; i < chip->num_regs; i++) { 981 833 d->wake_buf[i] = d->mask_buf_def[i]; 982 - reg = sub_irq_reg(d, d->chip->wake_base, i); 834 + reg = d->get_irq_reg(d, d->chip->wake_base, i); 983 835 984 836 if (chip->wake_invert) 985 - ret = regmap_irq_update_bits(d, reg, 837 + ret = regmap_update_bits(d->map, reg, 986 838 d->mask_buf_def[i], 987 839 0); 988 840 else 989 - ret = regmap_irq_update_bits(d, reg, 841 + ret = regmap_update_bits(d->map, reg, 990 842 d->mask_buf_def[i], 991 843 d->wake_buf[i]); 992 844 if (ret != 0) { ··· 999 851 1000 852 if (chip->num_type_reg && !chip->type_in_mask) { 1001 853 for (i = 0; i < chip->num_type_reg; ++i) { 1002 - reg = sub_irq_reg(d, d->chip->type_base, i); 854 + reg = d->get_irq_reg(d, d->chip->type_base, i); 1003 855 1004 856 ret = regmap_read(map, reg, &d->type_buf_def[i]); 1005 857 ··· 1055 907 kfree(d->virt_buf[i]); 1056 908 kfree(d->virt_buf); 1057 909 } 910 + if (d->config_buf) { 911 + for (i = 0; i < chip->num_config_bases; i++) 912 + kfree(d->config_buf[i]); 913 + kfree(d->config_buf); 914 + } 1058 915 kfree(d); 1059 916 return ret; 1060 917 } ··· 1100 947 void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *d) 1101 948 { 1102 949 unsigned int virq; 1103 - int hwirq; 950 + int i, hwirq; 1104 951 1105 952 if (!d) 1106 953 return; ··· 1130 977 kfree(d->mask_buf); 1131 978 kfree(d->status_reg_buf); 1132 979 kfree(d->status_buf); 980 + if (d->config_buf) { 981 + for (i = 0; i < d->chip->num_config_bases; i++) 982 + kfree(d->config_buf[i]); 983 + kfree(d->config_buf); 984 + } 1133 985 kfree(d); 1134 986 } 1135 987 EXPORT_SYMBOL_GPL(regmap_del_irq_chip);
+72 -32
include/linux/regmap.h
··· 1440 1440 unsigned int *offset; 1441 1441 }; 1442 1442 1443 + struct regmap_irq_chip_data; 1444 + 1443 1445 /** 1444 1446 * struct regmap_irq_chip - Description of a generic regmap irq_chip. 1445 1447 * ··· 1469 1467 * main_status set. 1470 1468 * 1471 1469 * @status_base: Base status register address. 1472 - * @mask_base: Base mask register address. 1473 - * @mask_writeonly: Base mask register is write only. 1474 - * @unmask_base: Base unmask register address. for chips who have 1475 - * separate mask and unmask registers 1470 + * @mask_base: Base mask register address. Mask bits are set to 1 when an 1471 + * interrupt is masked, 0 when unmasked. 1472 + * @unmask_base: Base unmask register address. Unmask bits are set to 1 when 1473 + * an interrupt is unmasked and 0 when masked. 1476 1474 * @ack_base: Base ack address. If zero then the chip is clear on read. 1477 1475 * Using zero value is possible with @use_ack bit. 1478 1476 * @wake_base: Base address for wake enables. If zero unsupported. 1479 - * @type_base: Base address for irq type. If zero unsupported. 1480 - * @virt_reg_base: Base addresses for extra config regs. 1477 + * @type_base: Base address for irq type. If zero unsupported. Deprecated, 1478 + * use @config_base instead. 1479 + * @virt_reg_base: Base addresses for extra config regs. Deprecated, use 1480 + * @config_base instead. 1481 + * @config_base: Base address for IRQ type config regs. If null unsupported. 1481 1482 * @irq_reg_stride: Stride to use for chips where registers are not contiguous. 1482 1483 * @init_ack_masked: Ack all masked interrupts once during initalization. 1483 1484 * @mask_invert: Inverted mask register: cleared bits are masked out. 1485 + * Deprecated; prefer describing an inverted mask register as 1486 + * an unmask register. 1487 + * @mask_unmask_non_inverted: Controls mask bit inversion for chips that set 1488 + * both @mask_base and @unmask_base. If false, mask and unmask bits are 1489 + * inverted (which is deprecated behavior); if true, bits will not be 1490 + * inverted and the registers keep their normal behavior. Note that if 1491 + * you use only one of @mask_base or @unmask_base, this flag has no 1492 + * effect and is unnecessary. Any new drivers that set both @mask_base 1493 + * and @unmask_base should set this to true to avoid relying on the 1494 + * deprecated behavior. 1484 1495 * @use_ack: Use @ack register even if it is zero. 1485 1496 * @ack_invert: Inverted ack register: cleared bits for ack. 1486 1497 * @clear_ack: Use this to set 1 and 0 or vice-versa to clear interrupts. 1487 1498 * @wake_invert: Inverted wake register: cleared bits are wake enabled. 1488 - * @type_invert: Invert the type flags. 1489 - * @type_in_mask: Use the mask registers for controlling irq type. For 1490 - * interrupts defining type_rising/falling_mask use mask_base 1491 - * for edge configuration and never update bits in type_base. 1499 + * @type_invert: Invert the type flags. Deprecated, use config registers 1500 + * instead. 1501 + * @type_in_mask: Use the mask registers for controlling irq type. Use this if 1502 + * the hardware provides separate bits for rising/falling edge 1503 + * or low/high level interrupts and they should be combined into 1504 + * a single logical interrupt. Use &struct regmap_irq_type data 1505 + * to define the mask bit for each irq type. 1492 1506 * @clear_on_unmask: For chips with interrupts cleared on read: read the status 1493 1507 * registers before unmasking interrupts to clear any bits 1494 1508 * set when they were masked. 1495 1509 * @not_fixed_stride: Used when chip peripherals are not laid out with fixed 1496 - * stride. Must be used with sub_reg_offsets containing the 1497 - * offsets to each peripheral. 1510 + * stride. Must be used with sub_reg_offsets containing the 1511 + * offsets to each peripheral. Deprecated; the same thing 1512 + * can be accomplished with a @get_irq_reg callback, without 1513 + * the need for a @sub_reg_offsets table. 1498 1514 * @status_invert: Inverted status register: cleared bits are active interrupts. 1499 1515 * @runtime_pm: Hold a runtime PM lock on the device when accessing it. 1500 1516 * ··· 1520 1500 * @irqs: Descriptors for individual IRQs. Interrupt numbers are 1521 1501 * assigned based on the index in the array of the interrupt. 1522 1502 * @num_irqs: Number of descriptors. 1523 - * @num_type_reg: Number of type registers. 1503 + * @num_type_reg: Number of type registers. Deprecated, use config registers 1504 + * instead. 1524 1505 * @num_virt_regs: Number of non-standard irq configuration registers. 1525 - * If zero unsupported. 1526 - * @type_reg_stride: Stride to use for chips where type registers are not 1527 - * contiguous. 1506 + * If zero unsupported. Deprecated, use config registers 1507 + * instead. 1508 + * @num_config_bases: Number of config base registers. 1509 + * @num_config_regs: Number of config registers for each config base register. 1528 1510 * @handle_pre_irq: Driver specific callback to handle interrupt from device 1529 1511 * before regmap_irq_handler process the interrupts. 1530 1512 * @handle_post_irq: Driver specific callback to handle interrupt from device 1531 1513 * after handling the interrupts in regmap_irq_handler(). 1532 1514 * @set_type_virt: Driver specific callback to extend regmap_irq_set_type() 1533 - * and configure virt regs. 1515 + * and configure virt regs. Deprecated, use @set_type_config 1516 + * callback and config registers instead. 1517 + * @set_type_config: Callback used for configuring irq types. 1518 + * @get_irq_reg: Callback for mapping (base register, index) pairs to register 1519 + * addresses. The base register will be one of @status_base, 1520 + * @mask_base, etc., @main_status, or any of @config_base. 1521 + * The index will be in the range [0, num_main_regs[ for the 1522 + * main status base, [0, num_type_settings[ for any config 1523 + * register base, and [0, num_regs[ for any other base. 1524 + * If unspecified then regmap_irq_get_irq_reg_linear() is used. 1534 1525 * @irq_drv_data: Driver specific IRQ data which is passed as parameter when 1535 1526 * driver specific pre/post interrupt handler is called. 1536 1527 * ··· 1564 1533 unsigned int wake_base; 1565 1534 unsigned int type_base; 1566 1535 unsigned int *virt_reg_base; 1536 + const unsigned int *config_base; 1567 1537 unsigned int irq_reg_stride; 1568 - bool mask_writeonly:1; 1569 - bool init_ack_masked:1; 1570 - bool mask_invert:1; 1571 - bool use_ack:1; 1572 - bool ack_invert:1; 1573 - bool clear_ack:1; 1574 - bool wake_invert:1; 1575 - bool runtime_pm:1; 1576 - bool type_invert:1; 1577 - bool type_in_mask:1; 1578 - bool clear_on_unmask:1; 1579 - bool not_fixed_stride:1; 1580 - bool status_invert:1; 1538 + unsigned int init_ack_masked:1; 1539 + unsigned int mask_invert:1; 1540 + unsigned int mask_unmask_non_inverted:1; 1541 + unsigned int use_ack:1; 1542 + unsigned int ack_invert:1; 1543 + unsigned int clear_ack:1; 1544 + unsigned int wake_invert:1; 1545 + unsigned int runtime_pm:1; 1546 + unsigned int type_invert:1; 1547 + unsigned int type_in_mask:1; 1548 + unsigned int clear_on_unmask:1; 1549 + unsigned int not_fixed_stride:1; 1550 + unsigned int status_invert:1; 1581 1551 1582 1552 int num_regs; 1583 1553 ··· 1587 1555 1588 1556 int num_type_reg; 1589 1557 int num_virt_regs; 1590 - unsigned int type_reg_stride; 1558 + int num_config_bases; 1559 + int num_config_regs; 1591 1560 1592 1561 int (*handle_pre_irq)(void *irq_drv_data); 1593 1562 int (*handle_post_irq)(void *irq_drv_data); 1594 1563 int (*set_type_virt)(unsigned int **buf, unsigned int type, 1595 1564 unsigned long hwirq, int reg); 1565 + int (*set_type_config)(unsigned int **buf, unsigned int type, 1566 + const struct regmap_irq *irq_data, int idx); 1567 + unsigned int (*get_irq_reg)(struct regmap_irq_chip_data *data, 1568 + unsigned int base, int index); 1596 1569 void *irq_drv_data; 1597 1570 }; 1598 1571 1599 - struct regmap_irq_chip_data; 1572 + unsigned int regmap_irq_get_irq_reg_linear(struct regmap_irq_chip_data *data, 1573 + unsigned int base, int index); 1574 + int regmap_irq_set_type_config_simple(unsigned int **buf, unsigned int type, 1575 + const struct regmap_irq *irq_data, int idx); 1600 1576 1601 1577 int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags, 1602 1578 int irq_base, const struct regmap_irq_chip *chip,