1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * GPIO driver for Marvell SoCs 4 * 5 * Copyright (C) 2012 Marvell 6 * 7 * Thomas Petazzoni <thomas.petazzoni@free-electrons.com> 8 * Andrew Lunn <andrew@lunn.ch> 9 * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com> 10 * 11 * This driver is a fairly straightforward GPIO driver for the 12 * complete family of Marvell EBU SoC platforms (Orion, Dove, 13 * Kirkwood, Discovery, Armada 370/XP). The only complexity of this 14 * driver is the different register layout that exists between the 15 * non-SMP platforms (Orion, Dove, Kirkwood, Armada 370) and the SMP 16 * platforms (MV78200 from the Discovery family and the Armada 17 * XP). Therefore, this driver handles three variants of the GPIO 18 * block: 19 * - the basic variant, called "orion-gpio", with the simplest 20 * register set. Used on Orion, Dove, Kirkwoord, Armada 370 and 21 * non-SMP Discovery systems 22 * - the mv78200 variant for MV78200 Discovery systems. This variant 23 * turns the edge mask and level mask registers into CPU0 edge 24 * mask/level mask registers, and adds CPU1 edge mask/level mask 25 * registers. 26 * - the armadaxp variant for Armada XP systems. This variant keeps 27 * the normal cause/edge mask/level mask registers when the global 28 * interrupts are used, but adds per-CPU cause/edge mask/level mask 29 * registers n a separate memory area for the per-CPU GPIO 30 * interrupts. 31 */ 32 33 #include <linux/bitops.h> 34 #include <linux/clk.h> 35 #include <linux/err.h> 36 #include <linux/gpio/driver.h> 37 #include <linux/gpio/consumer.h> 38 #include <linux/gpio/machine.h> 39 #include <linux/init.h> 40 #include <linux/interrupt.h> 41 #include <linux/io.h> 42 #include <linux/irq.h> 43 #include <linux/irqchip/chained_irq.h> 44 #include <linux/irqdomain.h> 45 #include <linux/mfd/syscon.h> 46 #include <linux/of.h> 47 #include <linux/pinctrl/consumer.h> 48 #include <linux/platform_device.h> 49 #include <linux/property.h> 50 #include <linux/pwm.h> 51 #include <linux/regmap.h> 52 #include <linux/slab.h> 53 #include <linux/string_choices.h> 54 55 /* 56 * GPIO unit register offsets. 57 */ 58 #define GPIO_OUT_OFF 0x0000 59 #define GPIO_IO_CONF_OFF 0x0004 60 #define GPIO_BLINK_EN_OFF 0x0008 61 #define GPIO_IN_POL_OFF 0x000c 62 #define GPIO_DATA_IN_OFF 0x0010 63 #define GPIO_EDGE_CAUSE_OFF 0x0014 64 #define GPIO_EDGE_MASK_OFF 0x0018 65 #define GPIO_LEVEL_MASK_OFF 0x001c 66 #define GPIO_BLINK_CNT_SELECT_OFF 0x0020 67 68 /* 69 * PWM register offsets. 70 */ 71 #define PWM_BLINK_ON_DURATION_OFF 0x0 72 #define PWM_BLINK_OFF_DURATION_OFF 0x4 73 #define PWM_BLINK_COUNTER_B_OFF 0x8 74 75 /* Armada 8k variant gpios register offsets */ 76 #define AP80X_GPIO0_OFF_A8K 0x1040 77 #define CP11X_GPIO0_OFF_A8K 0x100 78 #define CP11X_GPIO1_OFF_A8K 0x140 79 80 /* The MV78200 has per-CPU registers for edge mask and level mask */ 81 #define GPIO_EDGE_MASK_MV78200_OFF(cpu) ((cpu) ? 0x30 : 0x18) 82 #define GPIO_LEVEL_MASK_MV78200_OFF(cpu) ((cpu) ? 0x34 : 0x1C) 83 84 /* 85 * The Armada XP has per-CPU registers for interrupt cause, interrupt 86 * mask and interrupt level mask. Those are in percpu_regs range. 87 */ 88 #define GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu) ((cpu) * 0x4) 89 #define GPIO_EDGE_MASK_ARMADAXP_OFF(cpu) (0x10 + (cpu) * 0x4) 90 #define GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu) (0x20 + (cpu) * 0x4) 91 92 #define MVEBU_GPIO_SOC_VARIANT_ORION 0x1 93 #define MVEBU_GPIO_SOC_VARIANT_MV78200 0x2 94 #define MVEBU_GPIO_SOC_VARIANT_ARMADAXP 0x3 95 #define MVEBU_GPIO_SOC_VARIANT_A8K 0x4 96 97 #define MVEBU_MAX_GPIO_PER_BANK 32 98 99 struct mvebu_pwm { 100 struct regmap *regs; 101 u32 offset; 102 unsigned long clk_rate; 103 struct gpio_desc *gpiod; 104 spinlock_t lock; 105 struct mvebu_gpio_chip *mvchip; 106 107 /* Used to preserve GPIO/PWM registers across suspend/resume */ 108 u32 blink_select; 109 u32 blink_on_duration; 110 u32 blink_off_duration; 111 }; 112 113 struct mvebu_gpio_chip { 114 struct gpio_chip chip; 115 struct regmap *regs; 116 u32 offset; 117 struct regmap *percpu_regs; 118 int bank_irq[4]; 119 struct irq_domain *domain; 120 int soc_variant; 121 122 /* Used for PWM support */ 123 struct clk *clk; 124 struct mvebu_pwm *mvpwm; 125 126 /* Used to preserve GPIO registers across suspend/resume */ 127 u32 out_reg; 128 u32 io_conf_reg; 129 u32 blink_en_reg; 130 u32 in_pol_reg; 131 u32 edge_mask_regs[4]; 132 u32 level_mask_regs[4]; 133 }; 134 135 /* 136 * Functions returning addresses of individual registers for a given 137 * GPIO controller. 138 */ 139 140 static void mvebu_gpioreg_edge_cause(struct mvebu_gpio_chip *mvchip, 141 struct regmap **map, unsigned int *offset) 142 { 143 int cpu; 144 145 switch (mvchip->soc_variant) { 146 case MVEBU_GPIO_SOC_VARIANT_ORION: 147 case MVEBU_GPIO_SOC_VARIANT_MV78200: 148 case MVEBU_GPIO_SOC_VARIANT_A8K: 149 *map = mvchip->regs; 150 *offset = GPIO_EDGE_CAUSE_OFF + mvchip->offset; 151 break; 152 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP: 153 cpu = smp_processor_id(); 154 *map = mvchip->percpu_regs; 155 *offset = GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu); 156 break; 157 default: 158 BUG(); 159 } 160 } 161 162 static u32 163 mvebu_gpio_read_edge_cause(struct mvebu_gpio_chip *mvchip) 164 { 165 struct regmap *map; 166 unsigned int offset; 167 u32 val; 168 169 mvebu_gpioreg_edge_cause(mvchip, &map, &offset); 170 regmap_read(map, offset, &val); 171 172 return val; 173 } 174 175 static void 176 mvebu_gpio_write_edge_cause(struct mvebu_gpio_chip *mvchip, u32 val) 177 { 178 struct regmap *map; 179 unsigned int offset; 180 181 mvebu_gpioreg_edge_cause(mvchip, &map, &offset); 182 regmap_write(map, offset, val); 183 } 184 185 static inline void 186 mvebu_gpioreg_edge_mask(struct mvebu_gpio_chip *mvchip, 187 struct regmap **map, unsigned int *offset) 188 { 189 int cpu; 190 191 switch (mvchip->soc_variant) { 192 case MVEBU_GPIO_SOC_VARIANT_ORION: 193 case MVEBU_GPIO_SOC_VARIANT_A8K: 194 *map = mvchip->regs; 195 *offset = GPIO_EDGE_MASK_OFF + mvchip->offset; 196 break; 197 case MVEBU_GPIO_SOC_VARIANT_MV78200: 198 cpu = smp_processor_id(); 199 *map = mvchip->regs; 200 *offset = GPIO_EDGE_MASK_MV78200_OFF(cpu); 201 break; 202 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP: 203 cpu = smp_processor_id(); 204 *map = mvchip->percpu_regs; 205 *offset = GPIO_EDGE_MASK_ARMADAXP_OFF(cpu); 206 break; 207 default: 208 BUG(); 209 } 210 } 211 212 static u32 213 mvebu_gpio_read_edge_mask(struct mvebu_gpio_chip *mvchip) 214 { 215 struct regmap *map; 216 unsigned int offset; 217 u32 val; 218 219 mvebu_gpioreg_edge_mask(mvchip, &map, &offset); 220 regmap_read(map, offset, &val); 221 222 return val; 223 } 224 225 static void 226 mvebu_gpio_write_edge_mask(struct mvebu_gpio_chip *mvchip, u32 val) 227 { 228 struct regmap *map; 229 unsigned int offset; 230 231 mvebu_gpioreg_edge_mask(mvchip, &map, &offset); 232 regmap_write(map, offset, val); 233 } 234 235 static void 236 mvebu_gpioreg_level_mask(struct mvebu_gpio_chip *mvchip, 237 struct regmap **map, unsigned int *offset) 238 { 239 int cpu; 240 241 switch (mvchip->soc_variant) { 242 case MVEBU_GPIO_SOC_VARIANT_ORION: 243 case MVEBU_GPIO_SOC_VARIANT_A8K: 244 *map = mvchip->regs; 245 *offset = GPIO_LEVEL_MASK_OFF + mvchip->offset; 246 break; 247 case MVEBU_GPIO_SOC_VARIANT_MV78200: 248 cpu = smp_processor_id(); 249 *map = mvchip->regs; 250 *offset = GPIO_LEVEL_MASK_MV78200_OFF(cpu); 251 break; 252 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP: 253 cpu = smp_processor_id(); 254 *map = mvchip->percpu_regs; 255 *offset = GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu); 256 break; 257 default: 258 BUG(); 259 } 260 } 261 262 static u32 263 mvebu_gpio_read_level_mask(struct mvebu_gpio_chip *mvchip) 264 { 265 struct regmap *map; 266 unsigned int offset; 267 u32 val; 268 269 mvebu_gpioreg_level_mask(mvchip, &map, &offset); 270 regmap_read(map, offset, &val); 271 272 return val; 273 } 274 275 static void 276 mvebu_gpio_write_level_mask(struct mvebu_gpio_chip *mvchip, u32 val) 277 { 278 struct regmap *map; 279 unsigned int offset; 280 281 mvebu_gpioreg_level_mask(mvchip, &map, &offset); 282 regmap_write(map, offset, val); 283 } 284 285 /* 286 * Functions returning offsets of individual registers for a given 287 * PWM controller. 288 */ 289 static unsigned int mvebu_pwmreg_blink_on_duration(struct mvebu_pwm *mvpwm) 290 { 291 return mvpwm->offset + PWM_BLINK_ON_DURATION_OFF; 292 } 293 294 static unsigned int mvebu_pwmreg_blink_off_duration(struct mvebu_pwm *mvpwm) 295 { 296 return mvpwm->offset + PWM_BLINK_OFF_DURATION_OFF; 297 } 298 299 /* 300 * Functions implementing the gpio_chip methods 301 */ 302 static int mvebu_gpio_set(struct gpio_chip *chip, unsigned int pin, int value) 303 { 304 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip); 305 306 return regmap_update_bits(mvchip->regs, GPIO_OUT_OFF + mvchip->offset, 307 BIT(pin), value ? BIT(pin) : 0); 308 } 309 310 static int mvebu_gpio_get(struct gpio_chip *chip, unsigned int pin) 311 { 312 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip); 313 u32 u; 314 315 regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset, &u); 316 317 if (u & BIT(pin)) { 318 u32 data_in, in_pol; 319 320 regmap_read(mvchip->regs, GPIO_DATA_IN_OFF + mvchip->offset, 321 &data_in); 322 regmap_read(mvchip->regs, GPIO_IN_POL_OFF + mvchip->offset, 323 &in_pol); 324 u = data_in ^ in_pol; 325 } else { 326 regmap_read(mvchip->regs, GPIO_OUT_OFF + mvchip->offset, &u); 327 } 328 329 return (u >> pin) & 1; 330 } 331 332 static void mvebu_gpio_blink(struct gpio_chip *chip, unsigned int pin, 333 int value) 334 { 335 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip); 336 337 regmap_update_bits(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset, 338 BIT(pin), value ? BIT(pin) : 0); 339 } 340 341 static int mvebu_gpio_direction_input(struct gpio_chip *chip, unsigned int pin) 342 { 343 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip); 344 int ret; 345 346 /* 347 * Check with the pinctrl driver whether this pin is usable as 348 * an input GPIO 349 */ 350 ret = pinctrl_gpio_direction_input(chip, pin); 351 if (ret) 352 return ret; 353 354 regmap_update_bits(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset, 355 BIT(pin), BIT(pin)); 356 357 return 0; 358 } 359 360 static int mvebu_gpio_direction_output(struct gpio_chip *chip, unsigned int pin, 361 int value) 362 { 363 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip); 364 int ret; 365 366 /* 367 * Check with the pinctrl driver whether this pin is usable as 368 * an output GPIO 369 */ 370 ret = pinctrl_gpio_direction_output(chip, pin); 371 if (ret) 372 return ret; 373 374 mvebu_gpio_blink(chip, pin, 0); 375 mvebu_gpio_set(chip, pin, value); 376 377 regmap_update_bits(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset, 378 BIT(pin), 0); 379 380 return 0; 381 } 382 383 static int mvebu_gpio_get_direction(struct gpio_chip *chip, unsigned int pin) 384 { 385 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip); 386 u32 u; 387 388 regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset, &u); 389 390 if (u & BIT(pin)) 391 return GPIO_LINE_DIRECTION_IN; 392 393 return GPIO_LINE_DIRECTION_OUT; 394 } 395 396 static int mvebu_gpio_to_irq(struct gpio_chip *chip, unsigned int pin) 397 { 398 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip); 399 400 return irq_create_mapping(mvchip->domain, pin); 401 } 402 403 /* 404 * Functions implementing the irq_chip methods 405 */ 406 static void mvebu_gpio_irq_ack(struct irq_data *d) 407 { 408 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 409 struct mvebu_gpio_chip *mvchip = gc->private; 410 u32 mask = d->mask; 411 412 guard(raw_spinlock)(&gc->lock); 413 mvebu_gpio_write_edge_cause(mvchip, ~mask); 414 } 415 416 static void mvebu_gpio_edge_irq_mask(struct irq_data *d) 417 { 418 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 419 struct mvebu_gpio_chip *mvchip = gc->private; 420 struct irq_chip_type *ct = irq_data_get_chip_type(d); 421 u32 mask = d->mask; 422 423 guard(raw_spinlock)(&gc->lock); 424 ct->mask_cache_priv &= ~mask; 425 mvebu_gpio_write_edge_mask(mvchip, ct->mask_cache_priv); 426 } 427 428 static void mvebu_gpio_edge_irq_unmask(struct irq_data *d) 429 { 430 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 431 struct mvebu_gpio_chip *mvchip = gc->private; 432 struct irq_chip_type *ct = irq_data_get_chip_type(d); 433 u32 mask = d->mask; 434 435 guard(raw_spinlock)(&gc->lock); 436 mvebu_gpio_write_edge_cause(mvchip, ~mask); 437 ct->mask_cache_priv |= mask; 438 mvebu_gpio_write_edge_mask(mvchip, ct->mask_cache_priv); 439 } 440 441 static void mvebu_gpio_level_irq_mask(struct irq_data *d) 442 { 443 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 444 struct mvebu_gpio_chip *mvchip = gc->private; 445 struct irq_chip_type *ct = irq_data_get_chip_type(d); 446 u32 mask = d->mask; 447 448 guard(raw_spinlock)(&gc->lock); 449 ct->mask_cache_priv &= ~mask; 450 mvebu_gpio_write_level_mask(mvchip, ct->mask_cache_priv); 451 } 452 453 static void mvebu_gpio_level_irq_unmask(struct irq_data *d) 454 { 455 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 456 struct mvebu_gpio_chip *mvchip = gc->private; 457 struct irq_chip_type *ct = irq_data_get_chip_type(d); 458 u32 mask = d->mask; 459 460 guard(raw_spinlock)(&gc->lock); 461 ct->mask_cache_priv |= mask; 462 mvebu_gpio_write_level_mask(mvchip, ct->mask_cache_priv); 463 } 464 465 /***************************************************************************** 466 * MVEBU GPIO IRQ 467 * 468 * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same 469 * value of the line or the opposite value. 470 * 471 * Level IRQ handlers: DATA_IN is used directly as cause register. 472 * Interrupt are masked by LEVEL_MASK registers. 473 * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE. 474 * Interrupt are masked by EDGE_MASK registers. 475 * Both-edge handlers: Similar to regular Edge handlers, but also swaps 476 * the polarity to catch the next line transaction. 477 * This is a race condition that might not perfectly 478 * work on some use cases. 479 * 480 * Every eight GPIO lines are grouped (OR'ed) before going up to main 481 * cause register. 482 * 483 * EDGE cause mask 484 * data-in /--------| |-----| |----\ 485 * -----| |----- ---- to main cause reg 486 * X \----------------| |----/ 487 * polarity LEVEL mask 488 * 489 ****************************************************************************/ 490 491 static int mvebu_gpio_irq_set_type(struct irq_data *d, unsigned int type) 492 { 493 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 494 struct irq_chip_type *ct = irq_data_get_chip_type(d); 495 struct mvebu_gpio_chip *mvchip = gc->private; 496 int pin; 497 u32 u; 498 499 pin = d->hwirq; 500 501 regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset, &u); 502 if ((u & BIT(pin)) == 0) 503 return -EINVAL; 504 505 type &= IRQ_TYPE_SENSE_MASK; 506 if (type == IRQ_TYPE_NONE) 507 return -EINVAL; 508 509 /* Check if we need to change chip and handler */ 510 if (!(ct->type & type)) 511 if (irq_setup_alt_chip(d, type)) 512 return -EINVAL; 513 514 /* 515 * Configure interrupt polarity. 516 */ 517 switch (type) { 518 case IRQ_TYPE_EDGE_RISING: 519 case IRQ_TYPE_LEVEL_HIGH: 520 regmap_update_bits(mvchip->regs, 521 GPIO_IN_POL_OFF + mvchip->offset, 522 BIT(pin), 0); 523 break; 524 case IRQ_TYPE_EDGE_FALLING: 525 case IRQ_TYPE_LEVEL_LOW: 526 regmap_update_bits(mvchip->regs, 527 GPIO_IN_POL_OFF + mvchip->offset, 528 BIT(pin), BIT(pin)); 529 break; 530 case IRQ_TYPE_EDGE_BOTH: { 531 u32 data_in, in_pol, val; 532 533 regmap_read(mvchip->regs, 534 GPIO_IN_POL_OFF + mvchip->offset, &in_pol); 535 regmap_read(mvchip->regs, 536 GPIO_DATA_IN_OFF + mvchip->offset, &data_in); 537 538 /* 539 * set initial polarity based on current input level 540 */ 541 if ((data_in ^ in_pol) & BIT(pin)) 542 val = BIT(pin); /* falling */ 543 else 544 val = 0; /* raising */ 545 546 regmap_update_bits(mvchip->regs, 547 GPIO_IN_POL_OFF + mvchip->offset, 548 BIT(pin), val); 549 break; 550 } 551 } 552 return 0; 553 } 554 555 static void mvebu_gpio_irq_handler(struct irq_desc *desc) 556 { 557 struct mvebu_gpio_chip *mvchip = irq_desc_get_handler_data(desc); 558 struct irq_chip *chip = irq_desc_get_chip(desc); 559 u32 cause, type, data_in, level_mask, edge_cause, edge_mask; 560 int i; 561 562 if (mvchip == NULL) 563 return; 564 565 chained_irq_enter(chip, desc); 566 567 regmap_read(mvchip->regs, GPIO_DATA_IN_OFF + mvchip->offset, &data_in); 568 level_mask = mvebu_gpio_read_level_mask(mvchip); 569 edge_cause = mvebu_gpio_read_edge_cause(mvchip); 570 edge_mask = mvebu_gpio_read_edge_mask(mvchip); 571 572 cause = (data_in & level_mask) | (edge_cause & edge_mask); 573 574 for (i = 0; i < mvchip->chip.ngpio; i++) { 575 int irq; 576 577 if (!(cause & BIT(i))) 578 continue; 579 580 irq = irq_find_mapping(mvchip->domain, i); 581 type = irq_get_trigger_type(irq); 582 if ((type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) { 583 /* Swap polarity (race with GPIO line) */ 584 u32 polarity; 585 586 regmap_read(mvchip->regs, 587 GPIO_IN_POL_OFF + mvchip->offset, 588 &polarity); 589 polarity ^= BIT(i); 590 regmap_write(mvchip->regs, 591 GPIO_IN_POL_OFF + mvchip->offset, 592 polarity); 593 } 594 595 generic_handle_irq(irq); 596 } 597 598 chained_irq_exit(chip, desc); 599 } 600 601 static const struct regmap_config mvebu_gpio_regmap_config = { 602 .name = "gpio", 603 .reg_bits = 32, 604 .reg_stride = 4, 605 .val_bits = 32, 606 .use_raw_spinlock = true, 607 }; 608 609 static const struct regmap_config mvebu_gpio_percpu_regmap_config = { 610 .name = "percpu", 611 .reg_bits = 32, 612 .reg_stride = 4, 613 .val_bits = 32, 614 .use_raw_spinlock = true, 615 }; 616 617 static const struct regmap_config mvebu_pwm_regmap_config = { 618 .name = "pwm", 619 .reg_bits = 32, 620 .reg_stride = 4, 621 .val_bits = 32, 622 }; 623 624 /* 625 * Forward wake-up configuration to the parent bank IRQ. 626 * @d: interrupt data 627 * @enable: enable as wake-up if non-zero 628 * 629 * Return: 0 on success, or a negative error code. 630 */ 631 static int mvebu_gpio_set_wake_irq(struct irq_data *d, unsigned int enable) 632 { 633 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 634 struct mvebu_gpio_chip *mvchip = gc->private; 635 int bank; 636 int irq; 637 638 bank = d->hwirq / 8; 639 if (bank >= ARRAY_SIZE(mvchip->bank_irq)) 640 return -EINVAL; 641 642 irq = mvchip->bank_irq[bank]; 643 if (irq <= 0) 644 return -EINVAL; 645 646 if (enable) 647 return enable_irq_wake(irq); 648 649 return disable_irq_wake(irq); 650 } 651 652 /* 653 * Functions implementing the pwm_chip methods 654 */ 655 static struct mvebu_pwm *to_mvebu_pwm(struct pwm_chip *chip) 656 { 657 return pwmchip_get_drvdata(chip); 658 } 659 660 static int mvebu_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) 661 { 662 struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip); 663 struct mvebu_gpio_chip *mvchip = mvpwm->mvchip; 664 struct gpio_desc *desc; 665 unsigned long flags; 666 int ret = 0; 667 668 spin_lock_irqsave(&mvpwm->lock, flags); 669 670 if (mvpwm->gpiod) { 671 ret = -EBUSY; 672 } else { 673 desc = gpiochip_request_own_desc(&mvchip->chip, 674 pwm->hwpwm, "mvebu-pwm", 675 GPIO_ACTIVE_HIGH, 676 GPIOD_OUT_LOW); 677 if (IS_ERR(desc)) { 678 ret = PTR_ERR(desc); 679 goto out; 680 } 681 682 mvpwm->gpiod = desc; 683 } 684 out: 685 spin_unlock_irqrestore(&mvpwm->lock, flags); 686 return ret; 687 } 688 689 static void mvebu_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) 690 { 691 struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip); 692 unsigned long flags; 693 694 spin_lock_irqsave(&mvpwm->lock, flags); 695 gpiochip_free_own_desc(mvpwm->gpiod); 696 mvpwm->gpiod = NULL; 697 spin_unlock_irqrestore(&mvpwm->lock, flags); 698 } 699 700 static int mvebu_pwm_get_state(struct pwm_chip *chip, 701 struct pwm_device *pwm, 702 struct pwm_state *state) 703 { 704 705 struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip); 706 struct mvebu_gpio_chip *mvchip = mvpwm->mvchip; 707 unsigned long long val; 708 unsigned long flags; 709 u32 u; 710 711 spin_lock_irqsave(&mvpwm->lock, flags); 712 713 regmap_read(mvpwm->regs, mvebu_pwmreg_blink_on_duration(mvpwm), &u); 714 /* Hardware treats zero as 2^32. See mvebu_pwm_apply(). */ 715 if (u > 0) 716 val = u; 717 else 718 val = UINT_MAX + 1ULL; 719 state->duty_cycle = DIV_ROUND_UP_ULL(val * NSEC_PER_SEC, 720 mvpwm->clk_rate); 721 722 regmap_read(mvpwm->regs, mvebu_pwmreg_blink_off_duration(mvpwm), &u); 723 /* period = on + off duration */ 724 if (u > 0) 725 val += u; 726 else 727 val += UINT_MAX + 1ULL; 728 state->period = DIV_ROUND_UP_ULL(val * NSEC_PER_SEC, mvpwm->clk_rate); 729 730 regmap_read(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset, &u); 731 if (u) 732 state->enabled = true; 733 else 734 state->enabled = false; 735 736 spin_unlock_irqrestore(&mvpwm->lock, flags); 737 738 return 0; 739 } 740 741 static int mvebu_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, 742 const struct pwm_state *state) 743 { 744 struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip); 745 struct mvebu_gpio_chip *mvchip = mvpwm->mvchip; 746 unsigned long long val; 747 unsigned long flags; 748 unsigned int on, off; 749 750 if (state->polarity != PWM_POLARITY_NORMAL) 751 return -EINVAL; 752 753 val = (unsigned long long) mvpwm->clk_rate * state->duty_cycle; 754 do_div(val, NSEC_PER_SEC); 755 if (val > UINT_MAX + 1ULL) 756 return -EINVAL; 757 /* 758 * Zero on/off values don't work as expected. Experimentation shows 759 * that zero value is treated as 2^32. This behavior is not documented. 760 */ 761 if (val == UINT_MAX + 1ULL) 762 on = 0; 763 else if (val) 764 on = val; 765 else 766 on = 1; 767 768 val = (unsigned long long) mvpwm->clk_rate * state->period; 769 do_div(val, NSEC_PER_SEC); 770 val -= on; 771 if (val > UINT_MAX + 1ULL) 772 return -EINVAL; 773 if (val == UINT_MAX + 1ULL) 774 off = 0; 775 else if (val) 776 off = val; 777 else 778 off = 1; 779 780 spin_lock_irqsave(&mvpwm->lock, flags); 781 782 regmap_write(mvpwm->regs, mvebu_pwmreg_blink_on_duration(mvpwm), on); 783 regmap_write(mvpwm->regs, mvebu_pwmreg_blink_off_duration(mvpwm), off); 784 if (state->enabled) 785 mvebu_gpio_blink(&mvchip->chip, pwm->hwpwm, 1); 786 else 787 mvebu_gpio_blink(&mvchip->chip, pwm->hwpwm, 0); 788 789 spin_unlock_irqrestore(&mvpwm->lock, flags); 790 791 return 0; 792 } 793 794 static const struct pwm_ops mvebu_pwm_ops = { 795 .request = mvebu_pwm_request, 796 .free = mvebu_pwm_free, 797 .get_state = mvebu_pwm_get_state, 798 .apply = mvebu_pwm_apply, 799 }; 800 801 static void __maybe_unused mvebu_pwm_suspend(struct mvebu_gpio_chip *mvchip) 802 { 803 struct mvebu_pwm *mvpwm = mvchip->mvpwm; 804 805 regmap_read(mvchip->regs, GPIO_BLINK_CNT_SELECT_OFF + mvchip->offset, 806 &mvpwm->blink_select); 807 regmap_read(mvpwm->regs, mvebu_pwmreg_blink_on_duration(mvpwm), 808 &mvpwm->blink_on_duration); 809 regmap_read(mvpwm->regs, mvebu_pwmreg_blink_off_duration(mvpwm), 810 &mvpwm->blink_off_duration); 811 } 812 813 static void __maybe_unused mvebu_pwm_resume(struct mvebu_gpio_chip *mvchip) 814 { 815 struct mvebu_pwm *mvpwm = mvchip->mvpwm; 816 817 regmap_write(mvchip->regs, GPIO_BLINK_CNT_SELECT_OFF + mvchip->offset, 818 mvpwm->blink_select); 819 regmap_write(mvpwm->regs, mvebu_pwmreg_blink_on_duration(mvpwm), 820 mvpwm->blink_on_duration); 821 regmap_write(mvpwm->regs, mvebu_pwmreg_blink_off_duration(mvpwm), 822 mvpwm->blink_off_duration); 823 } 824 825 static int mvebu_pwm_probe(struct platform_device *pdev, 826 struct mvebu_gpio_chip *mvchip, 827 int id) 828 { 829 struct device *dev = &pdev->dev; 830 struct mvebu_pwm *mvpwm; 831 struct pwm_chip *chip; 832 void __iomem *base; 833 u32 offset; 834 u32 set; 835 836 if (mvchip->soc_variant == MVEBU_GPIO_SOC_VARIANT_A8K) { 837 int ret = device_property_read_u32(dev, "marvell,pwm-offset", 838 &offset); 839 if (ret < 0) 840 return 0; 841 } else { 842 /* 843 * There are only two sets of PWM configuration registers for 844 * all the GPIO lines on those SoCs which this driver reserves 845 * for the first two GPIO chips. So if the resource is missing 846 * we can't treat it as an error. 847 */ 848 if (!platform_get_resource_byname(pdev, IORESOURCE_MEM, "pwm")) 849 return 0; 850 offset = 0; 851 } 852 853 if (IS_ERR(mvchip->clk)) 854 return PTR_ERR(mvchip->clk); 855 856 chip = devm_pwmchip_alloc(dev, mvchip->chip.ngpio, sizeof(*mvpwm)); 857 if (IS_ERR(chip)) 858 return PTR_ERR(chip); 859 mvpwm = to_mvebu_pwm(chip); 860 861 mvchip->mvpwm = mvpwm; 862 mvpwm->mvchip = mvchip; 863 mvpwm->offset = offset; 864 865 if (mvchip->soc_variant == MVEBU_GPIO_SOC_VARIANT_A8K) { 866 mvpwm->regs = mvchip->regs; 867 868 switch (mvchip->offset) { 869 case AP80X_GPIO0_OFF_A8K: 870 case CP11X_GPIO0_OFF_A8K: 871 /* Blink counter A */ 872 set = 0; 873 break; 874 case CP11X_GPIO1_OFF_A8K: 875 /* Blink counter B */ 876 set = U32_MAX; 877 mvpwm->offset += PWM_BLINK_COUNTER_B_OFF; 878 break; 879 default: 880 return -EINVAL; 881 } 882 } else { 883 base = devm_platform_ioremap_resource_byname(pdev, "pwm"); 884 if (IS_ERR(base)) 885 return PTR_ERR(base); 886 887 mvpwm->regs = devm_regmap_init_mmio(&pdev->dev, base, 888 &mvebu_pwm_regmap_config); 889 if (IS_ERR(mvpwm->regs)) 890 return PTR_ERR(mvpwm->regs); 891 892 /* 893 * Use set A for lines of GPIO chip with id 0, B for GPIO chip 894 * with id 1. Don't allow further GPIO chips to be used for PWM. 895 */ 896 if (id == 0) 897 set = 0; 898 else if (id == 1) 899 set = U32_MAX; 900 else 901 return -EINVAL; 902 } 903 904 regmap_write(mvchip->regs, 905 GPIO_BLINK_CNT_SELECT_OFF + mvchip->offset, set); 906 907 mvpwm->clk_rate = clk_get_rate(mvchip->clk); 908 if (!mvpwm->clk_rate) { 909 dev_err(dev, "failed to get clock rate\n"); 910 return -EINVAL; 911 } 912 913 chip->ops = &mvebu_pwm_ops; 914 915 spin_lock_init(&mvpwm->lock); 916 917 return devm_pwmchip_add(dev, chip); 918 } 919 920 #ifdef CONFIG_DEBUG_FS 921 #include <linux/seq_file.h> 922 923 static void mvebu_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip) 924 { 925 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip); 926 u32 out, io_conf, blink, in_pol, data_in, cause, edg_msk, lvl_msk; 927 const char *label; 928 int i; 929 930 regmap_read(mvchip->regs, GPIO_OUT_OFF + mvchip->offset, &out); 931 regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset, &io_conf); 932 regmap_read(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset, &blink); 933 regmap_read(mvchip->regs, GPIO_IN_POL_OFF + mvchip->offset, &in_pol); 934 regmap_read(mvchip->regs, GPIO_DATA_IN_OFF + mvchip->offset, &data_in); 935 cause = mvebu_gpio_read_edge_cause(mvchip); 936 edg_msk = mvebu_gpio_read_edge_mask(mvchip); 937 lvl_msk = mvebu_gpio_read_level_mask(mvchip); 938 939 for_each_requested_gpio(chip, i, label) { 940 u32 msk; 941 bool is_out; 942 943 msk = BIT(i); 944 is_out = !(io_conf & msk); 945 946 seq_printf(s, " gpio-%-3d (%-20.20s)", i, label); 947 948 if (is_out) { 949 seq_printf(s, " out %s %s\n", 950 str_hi_lo(out & msk), 951 blink & msk ? "(blink )" : ""); 952 continue; 953 } 954 955 seq_printf(s, " in %s (act %s) - IRQ", 956 str_hi_lo((data_in ^ in_pol) & msk), 957 str_lo_hi(in_pol & msk)); 958 if (!((edg_msk | lvl_msk) & msk)) { 959 seq_puts(s, " disabled\n"); 960 continue; 961 } 962 if (edg_msk & msk) 963 seq_puts(s, " edge "); 964 if (lvl_msk & msk) 965 seq_puts(s, " level"); 966 seq_printf(s, " (%s)\n", cause & msk ? "pending" : "clear "); 967 } 968 } 969 #else 970 #define mvebu_gpio_dbg_show NULL 971 #endif 972 973 static const struct of_device_id mvebu_gpio_of_match[] = { 974 { 975 .compatible = "marvell,orion-gpio", 976 .data = (void *) MVEBU_GPIO_SOC_VARIANT_ORION, 977 }, 978 { 979 .compatible = "marvell,mv78200-gpio", 980 .data = (void *) MVEBU_GPIO_SOC_VARIANT_MV78200, 981 }, 982 { 983 .compatible = "marvell,armadaxp-gpio", 984 .data = (void *) MVEBU_GPIO_SOC_VARIANT_ARMADAXP, 985 }, 986 { 987 .compatible = "marvell,armada-370-gpio", 988 .data = (void *) MVEBU_GPIO_SOC_VARIANT_ORION, 989 }, 990 { 991 .compatible = "marvell,armada-8k-gpio", 992 .data = (void *) MVEBU_GPIO_SOC_VARIANT_A8K, 993 }, 994 { 995 /* sentinel */ 996 }, 997 }; 998 999 static int mvebu_gpio_suspend(struct platform_device *pdev, pm_message_t state) 1000 { 1001 struct mvebu_gpio_chip *mvchip = platform_get_drvdata(pdev); 1002 int i; 1003 1004 regmap_read(mvchip->regs, GPIO_OUT_OFF + mvchip->offset, 1005 &mvchip->out_reg); 1006 regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset, 1007 &mvchip->io_conf_reg); 1008 regmap_read(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset, 1009 &mvchip->blink_en_reg); 1010 regmap_read(mvchip->regs, GPIO_IN_POL_OFF + mvchip->offset, 1011 &mvchip->in_pol_reg); 1012 1013 switch (mvchip->soc_variant) { 1014 case MVEBU_GPIO_SOC_VARIANT_ORION: 1015 case MVEBU_GPIO_SOC_VARIANT_A8K: 1016 regmap_read(mvchip->regs, GPIO_EDGE_MASK_OFF + mvchip->offset, 1017 &mvchip->edge_mask_regs[0]); 1018 regmap_read(mvchip->regs, GPIO_LEVEL_MASK_OFF + mvchip->offset, 1019 &mvchip->level_mask_regs[0]); 1020 break; 1021 case MVEBU_GPIO_SOC_VARIANT_MV78200: 1022 for (i = 0; i < 2; i++) { 1023 regmap_read(mvchip->regs, 1024 GPIO_EDGE_MASK_MV78200_OFF(i), 1025 &mvchip->edge_mask_regs[i]); 1026 regmap_read(mvchip->regs, 1027 GPIO_LEVEL_MASK_MV78200_OFF(i), 1028 &mvchip->level_mask_regs[i]); 1029 } 1030 break; 1031 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP: 1032 for (i = 0; i < 4; i++) { 1033 regmap_read(mvchip->percpu_regs, 1034 GPIO_EDGE_MASK_ARMADAXP_OFF(i), 1035 &mvchip->edge_mask_regs[i]); 1036 regmap_read(mvchip->percpu_regs, 1037 GPIO_LEVEL_MASK_ARMADAXP_OFF(i), 1038 &mvchip->level_mask_regs[i]); 1039 } 1040 break; 1041 default: 1042 BUG(); 1043 } 1044 1045 if (IS_REACHABLE(CONFIG_PWM) && mvchip->mvpwm) 1046 mvebu_pwm_suspend(mvchip); 1047 1048 return 0; 1049 } 1050 1051 static int mvebu_gpio_resume(struct platform_device *pdev) 1052 { 1053 struct mvebu_gpio_chip *mvchip = platform_get_drvdata(pdev); 1054 u32 edge_cache = ~0U, level_cache = ~0U; 1055 unsigned long flags; 1056 int i; 1057 1058 regmap_write(mvchip->regs, GPIO_OUT_OFF + mvchip->offset, 1059 mvchip->out_reg); 1060 regmap_write(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset, 1061 mvchip->io_conf_reg); 1062 regmap_write(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset, 1063 mvchip->blink_en_reg); 1064 regmap_write(mvchip->regs, GPIO_IN_POL_OFF + mvchip->offset, 1065 mvchip->in_pol_reg); 1066 1067 /* 1068 * genirq skips mask_irq() for a line it already considers masked, so 1069 * unmasking one behind its back leaves an asserted level line that 1070 * nobody masks. Restore only bits the irqchip cache still has set. 1071 * 1072 * Snapshot the caches under the raw spinlock, but release it before 1073 * the regmap writes below: regmap_write() takes a sleepable lock on 1074 * PREEMPT_RT. 1075 */ 1076 if (mvchip->domain) { 1077 struct irq_chip_generic *gc; 1078 1079 gc = irq_get_domain_generic_chip(mvchip->domain, 0); 1080 raw_spin_lock_irqsave(&gc->lock, flags); 1081 level_cache = gc->chip_types[0].mask_cache_priv; 1082 edge_cache = gc->chip_types[1].mask_cache_priv; 1083 raw_spin_unlock_irqrestore(&gc->lock, flags); 1084 } 1085 1086 switch (mvchip->soc_variant) { 1087 case MVEBU_GPIO_SOC_VARIANT_ORION: 1088 case MVEBU_GPIO_SOC_VARIANT_A8K: 1089 regmap_write(mvchip->regs, GPIO_EDGE_MASK_OFF + mvchip->offset, 1090 mvchip->edge_mask_regs[0] & edge_cache); 1091 regmap_write(mvchip->regs, GPIO_LEVEL_MASK_OFF + mvchip->offset, 1092 mvchip->level_mask_regs[0] & level_cache); 1093 break; 1094 case MVEBU_GPIO_SOC_VARIANT_MV78200: 1095 for (i = 0; i < 2; i++) { 1096 regmap_write(mvchip->regs, 1097 GPIO_EDGE_MASK_MV78200_OFF(i), 1098 mvchip->edge_mask_regs[i] & edge_cache); 1099 regmap_write(mvchip->regs, 1100 GPIO_LEVEL_MASK_MV78200_OFF(i), 1101 mvchip->level_mask_regs[i] & level_cache); 1102 } 1103 break; 1104 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP: 1105 for (i = 0; i < 4; i++) { 1106 regmap_write(mvchip->percpu_regs, 1107 GPIO_EDGE_MASK_ARMADAXP_OFF(i), 1108 mvchip->edge_mask_regs[i] & edge_cache); 1109 regmap_write(mvchip->percpu_regs, 1110 GPIO_LEVEL_MASK_ARMADAXP_OFF(i), 1111 mvchip->level_mask_regs[i] & level_cache); 1112 } 1113 break; 1114 default: 1115 BUG(); 1116 } 1117 1118 if (IS_REACHABLE(CONFIG_PWM) && mvchip->mvpwm) 1119 mvebu_pwm_resume(mvchip); 1120 1121 return 0; 1122 } 1123 1124 static int mvebu_gpio_probe_raw(struct platform_device *pdev, 1125 struct mvebu_gpio_chip *mvchip) 1126 { 1127 void __iomem *base; 1128 1129 base = devm_platform_ioremap_resource(pdev, 0); 1130 if (IS_ERR(base)) 1131 return PTR_ERR(base); 1132 1133 mvchip->regs = devm_regmap_init_mmio(&pdev->dev, base, 1134 &mvebu_gpio_regmap_config); 1135 if (IS_ERR(mvchip->regs)) 1136 return PTR_ERR(mvchip->regs); 1137 1138 /* 1139 * For the legacy SoCs, the regmap directly maps to the GPIO 1140 * registers, so no offset is needed. 1141 */ 1142 mvchip->offset = 0; 1143 1144 /* 1145 * The Armada XP has a second range of registers for the 1146 * per-CPU registers 1147 */ 1148 if (mvchip->soc_variant == MVEBU_GPIO_SOC_VARIANT_ARMADAXP) { 1149 base = devm_platform_ioremap_resource(pdev, 1); 1150 if (IS_ERR(base)) 1151 return PTR_ERR(base); 1152 1153 mvchip->percpu_regs = 1154 devm_regmap_init_mmio(&pdev->dev, base, 1155 &mvebu_gpio_percpu_regmap_config); 1156 if (IS_ERR(mvchip->percpu_regs)) 1157 return PTR_ERR(mvchip->percpu_regs); 1158 } 1159 1160 return 0; 1161 } 1162 1163 static int mvebu_gpio_probe_syscon(struct platform_device *pdev, 1164 struct mvebu_gpio_chip *mvchip) 1165 { 1166 mvchip->regs = syscon_node_to_regmap(pdev->dev.parent->of_node); 1167 if (IS_ERR(mvchip->regs)) 1168 return PTR_ERR(mvchip->regs); 1169 1170 if (device_property_read_u32(&pdev->dev, "offset", &mvchip->offset)) 1171 return -EINVAL; 1172 1173 return 0; 1174 } 1175 1176 static void mvebu_gpio_remove_irq_domain(void *data) 1177 { 1178 struct irq_domain *domain = data; 1179 1180 irq_domain_remove_generic_chips(domain); 1181 irq_domain_remove(domain); 1182 } 1183 1184 static int mvebu_gpio_probe(struct platform_device *pdev) 1185 { 1186 struct mvebu_gpio_chip *mvchip; 1187 struct device_node *np = pdev->dev.of_node; 1188 struct irq_chip_generic *gc; 1189 struct irq_chip_type *ct; 1190 unsigned int ngpios; 1191 bool have_irqs; 1192 int soc_variant; 1193 int i, cpu, id; 1194 int err; 1195 1196 soc_variant = (unsigned long)device_get_match_data(&pdev->dev); 1197 1198 /* Some gpio controllers do not provide irq support */ 1199 err = platform_irq_count(pdev); 1200 if (err < 0) 1201 return err; 1202 1203 have_irqs = err != 0; 1204 1205 mvchip = devm_kzalloc_obj(&pdev->dev, struct mvebu_gpio_chip); 1206 if (!mvchip) 1207 return -ENOMEM; 1208 1209 platform_set_drvdata(pdev, mvchip); 1210 1211 if (device_property_read_u32(&pdev->dev, "ngpios", &ngpios)) { 1212 dev_err(&pdev->dev, "Missing ngpios OF property\n"); 1213 return -ENODEV; 1214 } 1215 1216 if (ngpios == 0 || ngpios > MVEBU_MAX_GPIO_PER_BANK) 1217 return dev_err_probe(&pdev->dev, -EINVAL, "ngpios must be between 1 and 32\n"); 1218 1219 id = of_alias_get_id(pdev->dev.of_node, "gpio"); 1220 if (id < 0) { 1221 dev_err(&pdev->dev, "Couldn't get OF id\n"); 1222 return id; 1223 } 1224 1225 mvchip->clk = devm_clk_get(&pdev->dev, NULL); 1226 /* Not all SoCs require a clock.*/ 1227 if (!IS_ERR(mvchip->clk)) 1228 clk_prepare_enable(mvchip->clk); 1229 1230 mvchip->soc_variant = soc_variant; 1231 mvchip->chip.label = dev_name(&pdev->dev); 1232 mvchip->chip.parent = &pdev->dev; 1233 mvchip->chip.request = gpiochip_generic_request; 1234 mvchip->chip.free = gpiochip_generic_free; 1235 mvchip->chip.get_direction = mvebu_gpio_get_direction; 1236 mvchip->chip.direction_input = mvebu_gpio_direction_input; 1237 mvchip->chip.get = mvebu_gpio_get; 1238 mvchip->chip.direction_output = mvebu_gpio_direction_output; 1239 mvchip->chip.set = mvebu_gpio_set; 1240 if (have_irqs) 1241 mvchip->chip.to_irq = mvebu_gpio_to_irq; 1242 mvchip->chip.base = id * MVEBU_MAX_GPIO_PER_BANK; 1243 mvchip->chip.ngpio = ngpios; 1244 mvchip->chip.can_sleep = false; 1245 mvchip->chip.dbg_show = mvebu_gpio_dbg_show; 1246 1247 if (soc_variant == MVEBU_GPIO_SOC_VARIANT_A8K) 1248 err = mvebu_gpio_probe_syscon(pdev, mvchip); 1249 else 1250 err = mvebu_gpio_probe_raw(pdev, mvchip); 1251 1252 if (err) 1253 return err; 1254 1255 /* 1256 * Mask and clear GPIO interrupts. 1257 */ 1258 switch (soc_variant) { 1259 case MVEBU_GPIO_SOC_VARIANT_ORION: 1260 case MVEBU_GPIO_SOC_VARIANT_A8K: 1261 regmap_write(mvchip->regs, 1262 GPIO_EDGE_CAUSE_OFF + mvchip->offset, 0); 1263 regmap_write(mvchip->regs, 1264 GPIO_EDGE_MASK_OFF + mvchip->offset, 0); 1265 regmap_write(mvchip->regs, 1266 GPIO_LEVEL_MASK_OFF + mvchip->offset, 0); 1267 break; 1268 case MVEBU_GPIO_SOC_VARIANT_MV78200: 1269 regmap_write(mvchip->regs, GPIO_EDGE_CAUSE_OFF, 0); 1270 for (cpu = 0; cpu < 2; cpu++) { 1271 regmap_write(mvchip->regs, 1272 GPIO_EDGE_MASK_MV78200_OFF(cpu), 0); 1273 regmap_write(mvchip->regs, 1274 GPIO_LEVEL_MASK_MV78200_OFF(cpu), 0); 1275 } 1276 break; 1277 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP: 1278 regmap_write(mvchip->regs, GPIO_EDGE_CAUSE_OFF, 0); 1279 regmap_write(mvchip->regs, GPIO_EDGE_MASK_OFF, 0); 1280 regmap_write(mvchip->regs, GPIO_LEVEL_MASK_OFF, 0); 1281 for (cpu = 0; cpu < 4; cpu++) { 1282 regmap_write(mvchip->percpu_regs, 1283 GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu), 0); 1284 regmap_write(mvchip->percpu_regs, 1285 GPIO_EDGE_MASK_ARMADAXP_OFF(cpu), 0); 1286 regmap_write(mvchip->percpu_regs, 1287 GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu), 0); 1288 } 1289 break; 1290 default: 1291 BUG(); 1292 } 1293 1294 err = devm_gpiochip_add_data(&pdev->dev, &mvchip->chip, mvchip); 1295 if (err) 1296 return dev_err_probe(&pdev->dev, err, 1297 "failed to register gpiochip\n"); 1298 1299 /* Some MVEBU SoCs have simple PWM support for GPIO lines */ 1300 if (IS_REACHABLE(CONFIG_PWM)) { 1301 err = mvebu_pwm_probe(pdev, mvchip, id); 1302 if (err) 1303 return err; 1304 } 1305 1306 /* Some gpio controllers do not provide irq support */ 1307 if (!have_irqs) 1308 return 0; 1309 1310 mvchip->domain = irq_domain_create_linear(dev_fwnode(&pdev->dev), ngpios, 1311 &irq_generic_chip_ops, NULL); 1312 if (!mvchip->domain) { 1313 dev_err(&pdev->dev, "couldn't allocate irq domain %s (DT).\n", 1314 mvchip->chip.label); 1315 return -ENODEV; 1316 } 1317 1318 err = devm_add_action_or_reset(&pdev->dev, mvebu_gpio_remove_irq_domain, 1319 mvchip->domain); 1320 if (err) 1321 return err; 1322 1323 err = irq_alloc_domain_generic_chips( 1324 mvchip->domain, ngpios, 2, np->name, handle_level_irq, 1325 IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_LEVEL, 0, IRQ_GC_INIT_NESTED_LOCK); 1326 if (err) { 1327 dev_err(&pdev->dev, "couldn't allocate irq chips %s (DT).\n", 1328 mvchip->chip.label); 1329 return err; 1330 } 1331 1332 /* 1333 * NOTE: The common accessors cannot be used because of the percpu 1334 * access to the mask registers 1335 */ 1336 gc = irq_get_domain_generic_chip(mvchip->domain, 0); 1337 gc->private = mvchip; 1338 ct = &gc->chip_types[0]; 1339 ct->type = IRQ_TYPE_LEVEL_MASK; 1340 ct->chip.irq_mask = mvebu_gpio_level_irq_mask; 1341 ct->chip.irq_unmask = mvebu_gpio_level_irq_unmask; 1342 ct->chip.irq_set_type = mvebu_gpio_irq_set_type; 1343 ct->chip.irq_set_wake = mvebu_gpio_set_wake_irq; 1344 ct->chip.flags = IRQCHIP_SET_TYPE_MASKED | IRQCHIP_MASK_ON_SUSPEND; 1345 ct->chip.name = mvchip->chip.label; 1346 1347 ct = &gc->chip_types[1]; 1348 ct->type = IRQ_TYPE_EDGE_BOTH; 1349 ct->chip.irq_ack = mvebu_gpio_irq_ack; 1350 ct->chip.irq_mask = mvebu_gpio_edge_irq_mask; 1351 ct->chip.irq_unmask = mvebu_gpio_edge_irq_unmask; 1352 ct->chip.irq_set_type = mvebu_gpio_irq_set_type; 1353 ct->chip.irq_set_wake = mvebu_gpio_set_wake_irq; 1354 ct->chip.flags = IRQCHIP_SET_TYPE_MASKED | IRQCHIP_MASK_ON_SUSPEND; 1355 ct->handler = handle_edge_irq; 1356 ct->chip.name = mvchip->chip.label; 1357 1358 /* 1359 * Setup the interrupt handlers. Each chip can have up to 4 1360 * interrupt handlers, with each handler dealing with 8 GPIO 1361 * pins. 1362 */ 1363 for (i = 0; i < ARRAY_SIZE(mvchip->bank_irq); i++) { 1364 int irq = platform_get_irq_optional(pdev, i); 1365 1366 if (irq < 0) 1367 continue; 1368 irq_set_chained_handler_and_data(irq, mvebu_gpio_irq_handler, 1369 mvchip); 1370 mvchip->bank_irq[i] = irq; 1371 } 1372 1373 return 0; 1374 } 1375 1376 static struct platform_driver mvebu_gpio_driver = { 1377 .driver = { 1378 .name = "mvebu-gpio", 1379 .of_match_table = mvebu_gpio_of_match, 1380 }, 1381 .probe = mvebu_gpio_probe, 1382 .suspend = mvebu_gpio_suspend, 1383 .resume = mvebu_gpio_resume, 1384 }; 1385 builtin_platform_driver(mvebu_gpio_driver); 1386