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

Merge branch irq/generic_handle_domain_irq into irq/irqchip-next

- Tree-wide conversion to generic_handle_domain_irq()
- irqdomain documentation update

Drag the pinctl ib-rockchip branch to resolve conflicts.

* irq/generic_handle_domain_irq:
pinctrl/rockchip: drop the gpio related codes
gpio/rockchip: drop irq_gc_lock/irq_gc_unlock for irq set type
gpio/rockchip: support next version gpio controller
gpio/rockchip: use struct rockchip_gpio_regs for gpio controller
gpio/rockchip: add driver for rockchip gpio
dt-bindings: gpio: change items restriction of clock for rockchip,gpio-bank
pinctrl/rockchip: add pinctrl device to gpio bank struct
pinctrl/rockchip: separate struct rockchip_pin_bank to a head file
pinctrl/rockchip: always enable clock for gpio controller

Signed-off-by: Marc Zyngier <maz@kernel.org>

+1089 -887
+4 -1
Documentation/devicetree/bindings/gpio/rockchip,gpio-bank.yaml
··· 22 22 maxItems: 1 23 23 24 24 clocks: 25 - maxItems: 1 25 + minItems: 1 26 + items: 27 + - description: APB interface clock source 28 + - description: GPIO debounce reference clock source 26 29 27 30 gpio-controller: true 28 31
+8
drivers/gpio/Kconfig
··· 520 520 A 32-bit single register GPIO fixed in/out implementation. This 521 521 can be used to represent any register as a set of GPIO signals. 522 522 523 + config GPIO_ROCKCHIP 524 + tristate "Rockchip GPIO support" 525 + depends on ARCH_ROCKCHIP || COMPILE_TEST 526 + select GPIOLIB_IRQCHIP 527 + default ARCH_ROCKCHIP 528 + help 529 + Say yes here to support GPIO on Rockchip SoCs. 530 + 523 531 config GPIO_SAMA5D2_PIOBU 524 532 tristate "SAMA5D2 PIOBU GPIO support" 525 533 depends on MFD_SYSCON
+1
drivers/gpio/Makefile
··· 128 128 obj-$(CONFIG_GPIO_RDC321X) += gpio-rdc321x.o 129 129 obj-$(CONFIG_GPIO_REALTEK_OTTO) += gpio-realtek-otto.o 130 130 obj-$(CONFIG_GPIO_REG) += gpio-reg.o 131 + obj-$(CONFIG_GPIO_ROCKCHIP) += gpio-rockchip.o 131 132 obj-$(CONFIG_ARCH_SA1100) += gpio-sa1100.o 132 133 obj-$(CONFIG_GPIO_SAMA5D2_PIOBU) += gpio-sama5d2-piobu.o 133 134 obj-$(CONFIG_GPIO_SCH311X) += gpio-sch311x.o
+771
drivers/gpio/gpio-rockchip.c
··· 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_device.h> 21 + #include <linux/of_irq.h> 22 + #include <linux/regmap.h> 23 + 24 + #include "../pinctrl/core.h" 25 + #include "../pinctrl/pinctrl-rockchip.h" 26 + 27 + #define GPIO_TYPE_V1 (0) /* GPIO Version ID reserved */ 28 + #define GPIO_TYPE_V2 (0x01000C2B) /* GPIO Version ID 0x01000C2B */ 29 + 30 + static const struct rockchip_gpio_regs gpio_regs_v1 = { 31 + .port_dr = 0x00, 32 + .port_ddr = 0x04, 33 + .int_en = 0x30, 34 + .int_mask = 0x34, 35 + .int_type = 0x38, 36 + .int_polarity = 0x3c, 37 + .int_status = 0x40, 38 + .int_rawstatus = 0x44, 39 + .debounce = 0x48, 40 + .port_eoi = 0x4c, 41 + .ext_port = 0x50, 42 + }; 43 + 44 + static const struct rockchip_gpio_regs gpio_regs_v2 = { 45 + .port_dr = 0x00, 46 + .port_ddr = 0x08, 47 + .int_en = 0x10, 48 + .int_mask = 0x18, 49 + .int_type = 0x20, 50 + .int_polarity = 0x28, 51 + .int_bothedge = 0x30, 52 + .int_status = 0x50, 53 + .int_rawstatus = 0x58, 54 + .debounce = 0x38, 55 + .dbclk_div_en = 0x40, 56 + .dbclk_div_con = 0x48, 57 + .port_eoi = 0x60, 58 + .ext_port = 0x70, 59 + .version_id = 0x78, 60 + }; 61 + 62 + static inline void gpio_writel_v2(u32 val, void __iomem *reg) 63 + { 64 + writel((val & 0xffff) | 0xffff0000, reg); 65 + writel((val >> 16) | 0xffff0000, reg + 0x4); 66 + } 67 + 68 + static inline u32 gpio_readl_v2(void __iomem *reg) 69 + { 70 + return readl(reg + 0x4) << 16 | readl(reg); 71 + } 72 + 73 + static inline void rockchip_gpio_writel(struct rockchip_pin_bank *bank, 74 + u32 value, unsigned int offset) 75 + { 76 + void __iomem *reg = bank->reg_base + offset; 77 + 78 + if (bank->gpio_type == GPIO_TYPE_V2) 79 + gpio_writel_v2(value, reg); 80 + else 81 + writel(value, reg); 82 + } 83 + 84 + static inline u32 rockchip_gpio_readl(struct rockchip_pin_bank *bank, 85 + unsigned int offset) 86 + { 87 + void __iomem *reg = bank->reg_base + offset; 88 + u32 value; 89 + 90 + if (bank->gpio_type == GPIO_TYPE_V2) 91 + value = gpio_readl_v2(reg); 92 + else 93 + value = readl(reg); 94 + 95 + return value; 96 + } 97 + 98 + static inline void rockchip_gpio_writel_bit(struct rockchip_pin_bank *bank, 99 + u32 bit, u32 value, 100 + unsigned int offset) 101 + { 102 + void __iomem *reg = bank->reg_base + offset; 103 + u32 data; 104 + 105 + if (bank->gpio_type == GPIO_TYPE_V2) { 106 + if (value) 107 + data = BIT(bit % 16) | BIT(bit % 16 + 16); 108 + else 109 + data = BIT(bit % 16 + 16); 110 + writel(data, bit >= 16 ? reg + 0x4 : reg); 111 + } else { 112 + data = readl(reg); 113 + data &= ~BIT(bit); 114 + if (value) 115 + data |= BIT(bit); 116 + writel(data, reg); 117 + } 118 + } 119 + 120 + static inline u32 rockchip_gpio_readl_bit(struct rockchip_pin_bank *bank, 121 + u32 bit, unsigned int offset) 122 + { 123 + void __iomem *reg = bank->reg_base + offset; 124 + u32 data; 125 + 126 + if (bank->gpio_type == GPIO_TYPE_V2) { 127 + data = readl(bit >= 16 ? reg + 0x4 : reg); 128 + data >>= bit % 16; 129 + } else { 130 + data = readl(reg); 131 + data >>= bit; 132 + } 133 + 134 + return data & (0x1); 135 + } 136 + 137 + static int rockchip_gpio_get_direction(struct gpio_chip *chip, 138 + unsigned int offset) 139 + { 140 + struct rockchip_pin_bank *bank = gpiochip_get_data(chip); 141 + u32 data; 142 + 143 + data = rockchip_gpio_readl_bit(bank, offset, bank->gpio_regs->port_ddr); 144 + if (data & BIT(offset)) 145 + return GPIO_LINE_DIRECTION_OUT; 146 + 147 + return GPIO_LINE_DIRECTION_IN; 148 + } 149 + 150 + static int rockchip_gpio_set_direction(struct gpio_chip *chip, 151 + unsigned int offset, bool input) 152 + { 153 + struct rockchip_pin_bank *bank = gpiochip_get_data(chip); 154 + unsigned long flags; 155 + u32 data = input ? 0 : 1; 156 + 157 + raw_spin_lock_irqsave(&bank->slock, flags); 158 + rockchip_gpio_writel_bit(bank, offset, data, bank->gpio_regs->port_ddr); 159 + raw_spin_unlock_irqrestore(&bank->slock, flags); 160 + 161 + return 0; 162 + } 163 + 164 + static void rockchip_gpio_set(struct gpio_chip *gc, unsigned int offset, 165 + int value) 166 + { 167 + struct rockchip_pin_bank *bank = gpiochip_get_data(gc); 168 + unsigned long flags; 169 + 170 + raw_spin_lock_irqsave(&bank->slock, flags); 171 + rockchip_gpio_writel_bit(bank, offset, value, bank->gpio_regs->port_dr); 172 + raw_spin_unlock_irqrestore(&bank->slock, flags); 173 + } 174 + 175 + static int rockchip_gpio_get(struct gpio_chip *gc, unsigned int offset) 176 + { 177 + struct rockchip_pin_bank *bank = gpiochip_get_data(gc); 178 + u32 data; 179 + 180 + data = readl(bank->reg_base + bank->gpio_regs->ext_port); 181 + data >>= offset; 182 + data &= 1; 183 + 184 + return data; 185 + } 186 + 187 + static int rockchip_gpio_set_debounce(struct gpio_chip *gc, 188 + unsigned int offset, 189 + unsigned int debounce) 190 + { 191 + struct rockchip_pin_bank *bank = gpiochip_get_data(gc); 192 + const struct rockchip_gpio_regs *reg = bank->gpio_regs; 193 + unsigned long flags, div_reg, freq, max_debounce; 194 + bool div_debounce_support; 195 + unsigned int cur_div_reg; 196 + u64 div; 197 + 198 + if (!IS_ERR(bank->db_clk)) { 199 + div_debounce_support = true; 200 + freq = clk_get_rate(bank->db_clk); 201 + max_debounce = (GENMASK(23, 0) + 1) * 2 * 1000000 / freq; 202 + if (debounce > max_debounce) 203 + return -EINVAL; 204 + 205 + div = debounce * freq; 206 + div_reg = DIV_ROUND_CLOSEST_ULL(div, 2 * USEC_PER_SEC) - 1; 207 + } else { 208 + div_debounce_support = false; 209 + } 210 + 211 + raw_spin_lock_irqsave(&bank->slock, flags); 212 + 213 + /* Only the v1 needs to configure div_en and div_con for dbclk */ 214 + if (debounce) { 215 + if (div_debounce_support) { 216 + /* Configure the max debounce from consumers */ 217 + cur_div_reg = readl(bank->reg_base + 218 + reg->dbclk_div_con); 219 + if (cur_div_reg < div_reg) 220 + writel(div_reg, bank->reg_base + 221 + reg->dbclk_div_con); 222 + rockchip_gpio_writel_bit(bank, offset, 1, 223 + reg->dbclk_div_en); 224 + } 225 + 226 + rockchip_gpio_writel_bit(bank, offset, 1, reg->debounce); 227 + } else { 228 + if (div_debounce_support) 229 + rockchip_gpio_writel_bit(bank, offset, 0, 230 + reg->dbclk_div_en); 231 + 232 + rockchip_gpio_writel_bit(bank, offset, 0, reg->debounce); 233 + } 234 + 235 + raw_spin_unlock_irqrestore(&bank->slock, flags); 236 + 237 + /* Enable or disable dbclk at last */ 238 + if (div_debounce_support) { 239 + if (debounce) 240 + clk_prepare_enable(bank->db_clk); 241 + else 242 + clk_disable_unprepare(bank->db_clk); 243 + } 244 + 245 + return 0; 246 + } 247 + 248 + static int rockchip_gpio_direction_input(struct gpio_chip *gc, 249 + unsigned int offset) 250 + { 251 + return rockchip_gpio_set_direction(gc, offset, true); 252 + } 253 + 254 + static int rockchip_gpio_direction_output(struct gpio_chip *gc, 255 + unsigned int offset, int value) 256 + { 257 + rockchip_gpio_set(gc, offset, value); 258 + 259 + return rockchip_gpio_set_direction(gc, offset, false); 260 + } 261 + 262 + /* 263 + * gpiolib set_config callback function. The setting of the pin 264 + * mux function as 'gpio output' will be handled by the pinctrl subsystem 265 + * interface. 266 + */ 267 + static int rockchip_gpio_set_config(struct gpio_chip *gc, unsigned int offset, 268 + unsigned long config) 269 + { 270 + enum pin_config_param param = pinconf_to_config_param(config); 271 + 272 + switch (param) { 273 + case PIN_CONFIG_INPUT_DEBOUNCE: 274 + rockchip_gpio_set_debounce(gc, offset, true); 275 + /* 276 + * Rockchip's gpio could only support up to one period 277 + * of the debounce clock(pclk), which is far away from 278 + * satisftying the requirement, as pclk is usually near 279 + * 100MHz shared by all peripherals. So the fact is it 280 + * has crippled debounce capability could only be useful 281 + * to prevent any spurious glitches from waking up the system 282 + * if the gpio is conguired as wakeup interrupt source. Let's 283 + * still return -ENOTSUPP as before, to make sure the caller 284 + * of gpiod_set_debounce won't change its behaviour. 285 + */ 286 + return -ENOTSUPP; 287 + default: 288 + return -ENOTSUPP; 289 + } 290 + } 291 + 292 + /* 293 + * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin 294 + * and a virtual IRQ, if not already present. 295 + */ 296 + static int rockchip_gpio_to_irq(struct gpio_chip *gc, unsigned int offset) 297 + { 298 + struct rockchip_pin_bank *bank = gpiochip_get_data(gc); 299 + unsigned int virq; 300 + 301 + if (!bank->domain) 302 + return -ENXIO; 303 + 304 + virq = irq_create_mapping(bank->domain, offset); 305 + 306 + return (virq) ? : -ENXIO; 307 + } 308 + 309 + static const struct gpio_chip rockchip_gpiolib_chip = { 310 + .request = gpiochip_generic_request, 311 + .free = gpiochip_generic_free, 312 + .set = rockchip_gpio_set, 313 + .get = rockchip_gpio_get, 314 + .get_direction = rockchip_gpio_get_direction, 315 + .direction_input = rockchip_gpio_direction_input, 316 + .direction_output = rockchip_gpio_direction_output, 317 + .set_config = rockchip_gpio_set_config, 318 + .to_irq = rockchip_gpio_to_irq, 319 + .owner = THIS_MODULE, 320 + }; 321 + 322 + static void rockchip_irq_demux(struct irq_desc *desc) 323 + { 324 + struct irq_chip *chip = irq_desc_get_chip(desc); 325 + struct rockchip_pin_bank *bank = irq_desc_get_handler_data(desc); 326 + u32 pend; 327 + 328 + dev_dbg(bank->dev, "got irq for bank %s\n", bank->name); 329 + 330 + chained_irq_enter(chip, desc); 331 + 332 + pend = readl_relaxed(bank->reg_base + bank->gpio_regs->int_status); 333 + 334 + while (pend) { 335 + unsigned int irq, virq; 336 + 337 + irq = __ffs(pend); 338 + pend &= ~BIT(irq); 339 + virq = irq_find_mapping(bank->domain, irq); 340 + 341 + if (!virq) { 342 + dev_err(bank->dev, "unmapped irq %d\n", irq); 343 + continue; 344 + } 345 + 346 + dev_dbg(bank->dev, "handling irq %d\n", irq); 347 + 348 + /* 349 + * Triggering IRQ on both rising and falling edge 350 + * needs manual intervention. 351 + */ 352 + if (bank->toggle_edge_mode & BIT(irq)) { 353 + u32 data, data_old, polarity; 354 + unsigned long flags; 355 + 356 + data = readl_relaxed(bank->reg_base + 357 + bank->gpio_regs->ext_port); 358 + do { 359 + raw_spin_lock_irqsave(&bank->slock, flags); 360 + 361 + polarity = readl_relaxed(bank->reg_base + 362 + bank->gpio_regs->int_polarity); 363 + if (data & BIT(irq)) 364 + polarity &= ~BIT(irq); 365 + else 366 + polarity |= BIT(irq); 367 + writel(polarity, 368 + bank->reg_base + 369 + bank->gpio_regs->int_polarity); 370 + 371 + raw_spin_unlock_irqrestore(&bank->slock, flags); 372 + 373 + data_old = data; 374 + data = readl_relaxed(bank->reg_base + 375 + bank->gpio_regs->ext_port); 376 + } while ((data & BIT(irq)) != (data_old & BIT(irq))); 377 + } 378 + 379 + generic_handle_irq(virq); 380 + } 381 + 382 + chained_irq_exit(chip, desc); 383 + } 384 + 385 + static int rockchip_irq_set_type(struct irq_data *d, unsigned int type) 386 + { 387 + struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 388 + struct rockchip_pin_bank *bank = gc->private; 389 + u32 mask = BIT(d->hwirq); 390 + u32 polarity; 391 + u32 level; 392 + u32 data; 393 + unsigned long flags; 394 + int ret = 0; 395 + 396 + raw_spin_lock_irqsave(&bank->slock, flags); 397 + 398 + rockchip_gpio_writel_bit(bank, d->hwirq, 0, 399 + bank->gpio_regs->port_ddr); 400 + 401 + raw_spin_unlock_irqrestore(&bank->slock, flags); 402 + 403 + if (type & IRQ_TYPE_EDGE_BOTH) 404 + irq_set_handler_locked(d, handle_edge_irq); 405 + else 406 + irq_set_handler_locked(d, handle_level_irq); 407 + 408 + raw_spin_lock_irqsave(&bank->slock, flags); 409 + 410 + level = rockchip_gpio_readl(bank, bank->gpio_regs->int_type); 411 + polarity = rockchip_gpio_readl(bank, bank->gpio_regs->int_polarity); 412 + 413 + switch (type) { 414 + case IRQ_TYPE_EDGE_BOTH: 415 + if (bank->gpio_type == GPIO_TYPE_V2) { 416 + bank->toggle_edge_mode &= ~mask; 417 + rockchip_gpio_writel_bit(bank, d->hwirq, 1, 418 + bank->gpio_regs->int_bothedge); 419 + goto out; 420 + } else { 421 + bank->toggle_edge_mode |= mask; 422 + level |= mask; 423 + 424 + /* 425 + * Determine gpio state. If 1 next interrupt should be 426 + * falling otherwise rising. 427 + */ 428 + data = readl(bank->reg_base + bank->gpio_regs->ext_port); 429 + if (data & mask) 430 + polarity &= ~mask; 431 + else 432 + polarity |= mask; 433 + } 434 + break; 435 + case IRQ_TYPE_EDGE_RISING: 436 + bank->toggle_edge_mode &= ~mask; 437 + level |= mask; 438 + polarity |= mask; 439 + break; 440 + case IRQ_TYPE_EDGE_FALLING: 441 + bank->toggle_edge_mode &= ~mask; 442 + level |= mask; 443 + polarity &= ~mask; 444 + break; 445 + case IRQ_TYPE_LEVEL_HIGH: 446 + bank->toggle_edge_mode &= ~mask; 447 + level &= ~mask; 448 + polarity |= mask; 449 + break; 450 + case IRQ_TYPE_LEVEL_LOW: 451 + bank->toggle_edge_mode &= ~mask; 452 + level &= ~mask; 453 + polarity &= ~mask; 454 + break; 455 + default: 456 + ret = -EINVAL; 457 + goto out; 458 + } 459 + 460 + rockchip_gpio_writel(bank, level, bank->gpio_regs->int_type); 461 + rockchip_gpio_writel(bank, polarity, bank->gpio_regs->int_polarity); 462 + out: 463 + raw_spin_unlock_irqrestore(&bank->slock, flags); 464 + 465 + return ret; 466 + } 467 + 468 + static void rockchip_irq_suspend(struct irq_data *d) 469 + { 470 + struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 471 + struct rockchip_pin_bank *bank = gc->private; 472 + 473 + bank->saved_masks = irq_reg_readl(gc, bank->gpio_regs->int_mask); 474 + irq_reg_writel(gc, ~gc->wake_active, bank->gpio_regs->int_mask); 475 + } 476 + 477 + static void rockchip_irq_resume(struct irq_data *d) 478 + { 479 + struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 480 + struct rockchip_pin_bank *bank = gc->private; 481 + 482 + irq_reg_writel(gc, bank->saved_masks, bank->gpio_regs->int_mask); 483 + } 484 + 485 + static void rockchip_irq_enable(struct irq_data *d) 486 + { 487 + irq_gc_mask_clr_bit(d); 488 + } 489 + 490 + static void rockchip_irq_disable(struct irq_data *d) 491 + { 492 + irq_gc_mask_set_bit(d); 493 + } 494 + 495 + static int rockchip_interrupts_register(struct rockchip_pin_bank *bank) 496 + { 497 + unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN; 498 + struct irq_chip_generic *gc; 499 + int ret; 500 + 501 + bank->domain = irq_domain_add_linear(bank->of_node, 32, 502 + &irq_generic_chip_ops, NULL); 503 + if (!bank->domain) { 504 + dev_warn(bank->dev, "could not init irq domain for bank %s\n", 505 + bank->name); 506 + return -EINVAL; 507 + } 508 + 509 + ret = irq_alloc_domain_generic_chips(bank->domain, 32, 1, 510 + "rockchip_gpio_irq", 511 + handle_level_irq, 512 + clr, 0, 0); 513 + if (ret) { 514 + dev_err(bank->dev, "could not alloc generic chips for bank %s\n", 515 + bank->name); 516 + irq_domain_remove(bank->domain); 517 + return -EINVAL; 518 + } 519 + 520 + gc = irq_get_domain_generic_chip(bank->domain, 0); 521 + if (bank->gpio_type == GPIO_TYPE_V2) { 522 + gc->reg_writel = gpio_writel_v2; 523 + gc->reg_readl = gpio_readl_v2; 524 + } 525 + 526 + gc->reg_base = bank->reg_base; 527 + gc->private = bank; 528 + gc->chip_types[0].regs.mask = bank->gpio_regs->int_mask; 529 + gc->chip_types[0].regs.ack = bank->gpio_regs->port_eoi; 530 + gc->chip_types[0].chip.irq_ack = irq_gc_ack_set_bit; 531 + gc->chip_types[0].chip.irq_mask = irq_gc_mask_set_bit; 532 + gc->chip_types[0].chip.irq_unmask = irq_gc_mask_clr_bit; 533 + gc->chip_types[0].chip.irq_enable = rockchip_irq_enable; 534 + gc->chip_types[0].chip.irq_disable = rockchip_irq_disable; 535 + gc->chip_types[0].chip.irq_set_wake = irq_gc_set_wake; 536 + gc->chip_types[0].chip.irq_suspend = rockchip_irq_suspend; 537 + gc->chip_types[0].chip.irq_resume = rockchip_irq_resume; 538 + gc->chip_types[0].chip.irq_set_type = rockchip_irq_set_type; 539 + gc->wake_enabled = IRQ_MSK(bank->nr_pins); 540 + 541 + /* 542 + * Linux assumes that all interrupts start out disabled/masked. 543 + * Our driver only uses the concept of masked and always keeps 544 + * things enabled, so for us that's all masked and all enabled. 545 + */ 546 + rockchip_gpio_writel(bank, 0xffffffff, bank->gpio_regs->int_mask); 547 + rockchip_gpio_writel(bank, 0xffffffff, bank->gpio_regs->port_eoi); 548 + rockchip_gpio_writel(bank, 0xffffffff, bank->gpio_regs->int_en); 549 + gc->mask_cache = 0xffffffff; 550 + 551 + irq_set_chained_handler_and_data(bank->irq, 552 + rockchip_irq_demux, bank); 553 + 554 + return 0; 555 + } 556 + 557 + static int rockchip_gpiolib_register(struct rockchip_pin_bank *bank) 558 + { 559 + struct gpio_chip *gc; 560 + int ret; 561 + 562 + bank->gpio_chip = rockchip_gpiolib_chip; 563 + 564 + gc = &bank->gpio_chip; 565 + gc->base = bank->pin_base; 566 + gc->ngpio = bank->nr_pins; 567 + gc->label = bank->name; 568 + gc->parent = bank->dev; 569 + #ifdef CONFIG_OF_GPIO 570 + gc->of_node = of_node_get(bank->of_node); 571 + #endif 572 + 573 + ret = gpiochip_add_data(gc, bank); 574 + if (ret) { 575 + dev_err(bank->dev, "failed to add gpiochip %s, %d\n", 576 + gc->label, ret); 577 + return ret; 578 + } 579 + 580 + /* 581 + * For DeviceTree-supported systems, the gpio core checks the 582 + * pinctrl's device node for the "gpio-ranges" property. 583 + * If it is present, it takes care of adding the pin ranges 584 + * for the driver. In this case the driver can skip ahead. 585 + * 586 + * In order to remain compatible with older, existing DeviceTree 587 + * files which don't set the "gpio-ranges" property or systems that 588 + * utilize ACPI the driver has to call gpiochip_add_pin_range(). 589 + */ 590 + if (!of_property_read_bool(bank->of_node, "gpio-ranges")) { 591 + struct device_node *pctlnp = of_get_parent(bank->of_node); 592 + struct pinctrl_dev *pctldev = NULL; 593 + 594 + if (!pctlnp) 595 + return -ENODATA; 596 + 597 + pctldev = of_pinctrl_get(pctlnp); 598 + if (!pctldev) 599 + return -ENODEV; 600 + 601 + ret = gpiochip_add_pin_range(gc, dev_name(pctldev->dev), 0, 602 + gc->base, gc->ngpio); 603 + if (ret) { 604 + dev_err(bank->dev, "Failed to add pin range\n"); 605 + goto fail; 606 + } 607 + } 608 + 609 + ret = rockchip_interrupts_register(bank); 610 + if (ret) { 611 + dev_err(bank->dev, "failed to register interrupt, %d\n", ret); 612 + goto fail; 613 + } 614 + 615 + return 0; 616 + 617 + fail: 618 + gpiochip_remove(&bank->gpio_chip); 619 + 620 + return ret; 621 + } 622 + 623 + static int rockchip_get_bank_data(struct rockchip_pin_bank *bank) 624 + { 625 + struct resource res; 626 + int id = 0; 627 + 628 + if (of_address_to_resource(bank->of_node, 0, &res)) { 629 + dev_err(bank->dev, "cannot find IO resource for bank\n"); 630 + return -ENOENT; 631 + } 632 + 633 + bank->reg_base = devm_ioremap_resource(bank->dev, &res); 634 + if (IS_ERR(bank->reg_base)) 635 + return PTR_ERR(bank->reg_base); 636 + 637 + bank->irq = irq_of_parse_and_map(bank->of_node, 0); 638 + if (!bank->irq) 639 + return -EINVAL; 640 + 641 + bank->clk = of_clk_get(bank->of_node, 0); 642 + if (IS_ERR(bank->clk)) 643 + return PTR_ERR(bank->clk); 644 + 645 + clk_prepare_enable(bank->clk); 646 + id = readl(bank->reg_base + gpio_regs_v2.version_id); 647 + 648 + /* If not gpio v2, that is default to v1. */ 649 + if (id == GPIO_TYPE_V2) { 650 + bank->gpio_regs = &gpio_regs_v2; 651 + bank->gpio_type = GPIO_TYPE_V2; 652 + bank->db_clk = of_clk_get(bank->of_node, 1); 653 + if (IS_ERR(bank->db_clk)) { 654 + dev_err(bank->dev, "cannot find debounce clk\n"); 655 + clk_disable_unprepare(bank->clk); 656 + return -EINVAL; 657 + } 658 + } else { 659 + bank->gpio_regs = &gpio_regs_v1; 660 + bank->gpio_type = GPIO_TYPE_V1; 661 + } 662 + 663 + return 0; 664 + } 665 + 666 + static struct rockchip_pin_bank * 667 + rockchip_gpio_find_bank(struct pinctrl_dev *pctldev, int id) 668 + { 669 + struct rockchip_pinctrl *info; 670 + struct rockchip_pin_bank *bank; 671 + int i, found = 0; 672 + 673 + info = pinctrl_dev_get_drvdata(pctldev); 674 + bank = info->ctrl->pin_banks; 675 + for (i = 0; i < info->ctrl->nr_banks; i++, bank++) { 676 + if (bank->bank_num == id) { 677 + found = 1; 678 + break; 679 + } 680 + } 681 + 682 + return found ? bank : NULL; 683 + } 684 + 685 + static int rockchip_gpio_probe(struct platform_device *pdev) 686 + { 687 + struct device *dev = &pdev->dev; 688 + struct device_node *np = dev->of_node; 689 + struct device_node *pctlnp = of_get_parent(np); 690 + struct pinctrl_dev *pctldev = NULL; 691 + struct rockchip_pin_bank *bank = NULL; 692 + static int gpio; 693 + int id, ret; 694 + 695 + if (!np || !pctlnp) 696 + return -ENODEV; 697 + 698 + pctldev = of_pinctrl_get(pctlnp); 699 + if (!pctldev) 700 + return -EPROBE_DEFER; 701 + 702 + id = of_alias_get_id(np, "gpio"); 703 + if (id < 0) 704 + id = gpio++; 705 + 706 + bank = rockchip_gpio_find_bank(pctldev, id); 707 + if (!bank) 708 + return -EINVAL; 709 + 710 + bank->dev = dev; 711 + bank->of_node = np; 712 + 713 + raw_spin_lock_init(&bank->slock); 714 + 715 + ret = rockchip_get_bank_data(bank); 716 + if (ret) 717 + return ret; 718 + 719 + ret = rockchip_gpiolib_register(bank); 720 + if (ret) { 721 + clk_disable_unprepare(bank->clk); 722 + return ret; 723 + } 724 + 725 + platform_set_drvdata(pdev, bank); 726 + dev_info(dev, "probed %pOF\n", np); 727 + 728 + return 0; 729 + } 730 + 731 + static int rockchip_gpio_remove(struct platform_device *pdev) 732 + { 733 + struct rockchip_pin_bank *bank = platform_get_drvdata(pdev); 734 + 735 + clk_disable_unprepare(bank->clk); 736 + gpiochip_remove(&bank->gpio_chip); 737 + 738 + return 0; 739 + } 740 + 741 + static const struct of_device_id rockchip_gpio_match[] = { 742 + { .compatible = "rockchip,gpio-bank", }, 743 + { .compatible = "rockchip,rk3188-gpio-bank0" }, 744 + { }, 745 + }; 746 + 747 + static struct platform_driver rockchip_gpio_driver = { 748 + .probe = rockchip_gpio_probe, 749 + .remove = rockchip_gpio_remove, 750 + .driver = { 751 + .name = "rockchip-gpio", 752 + .of_match_table = rockchip_gpio_match, 753 + }, 754 + }; 755 + 756 + static int __init rockchip_gpio_init(void) 757 + { 758 + return platform_driver_register(&rockchip_gpio_driver); 759 + } 760 + postcore_initcall(rockchip_gpio_init); 761 + 762 + static void __exit rockchip_gpio_exit(void) 763 + { 764 + platform_driver_unregister(&rockchip_gpio_driver); 765 + } 766 + module_exit(rockchip_gpio_exit); 767 + 768 + MODULE_DESCRIPTION("Rockchip gpio driver"); 769 + MODULE_ALIAS("platform:rockchip-gpio"); 770 + MODULE_LICENSE("GPL v2"); 771 + MODULE_DEVICE_TABLE(of, rockchip_gpio_match);
+18 -886
drivers/pinctrl/pinctrl-rockchip.c
··· 21 21 #include <linux/io.h> 22 22 #include <linux/bitops.h> 23 23 #include <linux/gpio/driver.h> 24 - #include <linux/of_device.h> 25 24 #include <linux/of_address.h> 25 + #include <linux/of_device.h> 26 26 #include <linux/of_irq.h> 27 27 #include <linux/pinctrl/machine.h> 28 28 #include <linux/pinctrl/pinconf.h> ··· 37 37 38 38 #include "core.h" 39 39 #include "pinconf.h" 40 - 41 - /* GPIO control registers */ 42 - #define GPIO_SWPORT_DR 0x00 43 - #define GPIO_SWPORT_DDR 0x04 44 - #define GPIO_INTEN 0x30 45 - #define GPIO_INTMASK 0x34 46 - #define GPIO_INTTYPE_LEVEL 0x38 47 - #define GPIO_INT_POLARITY 0x3c 48 - #define GPIO_INT_STATUS 0x40 49 - #define GPIO_INT_RAWSTATUS 0x44 50 - #define GPIO_DEBOUNCE 0x48 51 - #define GPIO_PORTS_EOI 0x4c 52 - #define GPIO_EXT_PORT 0x50 53 - #define GPIO_LS_SYNC 0x60 54 - 55 - enum rockchip_pinctrl_type { 56 - PX30, 57 - RV1108, 58 - RK2928, 59 - RK3066B, 60 - RK3128, 61 - RK3188, 62 - RK3288, 63 - RK3308, 64 - RK3368, 65 - RK3399, 66 - RK3568, 67 - }; 68 - 40 + #include "pinctrl-rockchip.h" 69 41 70 42 /** 71 43 * Generate a bitmask for setting a value (v) with a write mask bit in hiword ··· 55 83 #define IOMUX_UNROUTED BIT(3) 56 84 #define IOMUX_WIDTH_3BIT BIT(4) 57 85 #define IOMUX_WIDTH_2BIT BIT(5) 58 - 59 - /** 60 - * struct rockchip_iomux 61 - * @type: iomux variant using IOMUX_* constants 62 - * @offset: if initialized to -1 it will be autocalculated, by specifying 63 - * an initial offset value the relevant source offset can be reset 64 - * to a new value for autocalculating the following iomux registers. 65 - */ 66 - struct rockchip_iomux { 67 - int type; 68 - int offset; 69 - }; 70 - 71 - /* 72 - * enum type index corresponding to rockchip_perpin_drv_list arrays index. 73 - */ 74 - enum rockchip_pin_drv_type { 75 - DRV_TYPE_IO_DEFAULT = 0, 76 - DRV_TYPE_IO_1V8_OR_3V0, 77 - DRV_TYPE_IO_1V8_ONLY, 78 - DRV_TYPE_IO_1V8_3V0_AUTO, 79 - DRV_TYPE_IO_3V3_ONLY, 80 - DRV_TYPE_MAX 81 - }; 82 - 83 - /* 84 - * enum type index corresponding to rockchip_pull_list arrays index. 85 - */ 86 - enum rockchip_pin_pull_type { 87 - PULL_TYPE_IO_DEFAULT = 0, 88 - PULL_TYPE_IO_1V8_ONLY, 89 - PULL_TYPE_MAX 90 - }; 91 - 92 - /** 93 - * struct rockchip_drv 94 - * @drv_type: drive strength variant using rockchip_perpin_drv_type 95 - * @offset: if initialized to -1 it will be autocalculated, by specifying 96 - * an initial offset value the relevant source offset can be reset 97 - * to a new value for autocalculating the following drive strength 98 - * registers. if used chips own cal_drv func instead to calculate 99 - * registers offset, the variant could be ignored. 100 - */ 101 - struct rockchip_drv { 102 - enum rockchip_pin_drv_type drv_type; 103 - int offset; 104 - }; 105 - 106 - /** 107 - * struct rockchip_pin_bank 108 - * @reg_base: register base of the gpio bank 109 - * @regmap_pull: optional separate register for additional pull settings 110 - * @clk: clock of the gpio bank 111 - * @irq: interrupt of the gpio bank 112 - * @saved_masks: Saved content of GPIO_INTEN at suspend time. 113 - * @pin_base: first pin number 114 - * @nr_pins: number of pins in this bank 115 - * @name: name of the bank 116 - * @bank_num: number of the bank, to account for holes 117 - * @iomux: array describing the 4 iomux sources of the bank 118 - * @drv: array describing the 4 drive strength sources of the bank 119 - * @pull_type: array describing the 4 pull type sources of the bank 120 - * @valid: is all necessary information present 121 - * @of_node: dt node of this bank 122 - * @drvdata: common pinctrl basedata 123 - * @domain: irqdomain of the gpio bank 124 - * @gpio_chip: gpiolib chip 125 - * @grange: gpio range 126 - * @slock: spinlock for the gpio bank 127 - * @toggle_edge_mode: bit mask to toggle (falling/rising) edge mode 128 - * @recalced_mask: bit mask to indicate a need to recalulate the mask 129 - * @route_mask: bits describing the routing pins of per bank 130 - */ 131 - struct rockchip_pin_bank { 132 - void __iomem *reg_base; 133 - struct regmap *regmap_pull; 134 - struct clk *clk; 135 - int irq; 136 - u32 saved_masks; 137 - u32 pin_base; 138 - u8 nr_pins; 139 - char *name; 140 - u8 bank_num; 141 - struct rockchip_iomux iomux[4]; 142 - struct rockchip_drv drv[4]; 143 - enum rockchip_pin_pull_type pull_type[4]; 144 - bool valid; 145 - struct device_node *of_node; 146 - struct rockchip_pinctrl *drvdata; 147 - struct irq_domain *domain; 148 - struct gpio_chip gpio_chip; 149 - struct pinctrl_gpio_range grange; 150 - raw_spinlock_t slock; 151 - u32 toggle_edge_mode; 152 - u32 recalced_mask; 153 - u32 route_mask; 154 - }; 155 86 156 87 #define PIN_BANK(id, pins, label) \ 157 88 { \ ··· 194 319 195 320 #define RK_MUXROUTE_PMU(ID, PIN, FUNC, REG, VAL) \ 196 321 PIN_BANK_MUX_ROUTE_FLAGS(ID, PIN, FUNC, REG, VAL, ROCKCHIP_ROUTE_PMU) 197 - 198 - /** 199 - * struct rockchip_mux_recalced_data: represent a pin iomux data. 200 - * @num: bank number. 201 - * @pin: pin number. 202 - * @bit: index at register. 203 - * @reg: register offset. 204 - * @mask: mask bit 205 - */ 206 - struct rockchip_mux_recalced_data { 207 - u8 num; 208 - u8 pin; 209 - u32 reg; 210 - u8 bit; 211 - u8 mask; 212 - }; 213 - 214 - enum rockchip_mux_route_location { 215 - ROCKCHIP_ROUTE_SAME = 0, 216 - ROCKCHIP_ROUTE_PMU, 217 - ROCKCHIP_ROUTE_GRF, 218 - }; 219 - 220 - /** 221 - * struct rockchip_mux_recalced_data: represent a pin iomux data. 222 - * @bank_num: bank number. 223 - * @pin: index at register or used to calc index. 224 - * @func: the min pin. 225 - * @route_location: the mux route location (same, pmu, grf). 226 - * @route_offset: the max pin. 227 - * @route_val: the register offset. 228 - */ 229 - struct rockchip_mux_route_data { 230 - u8 bank_num; 231 - u8 pin; 232 - u8 func; 233 - enum rockchip_mux_route_location route_location; 234 - u32 route_offset; 235 - u32 route_val; 236 - }; 237 - 238 - struct rockchip_pin_ctrl { 239 - struct rockchip_pin_bank *pin_banks; 240 - u32 nr_banks; 241 - u32 nr_pins; 242 - char *label; 243 - enum rockchip_pinctrl_type type; 244 - int grf_mux_offset; 245 - int pmu_mux_offset; 246 - int grf_drv_offset; 247 - int pmu_drv_offset; 248 - struct rockchip_mux_recalced_data *iomux_recalced; 249 - u32 niomux_recalced; 250 - struct rockchip_mux_route_data *iomux_routes; 251 - u32 niomux_routes; 252 - 253 - void (*pull_calc_reg)(struct rockchip_pin_bank *bank, 254 - int pin_num, struct regmap **regmap, 255 - int *reg, u8 *bit); 256 - void (*drv_calc_reg)(struct rockchip_pin_bank *bank, 257 - int pin_num, struct regmap **regmap, 258 - int *reg, u8 *bit); 259 - int (*schmitt_calc_reg)(struct rockchip_pin_bank *bank, 260 - int pin_num, struct regmap **regmap, 261 - int *reg, u8 *bit); 262 - }; 263 - 264 - struct rockchip_pin_config { 265 - unsigned int func; 266 - unsigned long *configs; 267 - unsigned int nconfigs; 268 - }; 269 - 270 - /** 271 - * struct rockchip_pin_group: represent group of pins of a pinmux function. 272 - * @name: name of the pin group, used to lookup the group. 273 - * @pins: the pins included in this group. 274 - * @npins: number of pins included in this group. 275 - * @data: local pin configuration 276 - */ 277 - struct rockchip_pin_group { 278 - const char *name; 279 - unsigned int npins; 280 - unsigned int *pins; 281 - struct rockchip_pin_config *data; 282 - }; 283 - 284 - /** 285 - * struct rockchip_pmx_func: represent a pin function. 286 - * @name: name of the pin function, used to lookup the function. 287 - * @groups: one or more names of pin groups that provide this function. 288 - * @ngroups: number of groups included in @groups. 289 - */ 290 - struct rockchip_pmx_func { 291 - const char *name; 292 - const char **groups; 293 - u8 ngroups; 294 - }; 295 - 296 - struct rockchip_pinctrl { 297 - struct regmap *regmap_base; 298 - int reg_size; 299 - struct regmap *regmap_pull; 300 - struct regmap *regmap_pmu; 301 - struct device *dev; 302 - struct rockchip_pin_ctrl *ctrl; 303 - struct pinctrl_desc pctl; 304 - struct pinctrl_dev *pctl_dev; 305 - struct rockchip_pin_group *groups; 306 - unsigned int ngroups; 307 - struct rockchip_pmx_func *functions; 308 - unsigned int nfunctions; 309 - }; 310 322 311 323 static struct regmap_config rockchip_regmap_config = { 312 324 .reg_bits = 32, ··· 2057 2295 return 0; 2058 2296 } 2059 2297 2060 - static int rockchip_gpio_get_direction(struct gpio_chip *chip, unsigned offset) 2061 - { 2062 - struct rockchip_pin_bank *bank = gpiochip_get_data(chip); 2063 - u32 data; 2064 - int ret; 2065 - 2066 - ret = clk_enable(bank->clk); 2067 - if (ret < 0) { 2068 - dev_err(bank->drvdata->dev, 2069 - "failed to enable clock for bank %s\n", bank->name); 2070 - return ret; 2071 - } 2072 - data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR); 2073 - clk_disable(bank->clk); 2074 - 2075 - if (data & BIT(offset)) 2076 - return GPIO_LINE_DIRECTION_OUT; 2077 - 2078 - return GPIO_LINE_DIRECTION_IN; 2079 - } 2080 - 2081 - /* 2082 - * The calls to gpio_direction_output() and gpio_direction_input() 2083 - * leads to this function call (via the pinctrl_gpio_direction_{input|output}() 2084 - * function called from the gpiolib interface). 2085 - */ 2086 - static int _rockchip_pmx_gpio_set_direction(struct gpio_chip *chip, 2087 - int pin, bool input) 2088 - { 2089 - struct rockchip_pin_bank *bank; 2090 - int ret; 2091 - unsigned long flags; 2092 - u32 data; 2093 - 2094 - bank = gpiochip_get_data(chip); 2095 - 2096 - ret = rockchip_set_mux(bank, pin, RK_FUNC_GPIO); 2097 - if (ret < 0) 2098 - return ret; 2099 - 2100 - clk_enable(bank->clk); 2101 - raw_spin_lock_irqsave(&bank->slock, flags); 2102 - 2103 - data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR); 2104 - /* set bit to 1 for output, 0 for input */ 2105 - if (!input) 2106 - data |= BIT(pin); 2107 - else 2108 - data &= ~BIT(pin); 2109 - writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR); 2110 - 2111 - raw_spin_unlock_irqrestore(&bank->slock, flags); 2112 - clk_disable(bank->clk); 2113 - 2114 - return 0; 2115 - } 2116 - 2117 - static int rockchip_pmx_gpio_set_direction(struct pinctrl_dev *pctldev, 2118 - struct pinctrl_gpio_range *range, 2119 - unsigned offset, bool input) 2120 - { 2121 - struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); 2122 - struct gpio_chip *chip; 2123 - int pin; 2124 - 2125 - chip = range->gc; 2126 - pin = offset - chip->base; 2127 - dev_dbg(info->dev, "gpio_direction for pin %u as %s-%d to %s\n", 2128 - offset, range->name, pin, input ? "input" : "output"); 2129 - 2130 - return _rockchip_pmx_gpio_set_direction(chip, offset - chip->base, 2131 - input); 2132 - } 2133 - 2134 2298 static const struct pinmux_ops rockchip_pmx_ops = { 2135 2299 .get_functions_count = rockchip_pmx_get_funcs_count, 2136 2300 .get_function_name = rockchip_pmx_get_func_name, 2137 2301 .get_function_groups = rockchip_pmx_get_groups, 2138 2302 .set_mux = rockchip_pmx_set, 2139 - .gpio_set_direction = rockchip_pmx_gpio_set_direction, 2140 2303 }; 2141 2304 2142 2305 /* ··· 2092 2405 return false; 2093 2406 } 2094 2407 2095 - static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value); 2096 - static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset); 2097 - 2098 2408 /* set the pin config settings for a specified pin */ 2099 2409 static int rockchip_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin, 2100 2410 unsigned long *configs, unsigned num_configs) 2101 2411 { 2102 2412 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); 2103 2413 struct rockchip_pin_bank *bank = pin_to_bank(info, pin); 2414 + struct gpio_chip *gpio = &bank->gpio_chip; 2104 2415 enum pin_config_param param; 2105 2416 u32 arg; 2106 2417 int i; ··· 2131 2446 return rc; 2132 2447 break; 2133 2448 case PIN_CONFIG_OUTPUT: 2134 - rockchip_gpio_set(&bank->gpio_chip, 2135 - pin - bank->pin_base, arg); 2136 - rc = _rockchip_pmx_gpio_set_direction(&bank->gpio_chip, 2137 - pin - bank->pin_base, false); 2449 + rc = rockchip_set_mux(bank, pin - bank->pin_base, 2450 + RK_FUNC_GPIO); 2451 + if (rc != RK_FUNC_GPIO) 2452 + return -EINVAL; 2453 + 2454 + rc = gpio->direction_output(gpio, pin - bank->pin_base, 2455 + arg); 2138 2456 if (rc) 2139 2457 return rc; 2140 2458 break; ··· 2175 2487 { 2176 2488 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); 2177 2489 struct rockchip_pin_bank *bank = pin_to_bank(info, pin); 2490 + struct gpio_chip *gpio = &bank->gpio_chip; 2178 2491 enum pin_config_param param = pinconf_to_config_param(*config); 2179 2492 u16 arg; 2180 2493 int rc; ··· 2204 2515 if (rc != RK_FUNC_GPIO) 2205 2516 return -EINVAL; 2206 2517 2207 - rc = rockchip_gpio_get(&bank->gpio_chip, pin - bank->pin_base); 2518 + rc = gpio->get(gpio, pin - bank->pin_base); 2208 2519 if (rc < 0) 2209 2520 return rc; 2210 2521 ··· 2442 2753 ctrldesc->npins = info->ctrl->nr_pins; 2443 2754 2444 2755 pdesc = pindesc; 2445 - for (bank = 0 , k = 0; bank < info->ctrl->nr_banks; bank++) { 2756 + for (bank = 0, k = 0; bank < info->ctrl->nr_banks; bank++) { 2446 2757 pin_bank = &info->ctrl->pin_banks[bank]; 2447 2758 for (pin = 0; pin < pin_bank->nr_pins; pin++, k++) { 2448 2759 pdesc->number = k; ··· 2462 2773 return PTR_ERR(info->pctl_dev); 2463 2774 } 2464 2775 2465 - for (bank = 0; bank < info->ctrl->nr_banks; ++bank) { 2466 - pin_bank = &info->ctrl->pin_banks[bank]; 2467 - pin_bank->grange.name = pin_bank->name; 2468 - pin_bank->grange.id = bank; 2469 - pin_bank->grange.pin_base = pin_bank->pin_base; 2470 - pin_bank->grange.base = pin_bank->gpio_chip.base; 2471 - pin_bank->grange.npins = pin_bank->gpio_chip.ngpio; 2472 - pin_bank->grange.gc = &pin_bank->gpio_chip; 2473 - pinctrl_add_gpio_range(info->pctl_dev, &pin_bank->grange); 2474 - } 2475 - 2476 2776 return 0; 2477 - } 2478 - 2479 - /* 2480 - * GPIO handling 2481 - */ 2482 - 2483 - static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value) 2484 - { 2485 - struct rockchip_pin_bank *bank = gpiochip_get_data(gc); 2486 - void __iomem *reg = bank->reg_base + GPIO_SWPORT_DR; 2487 - unsigned long flags; 2488 - u32 data; 2489 - 2490 - clk_enable(bank->clk); 2491 - raw_spin_lock_irqsave(&bank->slock, flags); 2492 - 2493 - data = readl(reg); 2494 - data &= ~BIT(offset); 2495 - if (value) 2496 - data |= BIT(offset); 2497 - writel(data, reg); 2498 - 2499 - raw_spin_unlock_irqrestore(&bank->slock, flags); 2500 - clk_disable(bank->clk); 2501 - } 2502 - 2503 - /* 2504 - * Returns the level of the pin for input direction and setting of the DR 2505 - * register for output gpios. 2506 - */ 2507 - static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset) 2508 - { 2509 - struct rockchip_pin_bank *bank = gpiochip_get_data(gc); 2510 - u32 data; 2511 - 2512 - clk_enable(bank->clk); 2513 - data = readl(bank->reg_base + GPIO_EXT_PORT); 2514 - clk_disable(bank->clk); 2515 - data >>= offset; 2516 - data &= 1; 2517 - return data; 2518 - } 2519 - 2520 - /* 2521 - * gpiolib gpio_direction_input callback function. The setting of the pin 2522 - * mux function as 'gpio input' will be handled by the pinctrl subsystem 2523 - * interface. 2524 - */ 2525 - static int rockchip_gpio_direction_input(struct gpio_chip *gc, unsigned offset) 2526 - { 2527 - return pinctrl_gpio_direction_input(gc->base + offset); 2528 - } 2529 - 2530 - /* 2531 - * gpiolib gpio_direction_output callback function. The setting of the pin 2532 - * mux function as 'gpio output' will be handled by the pinctrl subsystem 2533 - * interface. 2534 - */ 2535 - static int rockchip_gpio_direction_output(struct gpio_chip *gc, 2536 - unsigned offset, int value) 2537 - { 2538 - rockchip_gpio_set(gc, offset, value); 2539 - return pinctrl_gpio_direction_output(gc->base + offset); 2540 - } 2541 - 2542 - static void rockchip_gpio_set_debounce(struct gpio_chip *gc, 2543 - unsigned int offset, bool enable) 2544 - { 2545 - struct rockchip_pin_bank *bank = gpiochip_get_data(gc); 2546 - void __iomem *reg = bank->reg_base + GPIO_DEBOUNCE; 2547 - unsigned long flags; 2548 - u32 data; 2549 - 2550 - clk_enable(bank->clk); 2551 - raw_spin_lock_irqsave(&bank->slock, flags); 2552 - 2553 - data = readl(reg); 2554 - if (enable) 2555 - data |= BIT(offset); 2556 - else 2557 - data &= ~BIT(offset); 2558 - writel(data, reg); 2559 - 2560 - raw_spin_unlock_irqrestore(&bank->slock, flags); 2561 - clk_disable(bank->clk); 2562 - } 2563 - 2564 - /* 2565 - * gpiolib set_config callback function. The setting of the pin 2566 - * mux function as 'gpio output' will be handled by the pinctrl subsystem 2567 - * interface. 2568 - */ 2569 - static int rockchip_gpio_set_config(struct gpio_chip *gc, unsigned int offset, 2570 - unsigned long config) 2571 - { 2572 - enum pin_config_param param = pinconf_to_config_param(config); 2573 - 2574 - switch (param) { 2575 - case PIN_CONFIG_INPUT_DEBOUNCE: 2576 - rockchip_gpio_set_debounce(gc, offset, true); 2577 - /* 2578 - * Rockchip's gpio could only support up to one period 2579 - * of the debounce clock(pclk), which is far away from 2580 - * satisftying the requirement, as pclk is usually near 2581 - * 100MHz shared by all peripherals. So the fact is it 2582 - * has crippled debounce capability could only be useful 2583 - * to prevent any spurious glitches from waking up the system 2584 - * if the gpio is conguired as wakeup interrupt source. Let's 2585 - * still return -ENOTSUPP as before, to make sure the caller 2586 - * of gpiod_set_debounce won't change its behaviour. 2587 - */ 2588 - return -ENOTSUPP; 2589 - default: 2590 - return -ENOTSUPP; 2591 - } 2592 - } 2593 - 2594 - /* 2595 - * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin 2596 - * and a virtual IRQ, if not already present. 2597 - */ 2598 - static int rockchip_gpio_to_irq(struct gpio_chip *gc, unsigned offset) 2599 - { 2600 - struct rockchip_pin_bank *bank = gpiochip_get_data(gc); 2601 - unsigned int virq; 2602 - 2603 - if (!bank->domain) 2604 - return -ENXIO; 2605 - 2606 - clk_enable(bank->clk); 2607 - virq = irq_create_mapping(bank->domain, offset); 2608 - clk_disable(bank->clk); 2609 - 2610 - return (virq) ? : -ENXIO; 2611 - } 2612 - 2613 - static const struct gpio_chip rockchip_gpiolib_chip = { 2614 - .request = gpiochip_generic_request, 2615 - .free = gpiochip_generic_free, 2616 - .set = rockchip_gpio_set, 2617 - .get = rockchip_gpio_get, 2618 - .get_direction = rockchip_gpio_get_direction, 2619 - .direction_input = rockchip_gpio_direction_input, 2620 - .direction_output = rockchip_gpio_direction_output, 2621 - .set_config = rockchip_gpio_set_config, 2622 - .to_irq = rockchip_gpio_to_irq, 2623 - .owner = THIS_MODULE, 2624 - }; 2625 - 2626 - /* 2627 - * Interrupt handling 2628 - */ 2629 - 2630 - static void rockchip_irq_demux(struct irq_desc *desc) 2631 - { 2632 - struct irq_chip *chip = irq_desc_get_chip(desc); 2633 - struct rockchip_pin_bank *bank = irq_desc_get_handler_data(desc); 2634 - u32 pend; 2635 - 2636 - dev_dbg(bank->drvdata->dev, "got irq for bank %s\n", bank->name); 2637 - 2638 - chained_irq_enter(chip, desc); 2639 - 2640 - pend = readl_relaxed(bank->reg_base + GPIO_INT_STATUS); 2641 - 2642 - while (pend) { 2643 - unsigned int irq; 2644 - int ret; 2645 - 2646 - irq = __ffs(pend); 2647 - pend &= ~BIT(irq); 2648 - 2649 - /* 2650 - * Triggering IRQ on both rising and falling edge 2651 - * needs manual intervention. 2652 - */ 2653 - if (bank->toggle_edge_mode & BIT(irq)) { 2654 - u32 data, data_old, polarity; 2655 - unsigned long flags; 2656 - 2657 - data = readl_relaxed(bank->reg_base + GPIO_EXT_PORT); 2658 - do { 2659 - raw_spin_lock_irqsave(&bank->slock, flags); 2660 - 2661 - polarity = readl_relaxed(bank->reg_base + 2662 - GPIO_INT_POLARITY); 2663 - if (data & BIT(irq)) 2664 - polarity &= ~BIT(irq); 2665 - else 2666 - polarity |= BIT(irq); 2667 - writel(polarity, 2668 - bank->reg_base + GPIO_INT_POLARITY); 2669 - 2670 - raw_spin_unlock_irqrestore(&bank->slock, flags); 2671 - 2672 - data_old = data; 2673 - data = readl_relaxed(bank->reg_base + 2674 - GPIO_EXT_PORT); 2675 - } while ((data & BIT(irq)) != (data_old & BIT(irq))); 2676 - } 2677 - 2678 - ret = generic_handle_domain_irq(bank->domain, irq); 2679 - if (unlikely(ret)) 2680 - dev_err_ratelimited(bank->drvdata->dev, "unmapped irq %d\n", irq); 2681 - } 2682 - 2683 - chained_irq_exit(chip, desc); 2684 - } 2685 - 2686 - static int rockchip_irq_set_type(struct irq_data *d, unsigned int type) 2687 - { 2688 - struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 2689 - struct rockchip_pin_bank *bank = gc->private; 2690 - u32 mask = BIT(d->hwirq); 2691 - u32 polarity; 2692 - u32 level; 2693 - u32 data; 2694 - unsigned long flags; 2695 - int ret; 2696 - 2697 - /* make sure the pin is configured as gpio input */ 2698 - ret = rockchip_set_mux(bank, d->hwirq, RK_FUNC_GPIO); 2699 - if (ret < 0) 2700 - return ret; 2701 - 2702 - clk_enable(bank->clk); 2703 - raw_spin_lock_irqsave(&bank->slock, flags); 2704 - 2705 - data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR); 2706 - data &= ~mask; 2707 - writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR); 2708 - 2709 - raw_spin_unlock_irqrestore(&bank->slock, flags); 2710 - 2711 - if (type & IRQ_TYPE_EDGE_BOTH) 2712 - irq_set_handler_locked(d, handle_edge_irq); 2713 - else 2714 - irq_set_handler_locked(d, handle_level_irq); 2715 - 2716 - raw_spin_lock_irqsave(&bank->slock, flags); 2717 - irq_gc_lock(gc); 2718 - 2719 - level = readl_relaxed(gc->reg_base + GPIO_INTTYPE_LEVEL); 2720 - polarity = readl_relaxed(gc->reg_base + GPIO_INT_POLARITY); 2721 - 2722 - switch (type) { 2723 - case IRQ_TYPE_EDGE_BOTH: 2724 - bank->toggle_edge_mode |= mask; 2725 - level |= mask; 2726 - 2727 - /* 2728 - * Determine gpio state. If 1 next interrupt should be falling 2729 - * otherwise rising. 2730 - */ 2731 - data = readl(bank->reg_base + GPIO_EXT_PORT); 2732 - if (data & mask) 2733 - polarity &= ~mask; 2734 - else 2735 - polarity |= mask; 2736 - break; 2737 - case IRQ_TYPE_EDGE_RISING: 2738 - bank->toggle_edge_mode &= ~mask; 2739 - level |= mask; 2740 - polarity |= mask; 2741 - break; 2742 - case IRQ_TYPE_EDGE_FALLING: 2743 - bank->toggle_edge_mode &= ~mask; 2744 - level |= mask; 2745 - polarity &= ~mask; 2746 - break; 2747 - case IRQ_TYPE_LEVEL_HIGH: 2748 - bank->toggle_edge_mode &= ~mask; 2749 - level &= ~mask; 2750 - polarity |= mask; 2751 - break; 2752 - case IRQ_TYPE_LEVEL_LOW: 2753 - bank->toggle_edge_mode &= ~mask; 2754 - level &= ~mask; 2755 - polarity &= ~mask; 2756 - break; 2757 - default: 2758 - irq_gc_unlock(gc); 2759 - raw_spin_unlock_irqrestore(&bank->slock, flags); 2760 - clk_disable(bank->clk); 2761 - return -EINVAL; 2762 - } 2763 - 2764 - writel_relaxed(level, gc->reg_base + GPIO_INTTYPE_LEVEL); 2765 - writel_relaxed(polarity, gc->reg_base + GPIO_INT_POLARITY); 2766 - 2767 - irq_gc_unlock(gc); 2768 - raw_spin_unlock_irqrestore(&bank->slock, flags); 2769 - clk_disable(bank->clk); 2770 - 2771 - return 0; 2772 - } 2773 - 2774 - static void rockchip_irq_suspend(struct irq_data *d) 2775 - { 2776 - struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 2777 - struct rockchip_pin_bank *bank = gc->private; 2778 - 2779 - clk_enable(bank->clk); 2780 - bank->saved_masks = irq_reg_readl(gc, GPIO_INTMASK); 2781 - irq_reg_writel(gc, ~gc->wake_active, GPIO_INTMASK); 2782 - clk_disable(bank->clk); 2783 - } 2784 - 2785 - static void rockchip_irq_resume(struct irq_data *d) 2786 - { 2787 - struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 2788 - struct rockchip_pin_bank *bank = gc->private; 2789 - 2790 - clk_enable(bank->clk); 2791 - irq_reg_writel(gc, bank->saved_masks, GPIO_INTMASK); 2792 - clk_disable(bank->clk); 2793 - } 2794 - 2795 - static void rockchip_irq_enable(struct irq_data *d) 2796 - { 2797 - struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 2798 - struct rockchip_pin_bank *bank = gc->private; 2799 - 2800 - clk_enable(bank->clk); 2801 - irq_gc_mask_clr_bit(d); 2802 - } 2803 - 2804 - static void rockchip_irq_disable(struct irq_data *d) 2805 - { 2806 - struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 2807 - struct rockchip_pin_bank *bank = gc->private; 2808 - 2809 - irq_gc_mask_set_bit(d); 2810 - clk_disable(bank->clk); 2811 - } 2812 - 2813 - static int rockchip_interrupts_register(struct platform_device *pdev, 2814 - struct rockchip_pinctrl *info) 2815 - { 2816 - struct rockchip_pin_ctrl *ctrl = info->ctrl; 2817 - struct rockchip_pin_bank *bank = ctrl->pin_banks; 2818 - unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN; 2819 - struct irq_chip_generic *gc; 2820 - int ret; 2821 - int i; 2822 - 2823 - for (i = 0; i < ctrl->nr_banks; ++i, ++bank) { 2824 - if (!bank->valid) { 2825 - dev_warn(&pdev->dev, "bank %s is not valid\n", 2826 - bank->name); 2827 - continue; 2828 - } 2829 - 2830 - ret = clk_enable(bank->clk); 2831 - if (ret) { 2832 - dev_err(&pdev->dev, "failed to enable clock for bank %s\n", 2833 - bank->name); 2834 - continue; 2835 - } 2836 - 2837 - bank->domain = irq_domain_add_linear(bank->of_node, 32, 2838 - &irq_generic_chip_ops, NULL); 2839 - if (!bank->domain) { 2840 - dev_warn(&pdev->dev, "could not initialize irq domain for bank %s\n", 2841 - bank->name); 2842 - clk_disable(bank->clk); 2843 - continue; 2844 - } 2845 - 2846 - ret = irq_alloc_domain_generic_chips(bank->domain, 32, 1, 2847 - "rockchip_gpio_irq", handle_level_irq, 2848 - clr, 0, 0); 2849 - if (ret) { 2850 - dev_err(&pdev->dev, "could not alloc generic chips for bank %s\n", 2851 - bank->name); 2852 - irq_domain_remove(bank->domain); 2853 - clk_disable(bank->clk); 2854 - continue; 2855 - } 2856 - 2857 - gc = irq_get_domain_generic_chip(bank->domain, 0); 2858 - gc->reg_base = bank->reg_base; 2859 - gc->private = bank; 2860 - gc->chip_types[0].regs.mask = GPIO_INTMASK; 2861 - gc->chip_types[0].regs.ack = GPIO_PORTS_EOI; 2862 - gc->chip_types[0].chip.irq_ack = irq_gc_ack_set_bit; 2863 - gc->chip_types[0].chip.irq_mask = irq_gc_mask_set_bit; 2864 - gc->chip_types[0].chip.irq_unmask = irq_gc_mask_clr_bit; 2865 - gc->chip_types[0].chip.irq_enable = rockchip_irq_enable; 2866 - gc->chip_types[0].chip.irq_disable = rockchip_irq_disable; 2867 - gc->chip_types[0].chip.irq_set_wake = irq_gc_set_wake; 2868 - gc->chip_types[0].chip.irq_suspend = rockchip_irq_suspend; 2869 - gc->chip_types[0].chip.irq_resume = rockchip_irq_resume; 2870 - gc->chip_types[0].chip.irq_set_type = rockchip_irq_set_type; 2871 - gc->wake_enabled = IRQ_MSK(bank->nr_pins); 2872 - 2873 - /* 2874 - * Linux assumes that all interrupts start out disabled/masked. 2875 - * Our driver only uses the concept of masked and always keeps 2876 - * things enabled, so for us that's all masked and all enabled. 2877 - */ 2878 - writel_relaxed(0xffffffff, bank->reg_base + GPIO_INTMASK); 2879 - writel_relaxed(0xffffffff, bank->reg_base + GPIO_PORTS_EOI); 2880 - writel_relaxed(0xffffffff, bank->reg_base + GPIO_INTEN); 2881 - gc->mask_cache = 0xffffffff; 2882 - 2883 - irq_set_chained_handler_and_data(bank->irq, 2884 - rockchip_irq_demux, bank); 2885 - clk_disable(bank->clk); 2886 - } 2887 - 2888 - return 0; 2889 - } 2890 - 2891 - static int rockchip_gpiolib_register(struct platform_device *pdev, 2892 - struct rockchip_pinctrl *info) 2893 - { 2894 - struct rockchip_pin_ctrl *ctrl = info->ctrl; 2895 - struct rockchip_pin_bank *bank = ctrl->pin_banks; 2896 - struct gpio_chip *gc; 2897 - int ret; 2898 - int i; 2899 - 2900 - for (i = 0; i < ctrl->nr_banks; ++i, ++bank) { 2901 - if (!bank->valid) { 2902 - dev_warn(&pdev->dev, "bank %s is not valid\n", 2903 - bank->name); 2904 - continue; 2905 - } 2906 - 2907 - bank->gpio_chip = rockchip_gpiolib_chip; 2908 - 2909 - gc = &bank->gpio_chip; 2910 - gc->base = bank->pin_base; 2911 - gc->ngpio = bank->nr_pins; 2912 - gc->parent = &pdev->dev; 2913 - gc->of_node = bank->of_node; 2914 - gc->label = bank->name; 2915 - 2916 - ret = gpiochip_add_data(gc, bank); 2917 - if (ret) { 2918 - dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n", 2919 - gc->label, ret); 2920 - goto fail; 2921 - } 2922 - } 2923 - 2924 - rockchip_interrupts_register(pdev, info); 2925 - 2926 - return 0; 2927 - 2928 - fail: 2929 - for (--i, --bank; i >= 0; --i, --bank) { 2930 - if (!bank->valid) 2931 - continue; 2932 - gpiochip_remove(&bank->gpio_chip); 2933 - } 2934 - return ret; 2935 - } 2936 - 2937 - static int rockchip_gpiolib_unregister(struct platform_device *pdev, 2938 - struct rockchip_pinctrl *info) 2939 - { 2940 - struct rockchip_pin_ctrl *ctrl = info->ctrl; 2941 - struct rockchip_pin_bank *bank = ctrl->pin_banks; 2942 - int i; 2943 - 2944 - for (i = 0; i < ctrl->nr_banks; ++i, ++bank) { 2945 - if (!bank->valid) 2946 - continue; 2947 - gpiochip_remove(&bank->gpio_chip); 2948 - } 2949 - 2950 - return 0; 2951 - } 2952 - 2953 - static int rockchip_get_bank_data(struct rockchip_pin_bank *bank, 2954 - struct rockchip_pinctrl *info) 2955 - { 2956 - struct resource res; 2957 - void __iomem *base; 2958 - 2959 - if (of_address_to_resource(bank->of_node, 0, &res)) { 2960 - dev_err(info->dev, "cannot find IO resource for bank\n"); 2961 - return -ENOENT; 2962 - } 2963 - 2964 - bank->reg_base = devm_ioremap_resource(info->dev, &res); 2965 - if (IS_ERR(bank->reg_base)) 2966 - return PTR_ERR(bank->reg_base); 2967 - 2968 - /* 2969 - * special case, where parts of the pull setting-registers are 2970 - * part of the PMU register space 2971 - */ 2972 - if (of_device_is_compatible(bank->of_node, 2973 - "rockchip,rk3188-gpio-bank0")) { 2974 - struct device_node *node; 2975 - 2976 - node = of_parse_phandle(bank->of_node->parent, 2977 - "rockchip,pmu", 0); 2978 - if (!node) { 2979 - if (of_address_to_resource(bank->of_node, 1, &res)) { 2980 - dev_err(info->dev, "cannot find IO resource for bank\n"); 2981 - return -ENOENT; 2982 - } 2983 - 2984 - base = devm_ioremap_resource(info->dev, &res); 2985 - if (IS_ERR(base)) 2986 - return PTR_ERR(base); 2987 - rockchip_regmap_config.max_register = 2988 - resource_size(&res) - 4; 2989 - rockchip_regmap_config.name = 2990 - "rockchip,rk3188-gpio-bank0-pull"; 2991 - bank->regmap_pull = devm_regmap_init_mmio(info->dev, 2992 - base, 2993 - &rockchip_regmap_config); 2994 - } 2995 - of_node_put(node); 2996 - } 2997 - 2998 - bank->irq = irq_of_parse_and_map(bank->of_node, 0); 2999 - 3000 - bank->clk = of_clk_get(bank->of_node, 0); 3001 - if (IS_ERR(bank->clk)) 3002 - return PTR_ERR(bank->clk); 3003 - 3004 - return clk_prepare(bank->clk); 3005 2777 } 3006 2778 3007 2779 static const struct of_device_id rockchip_pinctrl_dt_match[]; ··· 2474 3324 { 2475 3325 const struct of_device_id *match; 2476 3326 struct device_node *node = pdev->dev.of_node; 2477 - struct device_node *np; 2478 3327 struct rockchip_pin_ctrl *ctrl; 2479 3328 struct rockchip_pin_bank *bank; 2480 3329 int grf_offs, pmu_offs, drv_grf_offs, drv_pmu_offs, i, j; 2481 3330 2482 3331 match = of_match_node(rockchip_pinctrl_dt_match, node); 2483 3332 ctrl = (struct rockchip_pin_ctrl *)match->data; 2484 - 2485 - for_each_child_of_node(node, np) { 2486 - if (!of_find_property(np, "gpio-controller", NULL)) 2487 - continue; 2488 - 2489 - bank = ctrl->pin_banks; 2490 - for (i = 0; i < ctrl->nr_banks; ++i, ++bank) { 2491 - if (!strcmp(bank->name, np->name)) { 2492 - bank->of_node = np; 2493 - 2494 - if (!rockchip_get_bank_data(bank, d)) 2495 - bank->valid = true; 2496 - 2497 - break; 2498 - } 2499 - } 2500 - } 2501 3333 2502 3334 grf_offs = ctrl->grf_mux_offset; 2503 3335 pmu_offs = ctrl->pmu_mux_offset; ··· 2701 3569 return PTR_ERR(info->regmap_pmu); 2702 3570 } 2703 3571 2704 - ret = rockchip_gpiolib_register(pdev, info); 3572 + ret = rockchip_pinctrl_register(pdev, info); 2705 3573 if (ret) 2706 3574 return ret; 2707 3575 2708 - ret = rockchip_pinctrl_register(pdev, info); 3576 + platform_set_drvdata(pdev, info); 3577 + 3578 + ret = of_platform_populate(np, rockchip_bank_match, NULL, NULL); 2709 3579 if (ret) { 2710 - rockchip_gpiolib_unregister(pdev, info); 3580 + dev_err(&pdev->dev, "failed to register gpio device\n"); 2711 3581 return ret; 2712 3582 } 2713 - 2714 - platform_set_drvdata(pdev, info); 2715 3583 2716 3584 return 0; 2717 3585 }
+287
drivers/pinctrl/pinctrl-rockchip.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-only */ 2 + /* 3 + * Copyright (c) 2020-2021 Rockchip Electronics Co. Ltd. 4 + * 5 + * Copyright (c) 2013 MundoReader S.L. 6 + * Author: Heiko Stuebner <heiko@sntech.de> 7 + * 8 + * With some ideas taken from pinctrl-samsung: 9 + * Copyright (c) 2012 Samsung Electronics Co., Ltd. 10 + * http://www.samsung.com 11 + * Copyright (c) 2012 Linaro Ltd 12 + * https://www.linaro.org 13 + * 14 + * and pinctrl-at91: 15 + * Copyright (C) 2011-2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> 16 + */ 17 + 18 + #ifndef _PINCTRL_ROCKCHIP_H 19 + #define _PINCTRL_ROCKCHIP_H 20 + 21 + enum rockchip_pinctrl_type { 22 + PX30, 23 + RV1108, 24 + RK2928, 25 + RK3066B, 26 + RK3128, 27 + RK3188, 28 + RK3288, 29 + RK3308, 30 + RK3368, 31 + RK3399, 32 + RK3568, 33 + }; 34 + 35 + /** 36 + * struct rockchip_gpio_regs 37 + * @port_dr: data register 38 + * @port_ddr: data direction register 39 + * @int_en: interrupt enable 40 + * @int_mask: interrupt mask 41 + * @int_type: interrupt trigger type, such as high, low, edge trriger type. 42 + * @int_polarity: interrupt polarity enable register 43 + * @int_bothedge: interrupt bothedge enable register 44 + * @int_status: interrupt status register 45 + * @int_rawstatus: int_status = int_rawstatus & int_mask 46 + * @debounce: enable debounce for interrupt signal 47 + * @dbclk_div_en: enable divider for debounce clock 48 + * @dbclk_div_con: setting for divider of debounce clock 49 + * @port_eoi: end of interrupt of the port 50 + * @ext_port: port data from external 51 + * @version_id: controller version register 52 + */ 53 + struct rockchip_gpio_regs { 54 + u32 port_dr; 55 + u32 port_ddr; 56 + u32 int_en; 57 + u32 int_mask; 58 + u32 int_type; 59 + u32 int_polarity; 60 + u32 int_bothedge; 61 + u32 int_status; 62 + u32 int_rawstatus; 63 + u32 debounce; 64 + u32 dbclk_div_en; 65 + u32 dbclk_div_con; 66 + u32 port_eoi; 67 + u32 ext_port; 68 + u32 version_id; 69 + }; 70 + 71 + /** 72 + * struct rockchip_iomux 73 + * @type: iomux variant using IOMUX_* constants 74 + * @offset: if initialized to -1 it will be autocalculated, by specifying 75 + * an initial offset value the relevant source offset can be reset 76 + * to a new value for autocalculating the following iomux registers. 77 + */ 78 + struct rockchip_iomux { 79 + int type; 80 + int offset; 81 + }; 82 + 83 + /* 84 + * enum type index corresponding to rockchip_perpin_drv_list arrays index. 85 + */ 86 + enum rockchip_pin_drv_type { 87 + DRV_TYPE_IO_DEFAULT = 0, 88 + DRV_TYPE_IO_1V8_OR_3V0, 89 + DRV_TYPE_IO_1V8_ONLY, 90 + DRV_TYPE_IO_1V8_3V0_AUTO, 91 + DRV_TYPE_IO_3V3_ONLY, 92 + DRV_TYPE_MAX 93 + }; 94 + 95 + /* 96 + * enum type index corresponding to rockchip_pull_list arrays index. 97 + */ 98 + enum rockchip_pin_pull_type { 99 + PULL_TYPE_IO_DEFAULT = 0, 100 + PULL_TYPE_IO_1V8_ONLY, 101 + PULL_TYPE_MAX 102 + }; 103 + 104 + /** 105 + * struct rockchip_drv 106 + * @drv_type: drive strength variant using rockchip_perpin_drv_type 107 + * @offset: if initialized to -1 it will be autocalculated, by specifying 108 + * an initial offset value the relevant source offset can be reset 109 + * to a new value for autocalculating the following drive strength 110 + * registers. if used chips own cal_drv func instead to calculate 111 + * registers offset, the variant could be ignored. 112 + */ 113 + struct rockchip_drv { 114 + enum rockchip_pin_drv_type drv_type; 115 + int offset; 116 + }; 117 + 118 + /** 119 + * struct rockchip_pin_bank 120 + * @dev: the pinctrl device bind to the bank 121 + * @reg_base: register base of the gpio bank 122 + * @regmap_pull: optional separate register for additional pull settings 123 + * @clk: clock of the gpio bank 124 + * @db_clk: clock of the gpio debounce 125 + * @irq: interrupt of the gpio bank 126 + * @saved_masks: Saved content of GPIO_INTEN at suspend time. 127 + * @pin_base: first pin number 128 + * @nr_pins: number of pins in this bank 129 + * @name: name of the bank 130 + * @bank_num: number of the bank, to account for holes 131 + * @iomux: array describing the 4 iomux sources of the bank 132 + * @drv: array describing the 4 drive strength sources of the bank 133 + * @pull_type: array describing the 4 pull type sources of the bank 134 + * @valid: is all necessary information present 135 + * @of_node: dt node of this bank 136 + * @drvdata: common pinctrl basedata 137 + * @domain: irqdomain of the gpio bank 138 + * @gpio_chip: gpiolib chip 139 + * @grange: gpio range 140 + * @slock: spinlock for the gpio bank 141 + * @toggle_edge_mode: bit mask to toggle (falling/rising) edge mode 142 + * @recalced_mask: bit mask to indicate a need to recalulate the mask 143 + * @route_mask: bits describing the routing pins of per bank 144 + */ 145 + struct rockchip_pin_bank { 146 + struct device *dev; 147 + void __iomem *reg_base; 148 + struct regmap *regmap_pull; 149 + struct clk *clk; 150 + struct clk *db_clk; 151 + int irq; 152 + u32 saved_masks; 153 + u32 pin_base; 154 + u8 nr_pins; 155 + char *name; 156 + u8 bank_num; 157 + struct rockchip_iomux iomux[4]; 158 + struct rockchip_drv drv[4]; 159 + enum rockchip_pin_pull_type pull_type[4]; 160 + bool valid; 161 + struct device_node *of_node; 162 + struct rockchip_pinctrl *drvdata; 163 + struct irq_domain *domain; 164 + struct gpio_chip gpio_chip; 165 + struct pinctrl_gpio_range grange; 166 + raw_spinlock_t slock; 167 + const struct rockchip_gpio_regs *gpio_regs; 168 + u32 gpio_type; 169 + u32 toggle_edge_mode; 170 + u32 recalced_mask; 171 + u32 route_mask; 172 + }; 173 + 174 + /** 175 + * struct rockchip_mux_recalced_data: represent a pin iomux data. 176 + * @num: bank number. 177 + * @pin: pin number. 178 + * @bit: index at register. 179 + * @reg: register offset. 180 + * @mask: mask bit 181 + */ 182 + struct rockchip_mux_recalced_data { 183 + u8 num; 184 + u8 pin; 185 + u32 reg; 186 + u8 bit; 187 + u8 mask; 188 + }; 189 + 190 + enum rockchip_mux_route_location { 191 + ROCKCHIP_ROUTE_SAME = 0, 192 + ROCKCHIP_ROUTE_PMU, 193 + ROCKCHIP_ROUTE_GRF, 194 + }; 195 + 196 + /** 197 + * struct rockchip_mux_recalced_data: represent a pin iomux data. 198 + * @bank_num: bank number. 199 + * @pin: index at register or used to calc index. 200 + * @func: the min pin. 201 + * @route_location: the mux route location (same, pmu, grf). 202 + * @route_offset: the max pin. 203 + * @route_val: the register offset. 204 + */ 205 + struct rockchip_mux_route_data { 206 + u8 bank_num; 207 + u8 pin; 208 + u8 func; 209 + enum rockchip_mux_route_location route_location; 210 + u32 route_offset; 211 + u32 route_val; 212 + }; 213 + 214 + struct rockchip_pin_ctrl { 215 + struct rockchip_pin_bank *pin_banks; 216 + u32 nr_banks; 217 + u32 nr_pins; 218 + char *label; 219 + enum rockchip_pinctrl_type type; 220 + int grf_mux_offset; 221 + int pmu_mux_offset; 222 + int grf_drv_offset; 223 + int pmu_drv_offset; 224 + struct rockchip_mux_recalced_data *iomux_recalced; 225 + u32 niomux_recalced; 226 + struct rockchip_mux_route_data *iomux_routes; 227 + u32 niomux_routes; 228 + 229 + void (*pull_calc_reg)(struct rockchip_pin_bank *bank, 230 + int pin_num, struct regmap **regmap, 231 + int *reg, u8 *bit); 232 + void (*drv_calc_reg)(struct rockchip_pin_bank *bank, 233 + int pin_num, struct regmap **regmap, 234 + int *reg, u8 *bit); 235 + int (*schmitt_calc_reg)(struct rockchip_pin_bank *bank, 236 + int pin_num, struct regmap **regmap, 237 + int *reg, u8 *bit); 238 + }; 239 + 240 + struct rockchip_pin_config { 241 + unsigned int func; 242 + unsigned long *configs; 243 + unsigned int nconfigs; 244 + }; 245 + 246 + /** 247 + * struct rockchip_pin_group: represent group of pins of a pinmux function. 248 + * @name: name of the pin group, used to lookup the group. 249 + * @pins: the pins included in this group. 250 + * @npins: number of pins included in this group. 251 + * @data: local pin configuration 252 + */ 253 + struct rockchip_pin_group { 254 + const char *name; 255 + unsigned int npins; 256 + unsigned int *pins; 257 + struct rockchip_pin_config *data; 258 + }; 259 + 260 + /** 261 + * struct rockchip_pmx_func: represent a pin function. 262 + * @name: name of the pin function, used to lookup the function. 263 + * @groups: one or more names of pin groups that provide this function. 264 + * @ngroups: number of groups included in @groups. 265 + */ 266 + struct rockchip_pmx_func { 267 + const char *name; 268 + const char **groups; 269 + u8 ngroups; 270 + }; 271 + 272 + struct rockchip_pinctrl { 273 + struct regmap *regmap_base; 274 + int reg_size; 275 + struct regmap *regmap_pull; 276 + struct regmap *regmap_pmu; 277 + struct device *dev; 278 + struct rockchip_pin_ctrl *ctrl; 279 + struct pinctrl_desc pctl; 280 + struct pinctrl_dev *pctl_dev; 281 + struct rockchip_pin_group *groups; 282 + unsigned int ngroups; 283 + struct rockchip_pmx_func *functions; 284 + unsigned int nfunctions; 285 + }; 286 + 287 + #endif