1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2013 MundoReader S.L. 4 * Author: Heiko Stuebner <heiko@sntech.de> 5 * 6 * Copyright (c) 2021 Rockchip Electronics Co. Ltd. 7 */ 8 9 #include <linux/bitops.h> 10 #include <linux/clk.h> 11 #include <linux/device.h> 12 #include <linux/err.h> 13 #include <linux/gpio/driver.h> 14 #include <linux/init.h> 15 #include <linux/interrupt.h> 16 #include <linux/io.h> 17 #include <linux/module.h> 18 #include <linux/of.h> 19 #include <linux/of_address.h> 20 #include <linux/of_irq.h> 21 #include <linux/pinctrl/pinconf-generic.h> 22 #include <linux/platform_device.h> 23 #include <linux/regmap.h> 24 25 #include "../pinctrl/core.h" 26 #include "../pinctrl/pinctrl-rockchip.h" 27 28 /* 29 * Version ID Register 30 * Bits [31:24] - Major Version 31 * Bits [23:16] - Minor Version 32 * Bits [15:0] - Revision Number 33 */ 34 #define GPIO_TYPE_V1 (0) /* GPIO Version ID reserved */ 35 #define GPIO_TYPE_V2 (0x01000C2B) 36 #define GPIO_TYPE_V2_1 (0x0101157C) 37 #define GPIO_TYPE_V2_2 (0x010219C8) 38 39 static const struct rockchip_gpio_regs gpio_regs_v1 = { 40 .port_dr = 0x00, 41 .port_ddr = 0x04, 42 .int_en = 0x30, 43 .int_mask = 0x34, 44 .int_type = 0x38, 45 .int_polarity = 0x3c, 46 .int_status = 0x40, 47 .int_rawstatus = 0x44, 48 .debounce = 0x48, 49 .port_eoi = 0x4c, 50 .ext_port = 0x50, 51 }; 52 53 static const struct rockchip_gpio_regs gpio_regs_v2 = { 54 .port_dr = 0x00, 55 .port_ddr = 0x08, 56 .int_en = 0x10, 57 .int_mask = 0x18, 58 .int_type = 0x20, 59 .int_polarity = 0x28, 60 .int_bothedge = 0x30, 61 .int_status = 0x50, 62 .int_rawstatus = 0x58, 63 .debounce = 0x38, 64 .dbclk_div_en = 0x40, 65 .dbclk_div_con = 0x48, 66 .port_eoi = 0x60, 67 .ext_port = 0x70, 68 .version_id = 0x78, 69 }; 70 71 static inline void gpio_writel_v2(u32 val, void __iomem *reg) 72 { 73 writel((val & 0xffff) | 0xffff0000, reg); 74 writel((val >> 16) | 0xffff0000, reg + 0x4); 75 } 76 77 static inline u32 gpio_readl_v2(void __iomem *reg) 78 { 79 return readl(reg + 0x4) << 16 | readl(reg); 80 } 81 82 static inline void rockchip_gpio_writel(struct rockchip_pin_bank *bank, 83 u32 value, unsigned int offset) 84 { 85 void __iomem *reg = bank->reg_base + offset; 86 87 if (bank->gpio_type == GPIO_TYPE_V2) 88 gpio_writel_v2(value, reg); 89 else 90 writel(value, reg); 91 } 92 93 static inline u32 rockchip_gpio_readl(struct rockchip_pin_bank *bank, 94 unsigned int offset) 95 { 96 void __iomem *reg = bank->reg_base + offset; 97 u32 value; 98 99 if (bank->gpio_type == GPIO_TYPE_V2) 100 value = gpio_readl_v2(reg); 101 else 102 value = readl(reg); 103 104 return value; 105 } 106 107 static inline void rockchip_gpio_writel_bit(struct rockchip_pin_bank *bank, 108 u32 bit, u32 value, 109 unsigned int offset) 110 { 111 void __iomem *reg = bank->reg_base + offset; 112 u32 data; 113 114 if (bank->gpio_type == GPIO_TYPE_V2) { 115 if (value) 116 data = BIT(bit % 16) | BIT(bit % 16 + 16); 117 else 118 data = BIT(bit % 16 + 16); 119 writel(data, bit >= 16 ? reg + 0x4 : reg); 120 } else { 121 data = readl(reg); 122 data &= ~BIT(bit); 123 if (value) 124 data |= BIT(bit); 125 writel(data, reg); 126 } 127 } 128 129 static inline u32 rockchip_gpio_readl_bit(struct rockchip_pin_bank *bank, 130 u32 bit, unsigned int offset) 131 { 132 void __iomem *reg = bank->reg_base + offset; 133 u32 data; 134 135 if (bank->gpio_type == GPIO_TYPE_V2) { 136 data = readl(bit >= 16 ? reg + 0x4 : reg); 137 data >>= bit % 16; 138 } else { 139 data = readl(reg); 140 data >>= bit; 141 } 142 143 return data & (0x1); 144 } 145 146 static int rockchip_gpio_get_direction(struct gpio_chip *chip, 147 unsigned int offset) 148 { 149 struct rockchip_pin_bank *bank = gpiochip_get_data(chip); 150 u32 data; 151 152 data = rockchip_gpio_readl_bit(bank, offset, bank->gpio_regs->port_ddr); 153 if (data) 154 return GPIO_LINE_DIRECTION_OUT; 155 156 return GPIO_LINE_DIRECTION_IN; 157 } 158 159 static int rockchip_gpio_set_direction(struct gpio_chip *chip, 160 unsigned int offset, bool input) 161 { 162 struct rockchip_pin_bank *bank = gpiochip_get_data(chip); 163 unsigned long flags; 164 u32 data = input ? 0 : 1; 165 166 raw_spin_lock_irqsave(&bank->slock, flags); 167 rockchip_gpio_writel_bit(bank, offset, data, bank->gpio_regs->port_ddr); 168 raw_spin_unlock_irqrestore(&bank->slock, flags); 169 170 return 0; 171 } 172 173 static int rockchip_gpio_set(struct gpio_chip *gc, unsigned int offset, 174 int value) 175 { 176 struct rockchip_pin_bank *bank = gpiochip_get_data(gc); 177 unsigned long flags; 178 179 raw_spin_lock_irqsave(&bank->slock, flags); 180 rockchip_gpio_writel_bit(bank, offset, value, bank->gpio_regs->port_dr); 181 raw_spin_unlock_irqrestore(&bank->slock, flags); 182 183 return 0; 184 } 185 186 static int rockchip_gpio_get(struct gpio_chip *gc, unsigned int offset) 187 { 188 struct rockchip_pin_bank *bank = gpiochip_get_data(gc); 189 u32 data; 190 191 data = readl(bank->reg_base + bank->gpio_regs->ext_port); 192 data >>= offset; 193 data &= 1; 194 195 return data; 196 } 197 198 static int rockchip_gpio_set_debounce(struct gpio_chip *gc, 199 unsigned int offset, 200 unsigned int debounce) 201 { 202 struct rockchip_pin_bank *bank = gpiochip_get_data(gc); 203 const struct rockchip_gpio_regs *reg = bank->gpio_regs; 204 unsigned long flags, div_reg, freq; 205 bool div_debounce_support; 206 unsigned int cur_div_reg; 207 u64 div, max_debounce; 208 209 if (bank->gpio_type == GPIO_TYPE_V2 && !IS_ERR(bank->db_clk)) { 210 div_debounce_support = true; 211 freq = clk_get_rate(bank->db_clk); 212 if (!freq) 213 return -EINVAL; 214 215 div = (u64)(GENMASK(23, 0) + 1) * 2 * USEC_PER_SEC; 216 max_debounce = div64_u64(div, freq); 217 if (debounce > max_debounce) 218 return -EINVAL; 219 220 div = (u64)debounce * freq; 221 div_reg = DIV_ROUND_CLOSEST_ULL(div, 2 * USEC_PER_SEC); 222 div_reg = div_reg ? div_reg - 1 : 0; 223 } else { 224 div_debounce_support = false; 225 } 226 227 raw_spin_lock_irqsave(&bank->slock, flags); 228 229 /* Only the v1 needs to configure div_en and div_con for dbclk */ 230 if (debounce) { 231 if (div_debounce_support) { 232 /* Configure the max debounce from consumers */ 233 cur_div_reg = readl(bank->reg_base + 234 reg->dbclk_div_con); 235 if (cur_div_reg < div_reg) 236 writel(div_reg, bank->reg_base + 237 reg->dbclk_div_con); 238 rockchip_gpio_writel_bit(bank, offset, 1, 239 reg->dbclk_div_en); 240 } 241 242 rockchip_gpio_writel_bit(bank, offset, 1, reg->debounce); 243 } else { 244 if (div_debounce_support) 245 rockchip_gpio_writel_bit(bank, offset, 0, 246 reg->dbclk_div_en); 247 248 rockchip_gpio_writel_bit(bank, offset, 0, reg->debounce); 249 } 250 251 raw_spin_unlock_irqrestore(&bank->slock, flags); 252 253 /* Enable or disable dbclk at last */ 254 if (div_debounce_support) { 255 if (debounce) 256 clk_prepare_enable(bank->db_clk); 257 else 258 clk_disable_unprepare(bank->db_clk); 259 } 260 261 return 0; 262 } 263 264 static int rockchip_gpio_direction_input(struct gpio_chip *gc, 265 unsigned int offset) 266 { 267 return rockchip_gpio_set_direction(gc, offset, true); 268 } 269 270 static int rockchip_gpio_direction_output(struct gpio_chip *gc, 271 unsigned int offset, int value) 272 { 273 rockchip_gpio_set(gc, offset, value); 274 275 return rockchip_gpio_set_direction(gc, offset, false); 276 } 277 278 /* 279 * gpiolib set_config callback function. The setting of the pin 280 * mux function as 'gpio output' will be handled by the pinctrl subsystem 281 * interface. 282 */ 283 static int rockchip_gpio_set_config(struct gpio_chip *gc, unsigned int offset, 284 unsigned long config) 285 { 286 enum pin_config_param param = pinconf_to_config_param(config); 287 288 switch (param) { 289 case PIN_CONFIG_INPUT_DEBOUNCE: 290 rockchip_gpio_set_debounce(gc, offset, true); 291 /* 292 * Rockchip's gpio could only support up to one period 293 * of the debounce clock(pclk), which is far away from 294 * satisftying the requirement, as pclk is usually near 295 * 100MHz shared by all peripherals. So the fact is it 296 * has crippled debounce capability could only be useful 297 * to prevent any spurious glitches from waking up the system 298 * if the gpio is conguired as wakeup interrupt source. Let's 299 * still return -ENOTSUPP as before, to make sure the caller 300 * of gpiod_set_debounce won't change its behaviour. 301 */ 302 return -ENOTSUPP; 303 default: 304 return gpiochip_generic_config(gc, offset, config); 305 } 306 } 307 308 /* 309 * gpiod_to_irq() callback function. Creates a mapping between a GPIO pin 310 * and a virtual IRQ, if not already present. 311 */ 312 static int rockchip_gpio_to_irq(struct gpio_chip *gc, unsigned int offset) 313 { 314 struct rockchip_pin_bank *bank = gpiochip_get_data(gc); 315 unsigned int virq; 316 317 if (!bank->domain) 318 return -ENXIO; 319 320 virq = irq_create_mapping(bank->domain, offset); 321 322 return (virq) ? : -ENXIO; 323 } 324 325 static const struct gpio_chip rockchip_gpiolib_chip = { 326 .request = gpiochip_generic_request, 327 .free = gpiochip_generic_free, 328 .set = rockchip_gpio_set, 329 .get = rockchip_gpio_get, 330 .get_direction = rockchip_gpio_get_direction, 331 .direction_input = rockchip_gpio_direction_input, 332 .direction_output = rockchip_gpio_direction_output, 333 .set_config = rockchip_gpio_set_config, 334 .to_irq = rockchip_gpio_to_irq, 335 .owner = THIS_MODULE, 336 }; 337 338 static void rockchip_irq_demux(struct irq_desc *desc) 339 { 340 struct irq_chip *chip = irq_desc_get_chip(desc); 341 struct rockchip_pin_bank *bank = irq_desc_get_handler_data(desc); 342 unsigned long pending; 343 unsigned int irq; 344 345 dev_dbg(bank->dev, "got irq for bank %s\n", bank->name); 346 347 chained_irq_enter(chip, desc); 348 349 pending = readl_relaxed(bank->reg_base + bank->gpio_regs->int_status); 350 for_each_set_bit(irq, &pending, 32) { 351 dev_dbg(bank->dev, "handling irq %d\n", irq); 352 353 /* 354 * Triggering IRQ on both rising and falling edge 355 * needs manual intervention. 356 */ 357 if (bank->toggle_edge_mode & BIT(irq)) { 358 u32 data, data_old, polarity; 359 unsigned long flags; 360 361 data = readl_relaxed(bank->reg_base + 362 bank->gpio_regs->ext_port); 363 do { 364 raw_spin_lock_irqsave(&bank->slock, flags); 365 366 polarity = readl_relaxed(bank->reg_base + 367 bank->gpio_regs->int_polarity); 368 if (data & BIT(irq)) 369 polarity &= ~BIT(irq); 370 else 371 polarity |= BIT(irq); 372 writel(polarity, 373 bank->reg_base + 374 bank->gpio_regs->int_polarity); 375 376 raw_spin_unlock_irqrestore(&bank->slock, flags); 377 378 data_old = data; 379 data = readl_relaxed(bank->reg_base + 380 bank->gpio_regs->ext_port); 381 } while ((data & BIT(irq)) != (data_old & BIT(irq))); 382 } 383 384 generic_handle_domain_irq(bank->domain, irq); 385 } 386 387 chained_irq_exit(chip, desc); 388 } 389 390 static int rockchip_irq_set_type(struct irq_data *d, unsigned int type) 391 { 392 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 393 struct rockchip_pin_bank *bank = gc->private; 394 u32 mask = BIT(d->hwirq); 395 u32 polarity; 396 u32 level; 397 u32 data; 398 unsigned long flags; 399 int ret = 0; 400 401 raw_spin_lock_irqsave(&bank->slock, flags); 402 403 rockchip_gpio_writel_bit(bank, d->hwirq, 0, 404 bank->gpio_regs->port_ddr); 405 406 raw_spin_unlock_irqrestore(&bank->slock, flags); 407 408 if (type & IRQ_TYPE_EDGE_BOTH) 409 irq_set_handler_locked(d, handle_edge_irq); 410 else 411 irq_set_handler_locked(d, handle_level_irq); 412 413 raw_spin_lock_irqsave(&bank->slock, flags); 414 415 level = rockchip_gpio_readl(bank, bank->gpio_regs->int_type); 416 polarity = rockchip_gpio_readl(bank, bank->gpio_regs->int_polarity); 417 418 if (type == IRQ_TYPE_EDGE_BOTH) { 419 if (bank->gpio_type == GPIO_TYPE_V2) { 420 rockchip_gpio_writel_bit(bank, d->hwirq, 1, 421 bank->gpio_regs->int_bothedge); 422 goto out; 423 } else { 424 bank->toggle_edge_mode |= mask; 425 level &= ~mask; 426 427 /* 428 * Determine gpio state. If 1 next interrupt should be 429 * low otherwise high. 430 */ 431 data = readl(bank->reg_base + bank->gpio_regs->ext_port); 432 if (data & mask) 433 polarity &= ~mask; 434 else 435 polarity |= mask; 436 } 437 } else { 438 if (bank->gpio_type == GPIO_TYPE_V2) { 439 rockchip_gpio_writel_bit(bank, d->hwirq, 0, 440 bank->gpio_regs->int_bothedge); 441 } else { 442 bank->toggle_edge_mode &= ~mask; 443 } 444 switch (type) { 445 case IRQ_TYPE_EDGE_RISING: 446 level |= mask; 447 polarity |= mask; 448 break; 449 case IRQ_TYPE_EDGE_FALLING: 450 level |= mask; 451 polarity &= ~mask; 452 break; 453 case IRQ_TYPE_LEVEL_HIGH: 454 level &= ~mask; 455 polarity |= mask; 456 break; 457 case IRQ_TYPE_LEVEL_LOW: 458 level &= ~mask; 459 polarity &= ~mask; 460 break; 461 default: 462 ret = -EINVAL; 463 goto out; 464 } 465 } 466 467 rockchip_gpio_writel(bank, level, bank->gpio_regs->int_type); 468 rockchip_gpio_writel(bank, polarity, bank->gpio_regs->int_polarity); 469 out: 470 raw_spin_unlock_irqrestore(&bank->slock, flags); 471 472 return ret; 473 } 474 475 static int rockchip_irq_reqres(struct irq_data *d) 476 { 477 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 478 struct rockchip_pin_bank *bank = gc->private; 479 480 return gpiochip_reqres_irq(&bank->gpio_chip, d->hwirq); 481 } 482 483 static void rockchip_irq_relres(struct irq_data *d) 484 { 485 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 486 struct rockchip_pin_bank *bank = gc->private; 487 488 gpiochip_relres_irq(&bank->gpio_chip, d->hwirq); 489 } 490 491 static void rockchip_irq_suspend(struct irq_data *d) 492 { 493 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 494 struct rockchip_pin_bank *bank = gc->private; 495 496 bank->saved_masks = irq_reg_readl(gc, bank->gpio_regs->int_mask); 497 irq_reg_writel(gc, ~gc->wake_active, bank->gpio_regs->int_mask); 498 } 499 500 static void rockchip_irq_resume(struct irq_data *d) 501 { 502 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 503 struct rockchip_pin_bank *bank = gc->private; 504 505 irq_reg_writel(gc, bank->saved_masks, bank->gpio_regs->int_mask); 506 } 507 508 static void rockchip_irq_enable(struct irq_data *d) 509 { 510 irq_gc_mask_clr_bit(d); 511 } 512 513 static void rockchip_irq_disable(struct irq_data *d) 514 { 515 irq_gc_mask_set_bit(d); 516 } 517 518 static int rockchip_interrupts_register(struct rockchip_pin_bank *bank) 519 { 520 unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN; 521 struct irq_chip_generic *gc; 522 int ret; 523 524 bank->domain = irq_domain_create_linear(dev_fwnode(bank->dev), 32, &irq_generic_chip_ops, 525 NULL); 526 if (!bank->domain) { 527 dev_warn(bank->dev, "could not init irq domain for bank %s\n", 528 bank->name); 529 return -EINVAL; 530 } 531 532 ret = irq_alloc_domain_generic_chips(bank->domain, 32, 1, 533 "rockchip_gpio_irq", 534 handle_level_irq, 535 clr, 0, 0); 536 if (ret) { 537 dev_err(bank->dev, "could not alloc generic chips for bank %s\n", 538 bank->name); 539 irq_domain_remove(bank->domain); 540 return -EINVAL; 541 } 542 543 gc = irq_get_domain_generic_chip(bank->domain, 0); 544 if (bank->gpio_type == GPIO_TYPE_V2) { 545 gc->reg_writel = gpio_writel_v2; 546 gc->reg_readl = gpio_readl_v2; 547 } 548 549 gc->reg_base = bank->reg_base; 550 gc->private = bank; 551 gc->chip_types[0].regs.mask = bank->gpio_regs->int_mask; 552 gc->chip_types[0].regs.ack = bank->gpio_regs->port_eoi; 553 gc->chip_types[0].chip.irq_ack = irq_gc_ack_set_bit; 554 gc->chip_types[0].chip.irq_mask = irq_gc_mask_set_bit; 555 gc->chip_types[0].chip.irq_unmask = irq_gc_mask_clr_bit; 556 gc->chip_types[0].chip.irq_enable = rockchip_irq_enable; 557 gc->chip_types[0].chip.irq_disable = rockchip_irq_disable; 558 gc->chip_types[0].chip.irq_set_wake = irq_gc_set_wake; 559 gc->chip_types[0].chip.irq_suspend = rockchip_irq_suspend; 560 gc->chip_types[0].chip.irq_resume = rockchip_irq_resume; 561 gc->chip_types[0].chip.irq_set_type = rockchip_irq_set_type; 562 gc->chip_types[0].chip.irq_request_resources = rockchip_irq_reqres; 563 gc->chip_types[0].chip.irq_release_resources = rockchip_irq_relres; 564 gc->wake_enabled = IRQ_MSK(bank->nr_pins); 565 566 /* 567 * Linux assumes that all interrupts start out disabled/masked. 568 * Our driver only uses the concept of masked and always keeps 569 * things enabled, so for us that's all masked and all enabled. 570 */ 571 rockchip_gpio_writel(bank, 0xffffffff, bank->gpio_regs->int_mask); 572 rockchip_gpio_writel(bank, 0xffffffff, bank->gpio_regs->port_eoi); 573 rockchip_gpio_writel(bank, 0xffffffff, bank->gpio_regs->int_en); 574 gc->mask_cache = 0xffffffff; 575 576 irq_set_chained_handler_and_data(bank->irq, 577 rockchip_irq_demux, bank); 578 579 return 0; 580 } 581 582 static int rockchip_gpiolib_register(struct rockchip_pin_bank *bank) 583 { 584 struct gpio_chip *gc; 585 int ret; 586 587 bank->gpio_chip = rockchip_gpiolib_chip; 588 589 gc = &bank->gpio_chip; 590 gc->base = -1; 591 gc->ngpio = bank->nr_pins; 592 gc->label = bank->name; 593 gc->parent = bank->dev; 594 595 ret = gpiochip_add_data(gc, bank); 596 if (ret) { 597 dev_err(bank->dev, "failed to add gpiochip %s, %d\n", 598 gc->label, ret); 599 return ret; 600 } 601 602 /* 603 * For DeviceTree-supported systems, the gpio core checks the 604 * pinctrl's device node for the "gpio-ranges" property. 605 * If it is present, it takes care of adding the pin ranges 606 * for the driver. In this case the driver can skip ahead. 607 * 608 * In order to remain compatible with older, existing DeviceTree 609 * files which don't set the "gpio-ranges" property or systems that 610 * utilize ACPI the driver has to call gpiochip_add_pin_range(). 611 */ 612 if (!of_property_present(bank->of_node, "gpio-ranges")) { 613 struct device_node *pctlnp = of_get_parent(bank->of_node); 614 struct pinctrl_dev *pctldev = NULL; 615 616 if (!pctlnp) 617 return -ENODATA; 618 619 pctldev = of_pinctrl_get(pctlnp); 620 of_node_put(pctlnp); 621 if (!pctldev) 622 return -ENODEV; 623 624 ret = gpiochip_add_pin_range(gc, dev_name(pctldev->dev), 0, 625 bank->pin_base, bank->nr_pins); 626 if (ret) { 627 dev_err(bank->dev, "Failed to add pin range\n"); 628 goto fail; 629 } 630 } 631 632 ret = rockchip_interrupts_register(bank); 633 if (ret) { 634 dev_err(bank->dev, "failed to register interrupt, %d\n", ret); 635 goto fail; 636 } 637 638 return 0; 639 640 fail: 641 gpiochip_remove(&bank->gpio_chip); 642 643 return ret; 644 } 645 646 static void rockchip_clk_put(void *data) 647 { 648 struct clk *clk = data; 649 650 clk_put(clk); 651 } 652 653 static int rockchip_get_bank_data(struct rockchip_pin_bank *bank) 654 { 655 struct platform_device *pdev = to_platform_device(bank->dev); 656 int id = 0, ret; 657 658 bank->reg_base = devm_platform_ioremap_resource(pdev, 0); 659 if (IS_ERR(bank->reg_base)) 660 return PTR_ERR(bank->reg_base); 661 662 ret = platform_get_irq(pdev, 0); 663 if (ret < 0) 664 return ret; 665 bank->irq = ret; 666 667 bank->clk = devm_clk_get_enabled(bank->dev, NULL); 668 if (IS_ERR(bank->clk)) 669 return PTR_ERR(bank->clk); 670 671 id = readl(bank->reg_base + gpio_regs_v2.version_id); 672 673 switch (id) { 674 case GPIO_TYPE_V2: 675 case GPIO_TYPE_V2_1: 676 case GPIO_TYPE_V2_2: 677 bank->gpio_regs = &gpio_regs_v2; 678 bank->gpio_type = GPIO_TYPE_V2; 679 bank->db_clk = of_clk_get(bank->of_node, 1); 680 if (IS_ERR(bank->db_clk)) { 681 dev_err(bank->dev, "cannot find debounce clk\n"); 682 return -EINVAL; 683 } 684 685 ret = devm_add_action_or_reset(bank->dev, rockchip_clk_put, 686 bank->db_clk); 687 if (ret) 688 return ret; 689 break; 690 case GPIO_TYPE_V1: 691 bank->gpio_regs = &gpio_regs_v1; 692 bank->gpio_type = GPIO_TYPE_V1; 693 break; 694 default: 695 dev_err(bank->dev, "unsupported version ID: 0x%08x\n", id); 696 return -ENODEV; 697 } 698 699 return 0; 700 } 701 702 static struct rockchip_pin_bank * 703 rockchip_gpio_find_bank(struct pinctrl_dev *pctldev, int id) 704 { 705 struct rockchip_pinctrl *info; 706 struct rockchip_pin_bank *bank; 707 int i, found = 0; 708 709 info = pinctrl_dev_get_drvdata(pctldev); 710 bank = info->ctrl->pin_banks; 711 for (i = 0; i < info->ctrl->nr_banks; i++, bank++) { 712 if (bank->bank_num == id) { 713 found = 1; 714 break; 715 } 716 } 717 718 return found ? bank : NULL; 719 } 720 721 static int rockchip_gpio_probe(struct platform_device *pdev) 722 { 723 struct device *dev = &pdev->dev; 724 struct device_node *np = dev->of_node; 725 struct device_node *pctlnp = of_get_parent(np); 726 struct pinctrl_dev *pctldev = NULL; 727 struct rockchip_pin_bank *bank = NULL; 728 struct rockchip_pin_deferred *cfg; 729 static int gpio; 730 int id, ret; 731 732 if (!np || !pctlnp) 733 return -ENODEV; 734 735 pctldev = of_pinctrl_get(pctlnp); 736 of_node_put(pctlnp); 737 if (!pctldev) 738 return -EPROBE_DEFER; 739 740 id = of_alias_get_id(np, "gpio"); 741 if (id < 0) 742 id = gpio++; 743 744 bank = rockchip_gpio_find_bank(pctldev, id); 745 if (!bank) 746 return -EINVAL; 747 748 bank->dev = dev; 749 bank->of_node = np; 750 751 raw_spin_lock_init(&bank->slock); 752 753 ret = rockchip_get_bank_data(bank); 754 if (ret) 755 return ret; 756 757 /* 758 * Prevent clashes with a deferred output setting 759 * being added right at this moment. 760 */ 761 mutex_lock(&bank->deferred_lock); 762 763 ret = rockchip_gpiolib_register(bank); 764 if (ret) { 765 mutex_unlock(&bank->deferred_lock); 766 return ret; 767 } 768 769 while (!list_empty(&bank->deferred_pins)) { 770 cfg = list_first_entry(&bank->deferred_pins, 771 struct rockchip_pin_deferred, head); 772 list_del(&cfg->head); 773 774 switch (cfg->param) { 775 case PIN_CONFIG_LEVEL: 776 ret = rockchip_gpio_direction_output(&bank->gpio_chip, cfg->pin, cfg->arg); 777 if (ret) 778 dev_warn(dev, "setting output pin %u to %u failed\n", cfg->pin, 779 cfg->arg); 780 break; 781 case PIN_CONFIG_INPUT_ENABLE: 782 ret = rockchip_gpio_direction_input(&bank->gpio_chip, cfg->pin); 783 if (ret) 784 dev_warn(dev, "setting input pin %u failed\n", cfg->pin); 785 break; 786 default: 787 dev_warn(dev, "unknown deferred config param %d\n", cfg->param); 788 break; 789 } 790 kfree(cfg); 791 } 792 793 mutex_unlock(&bank->deferred_lock); 794 795 platform_set_drvdata(pdev, bank); 796 dev_info(dev, "probed %pOF\n", np); 797 798 return 0; 799 } 800 801 static void rockchip_gpio_remove(struct platform_device *pdev) 802 { 803 struct rockchip_pin_bank *bank = platform_get_drvdata(pdev); 804 805 irq_set_chained_handler_and_data(bank->irq, NULL, NULL); 806 if (bank->domain) { 807 irq_domain_remove_generic_chips(bank->domain); 808 irq_domain_remove(bank->domain); 809 } 810 gpiochip_remove(&bank->gpio_chip); 811 } 812 813 static const struct of_device_id rockchip_gpio_match[] = { 814 { .compatible = "rockchip,gpio-bank", }, 815 { .compatible = "rockchip,rk3188-gpio-bank0" }, 816 { }, 817 }; 818 819 static struct platform_driver rockchip_gpio_driver = { 820 .probe = rockchip_gpio_probe, 821 .remove = rockchip_gpio_remove, 822 .driver = { 823 .name = "rockchip-gpio", 824 .of_match_table = rockchip_gpio_match, 825 }, 826 }; 827 828 static int __init rockchip_gpio_init(void) 829 { 830 return platform_driver_register(&rockchip_gpio_driver); 831 } 832 postcore_initcall(rockchip_gpio_init); 833 834 static void __exit rockchip_gpio_exit(void) 835 { 836 platform_driver_unregister(&rockchip_gpio_driver); 837 } 838 module_exit(rockchip_gpio_exit); 839 840 MODULE_DESCRIPTION("Rockchip gpio driver"); 841 MODULE_ALIAS("platform:rockchip-gpio"); 842 MODULE_LICENSE("GPL v2"); 843 MODULE_DEVICE_TABLE(of, rockchip_gpio_match); 844