1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2013 MundoReader S.L.
4 * Author: Heiko Stuebner <heiko@sntech.de>
6 * Copyright (c) 2021 Rockchip Electronics Co. Ltd.
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>
17 #include <linux/module.h>
19 #include <linux/of_address.h>
20 #include <linux/of_device.h>
21 #include <linux/of_irq.h>
22 #include <linux/regmap.h>
24 #include "../pinctrl/core.h"
25 #include "../pinctrl/pinctrl-rockchip.h"
27 #define GPIO_TYPE_V1 (0) /* GPIO Version ID reserved */
28 #define GPIO_TYPE_V2 (0x01000C2B) /* GPIO Version ID 0x01000C2B */
30 static const struct rockchip_gpio_regs gpio_regs_v1 = {
38 .int_rawstatus = 0x44,
44 static const struct rockchip_gpio_regs gpio_regs_v2 = {
53 .int_rawstatus = 0x58,
56 .dbclk_div_con = 0x48,
62 static inline void gpio_writel_v2(u32 val, void __iomem *reg)
64 writel((val & 0xffff) | 0xffff0000, reg);
65 writel((val >> 16) | 0xffff0000, reg + 0x4);
68 static inline u32 gpio_readl_v2(void __iomem *reg)
70 return readl(reg + 0x4) << 16 | readl(reg);
73 static inline void rockchip_gpio_writel(struct rockchip_pin_bank *bank,
74 u32 value, unsigned int offset)
76 void __iomem *reg = bank->reg_base + offset;
78 if (bank->gpio_type == GPIO_TYPE_V2)
79 gpio_writel_v2(value, reg);
84 static inline u32 rockchip_gpio_readl(struct rockchip_pin_bank *bank,
87 void __iomem *reg = bank->reg_base + offset;
90 if (bank->gpio_type == GPIO_TYPE_V2)
91 value = gpio_readl_v2(reg);
98 static inline void rockchip_gpio_writel_bit(struct rockchip_pin_bank *bank,
102 void __iomem *reg = bank->reg_base + offset;
105 if (bank->gpio_type == GPIO_TYPE_V2) {
107 data = BIT(bit % 16) | BIT(bit % 16 + 16);
109 data = BIT(bit % 16 + 16);
110 writel(data, bit >= 16 ? reg + 0x4 : reg);
120 static inline u32 rockchip_gpio_readl_bit(struct rockchip_pin_bank *bank,
121 u32 bit, unsigned int offset)
123 void __iomem *reg = bank->reg_base + offset;
126 if (bank->gpio_type == GPIO_TYPE_V2) {
127 data = readl(bit >= 16 ? reg + 0x4 : reg);
137 static int rockchip_gpio_get_direction(struct gpio_chip *chip,
140 struct rockchip_pin_bank *bank = gpiochip_get_data(chip);
143 data = rockchip_gpio_readl_bit(bank, offset, bank->gpio_regs->port_ddr);
144 if (data & BIT(offset))
145 return GPIO_LINE_DIRECTION_OUT;
147 return GPIO_LINE_DIRECTION_IN;
150 static int rockchip_gpio_set_direction(struct gpio_chip *chip,
151 unsigned int offset, bool input)
153 struct rockchip_pin_bank *bank = gpiochip_get_data(chip);
155 u32 data = input ? 0 : 1;
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);
164 static void rockchip_gpio_set(struct gpio_chip *gc, unsigned int offset,
167 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
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);
175 static int rockchip_gpio_get(struct gpio_chip *gc, unsigned int offset)
177 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
180 data = readl(bank->reg_base + bank->gpio_regs->ext_port);
187 static int rockchip_gpio_set_debounce(struct gpio_chip *gc,
189 unsigned int debounce)
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;
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)
205 div = debounce * freq;
206 div_reg = DIV_ROUND_CLOSEST_ULL(div, 2 * USEC_PER_SEC) - 1;
208 div_debounce_support = false;
211 raw_spin_lock_irqsave(&bank->slock, flags);
213 /* Only the v1 needs to configure div_en and div_con for dbclk */
215 if (div_debounce_support) {
216 /* Configure the max debounce from consumers */
217 cur_div_reg = readl(bank->reg_base +
219 if (cur_div_reg < div_reg)
220 writel(div_reg, bank->reg_base +
222 rockchip_gpio_writel_bit(bank, offset, 1,
226 rockchip_gpio_writel_bit(bank, offset, 1, reg->debounce);
228 if (div_debounce_support)
229 rockchip_gpio_writel_bit(bank, offset, 0,
232 rockchip_gpio_writel_bit(bank, offset, 0, reg->debounce);
235 raw_spin_unlock_irqrestore(&bank->slock, flags);
237 /* Enable or disable dbclk at last */
238 if (div_debounce_support) {
240 clk_prepare_enable(bank->db_clk);
242 clk_disable_unprepare(bank->db_clk);
248 static int rockchip_gpio_direction_input(struct gpio_chip *gc,
251 return rockchip_gpio_set_direction(gc, offset, true);
254 static int rockchip_gpio_direction_output(struct gpio_chip *gc,
255 unsigned int offset, int value)
257 rockchip_gpio_set(gc, offset, value);
259 return rockchip_gpio_set_direction(gc, offset, false);
263 * gpiolib set_config callback function. The setting of the pin
264 * mux function as 'gpio output' will be handled by the pinctrl subsystem
267 static int rockchip_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
268 unsigned long config)
270 enum pin_config_param param = pinconf_to_config_param(config);
273 case PIN_CONFIG_INPUT_DEBOUNCE:
274 rockchip_gpio_set_debounce(gc, offset, true);
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.
293 * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
294 * and a virtual IRQ, if not already present.
296 static int rockchip_gpio_to_irq(struct gpio_chip *gc, unsigned int offset)
298 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
304 virq = irq_create_mapping(bank->domain, offset);
306 return (virq) ? : -ENXIO;
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,
322 static void rockchip_irq_demux(struct irq_desc *desc)
324 struct irq_chip *chip = irq_desc_get_chip(desc);
325 struct rockchip_pin_bank *bank = irq_desc_get_handler_data(desc);
328 dev_dbg(bank->dev, "got irq for bank %s\n", bank->name);
330 chained_irq_enter(chip, desc);
332 pend = readl_relaxed(bank->reg_base + bank->gpio_regs->int_status);
335 unsigned int irq, virq;
339 virq = irq_find_mapping(bank->domain, irq);
342 dev_err(bank->dev, "unmapped irq %d\n", irq);
346 dev_dbg(bank->dev, "handling irq %d\n", irq);
349 * Triggering IRQ on both rising and falling edge
350 * needs manual intervention.
352 if (bank->toggle_edge_mode & BIT(irq)) {
353 u32 data, data_old, polarity;
356 data = readl_relaxed(bank->reg_base +
357 bank->gpio_regs->ext_port);
359 raw_spin_lock_irqsave(&bank->slock, flags);
361 polarity = readl_relaxed(bank->reg_base +
362 bank->gpio_regs->int_polarity);
364 polarity &= ~BIT(irq);
366 polarity |= BIT(irq);
369 bank->gpio_regs->int_polarity);
371 raw_spin_unlock_irqrestore(&bank->slock, flags);
374 data = readl_relaxed(bank->reg_base +
375 bank->gpio_regs->ext_port);
376 } while ((data & BIT(irq)) != (data_old & BIT(irq)));
379 generic_handle_irq(virq);
382 chained_irq_exit(chip, desc);
385 static int rockchip_irq_set_type(struct irq_data *d, unsigned int type)
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);
396 raw_spin_lock_irqsave(&bank->slock, flags);
398 rockchip_gpio_writel_bit(bank, d->hwirq, 0,
399 bank->gpio_regs->port_ddr);
401 raw_spin_unlock_irqrestore(&bank->slock, flags);
403 if (type & IRQ_TYPE_EDGE_BOTH)
404 irq_set_handler_locked(d, handle_edge_irq);
406 irq_set_handler_locked(d, handle_level_irq);
408 raw_spin_lock_irqsave(&bank->slock, flags);
410 level = rockchip_gpio_readl(bank, bank->gpio_regs->int_type);
411 polarity = rockchip_gpio_readl(bank, bank->gpio_regs->int_polarity);
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);
421 bank->toggle_edge_mode |= mask;
425 * Determine gpio state. If 1 next interrupt should be
426 * falling otherwise rising.
428 data = readl(bank->reg_base + bank->gpio_regs->ext_port);
435 case IRQ_TYPE_EDGE_RISING:
436 bank->toggle_edge_mode &= ~mask;
440 case IRQ_TYPE_EDGE_FALLING:
441 bank->toggle_edge_mode &= ~mask;
445 case IRQ_TYPE_LEVEL_HIGH:
446 bank->toggle_edge_mode &= ~mask;
450 case IRQ_TYPE_LEVEL_LOW:
451 bank->toggle_edge_mode &= ~mask;
460 rockchip_gpio_writel(bank, level, bank->gpio_regs->int_type);
461 rockchip_gpio_writel(bank, polarity, bank->gpio_regs->int_polarity);
463 raw_spin_unlock_irqrestore(&bank->slock, flags);
468 static void rockchip_irq_suspend(struct irq_data *d)
470 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
471 struct rockchip_pin_bank *bank = gc->private;
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);
477 static void rockchip_irq_resume(struct irq_data *d)
479 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
480 struct rockchip_pin_bank *bank = gc->private;
482 irq_reg_writel(gc, bank->saved_masks, bank->gpio_regs->int_mask);
485 static void rockchip_irq_enable(struct irq_data *d)
487 irq_gc_mask_clr_bit(d);
490 static void rockchip_irq_disable(struct irq_data *d)
492 irq_gc_mask_set_bit(d);
495 static int rockchip_interrupts_register(struct rockchip_pin_bank *bank)
497 unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
498 struct irq_chip_generic *gc;
501 bank->domain = irq_domain_add_linear(bank->of_node, 32,
502 &irq_generic_chip_ops, NULL);
504 dev_warn(bank->dev, "could not init irq domain for bank %s\n",
509 ret = irq_alloc_domain_generic_chips(bank->domain, 32, 1,
514 dev_err(bank->dev, "could not alloc generic chips for bank %s\n",
516 irq_domain_remove(bank->domain);
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;
526 gc->reg_base = bank->reg_base;
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);
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.
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;
551 irq_set_chained_handler_and_data(bank->irq,
552 rockchip_irq_demux, bank);
557 static int rockchip_gpiolib_register(struct rockchip_pin_bank *bank)
559 struct gpio_chip *gc;
562 bank->gpio_chip = rockchip_gpiolib_chip;
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);
573 ret = gpiochip_add_data(gc, bank);
575 dev_err(bank->dev, "failed to add gpiochip %s, %d\n",
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.
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().
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;
597 pctldev = of_pinctrl_get(pctlnp);
601 ret = gpiochip_add_pin_range(gc, dev_name(pctldev->dev), 0,
602 gc->base, gc->ngpio);
604 dev_err(bank->dev, "Failed to add pin range\n");
609 ret = rockchip_interrupts_register(bank);
611 dev_err(bank->dev, "failed to register interrupt, %d\n", ret);
618 gpiochip_remove(&bank->gpio_chip);
623 static int rockchip_get_bank_data(struct rockchip_pin_bank *bank)
628 if (of_address_to_resource(bank->of_node, 0, &res)) {
629 dev_err(bank->dev, "cannot find IO resource for bank\n");
633 bank->reg_base = devm_ioremap_resource(bank->dev, &res);
634 if (IS_ERR(bank->reg_base))
635 return PTR_ERR(bank->reg_base);
637 bank->irq = irq_of_parse_and_map(bank->of_node, 0);
641 bank->clk = of_clk_get(bank->of_node, 0);
642 if (IS_ERR(bank->clk))
643 return PTR_ERR(bank->clk);
645 clk_prepare_enable(bank->clk);
646 id = readl(bank->reg_base + gpio_regs_v2.version_id);
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);
659 bank->gpio_regs = &gpio_regs_v1;
660 bank->gpio_type = GPIO_TYPE_V1;
666 static struct rockchip_pin_bank *
667 rockchip_gpio_find_bank(struct pinctrl_dev *pctldev, int id)
669 struct rockchip_pinctrl *info;
670 struct rockchip_pin_bank *bank;
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) {
682 return found ? bank : NULL;
685 static int rockchip_gpio_probe(struct platform_device *pdev)
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;
698 pctldev = of_pinctrl_get(pctlnp);
700 return -EPROBE_DEFER;
702 id = of_alias_get_id(np, "gpio");
706 bank = rockchip_gpio_find_bank(pctldev, id);
713 raw_spin_lock_init(&bank->slock);
715 ret = rockchip_get_bank_data(bank);
719 ret = rockchip_gpiolib_register(bank);
721 clk_disable_unprepare(bank->clk);
725 platform_set_drvdata(pdev, bank);
726 dev_info(dev, "probed %pOF\n", np);
731 static int rockchip_gpio_remove(struct platform_device *pdev)
733 struct rockchip_pin_bank *bank = platform_get_drvdata(pdev);
735 clk_disable_unprepare(bank->clk);
736 gpiochip_remove(&bank->gpio_chip);
741 static const struct of_device_id rockchip_gpio_match[] = {
742 { .compatible = "rockchip,gpio-bank", },
743 { .compatible = "rockchip,rk3188-gpio-bank0" },
747 static struct platform_driver rockchip_gpio_driver = {
748 .probe = rockchip_gpio_probe,
749 .remove = rockchip_gpio_remove,
751 .name = "rockchip-gpio",
752 .of_match_table = rockchip_gpio_match,
756 static int __init rockchip_gpio_init(void)
758 return platform_driver_register(&rockchip_gpio_driver);
760 postcore_initcall(rockchip_gpio_init);
762 static void __exit rockchip_gpio_exit(void)
764 platform_driver_unregister(&rockchip_gpio_driver);
766 module_exit(rockchip_gpio_exit);
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);