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