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/pinctrl/consumer.h>
23 #include <linux/pinctrl/pinconf-generic.h>
24 #include <linux/regmap.h>
26 #include "../pinctrl/core.h"
27 #include "../pinctrl/pinctrl-rockchip.h"
29 #define GPIO_TYPE_V1 (0) /* GPIO Version ID reserved */
30 #define GPIO_TYPE_V2 (0x01000C2B) /* GPIO Version ID 0x01000C2B */
31 #define GPIO_TYPE_V2_1 (0x0101157C) /* GPIO Version ID 0x0101157C */
33 static const struct rockchip_gpio_regs gpio_regs_v1 = {
41 .int_rawstatus = 0x44,
47 static const struct rockchip_gpio_regs gpio_regs_v2 = {
56 .int_rawstatus = 0x58,
59 .dbclk_div_con = 0x48,
65 static inline void gpio_writel_v2(u32 val, void __iomem *reg)
67 writel((val & 0xffff) | 0xffff0000, reg);
68 writel((val >> 16) | 0xffff0000, reg + 0x4);
71 static inline u32 gpio_readl_v2(void __iomem *reg)
73 return readl(reg + 0x4) << 16 | readl(reg);
76 static inline void rockchip_gpio_writel(struct rockchip_pin_bank *bank,
77 u32 value, unsigned int offset)
79 void __iomem *reg = bank->reg_base + offset;
81 if (bank->gpio_type == GPIO_TYPE_V2)
82 gpio_writel_v2(value, reg);
87 static inline u32 rockchip_gpio_readl(struct rockchip_pin_bank *bank,
90 void __iomem *reg = bank->reg_base + offset;
93 if (bank->gpio_type == GPIO_TYPE_V2)
94 value = gpio_readl_v2(reg);
101 static inline void rockchip_gpio_writel_bit(struct rockchip_pin_bank *bank,
105 void __iomem *reg = bank->reg_base + offset;
108 if (bank->gpio_type == GPIO_TYPE_V2) {
110 data = BIT(bit % 16) | BIT(bit % 16 + 16);
112 data = BIT(bit % 16 + 16);
113 writel(data, bit >= 16 ? reg + 0x4 : reg);
123 static inline u32 rockchip_gpio_readl_bit(struct rockchip_pin_bank *bank,
124 u32 bit, unsigned int offset)
126 void __iomem *reg = bank->reg_base + offset;
129 if (bank->gpio_type == GPIO_TYPE_V2) {
130 data = readl(bit >= 16 ? reg + 0x4 : reg);
140 static int rockchip_gpio_get_direction(struct gpio_chip *chip,
143 struct rockchip_pin_bank *bank = gpiochip_get_data(chip);
146 data = rockchip_gpio_readl_bit(bank, offset, bank->gpio_regs->port_ddr);
148 return GPIO_LINE_DIRECTION_OUT;
150 return GPIO_LINE_DIRECTION_IN;
153 static int rockchip_gpio_set_direction(struct gpio_chip *chip,
154 unsigned int offset, bool input)
156 struct rockchip_pin_bank *bank = gpiochip_get_data(chip);
158 u32 data = input ? 0 : 1;
162 pinctrl_gpio_direction_input(bank->pin_base + offset);
164 pinctrl_gpio_direction_output(bank->pin_base + offset);
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);
173 static void rockchip_gpio_set(struct gpio_chip *gc, unsigned int offset,
176 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
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);
184 static int rockchip_gpio_get(struct gpio_chip *gc, unsigned int offset)
186 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
189 data = readl(bank->reg_base + bank->gpio_regs->ext_port);
196 static int rockchip_gpio_set_debounce(struct gpio_chip *gc,
198 unsigned int debounce)
200 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
201 const struct rockchip_gpio_regs *reg = bank->gpio_regs;
202 unsigned long flags, div_reg, freq, max_debounce;
203 bool div_debounce_support;
204 unsigned int cur_div_reg;
207 if (bank->gpio_type == GPIO_TYPE_V2 && !IS_ERR(bank->db_clk)) {
208 div_debounce_support = true;
209 freq = clk_get_rate(bank->db_clk);
210 max_debounce = (GENMASK(23, 0) + 1) * 2 * 1000000 / freq;
211 if (debounce > max_debounce)
214 div = debounce * freq;
215 div_reg = DIV_ROUND_CLOSEST_ULL(div, 2 * USEC_PER_SEC) - 1;
217 div_debounce_support = false;
220 raw_spin_lock_irqsave(&bank->slock, flags);
222 /* Only the v1 needs to configure div_en and div_con for dbclk */
224 if (div_debounce_support) {
225 /* Configure the max debounce from consumers */
226 cur_div_reg = readl(bank->reg_base +
228 if (cur_div_reg < div_reg)
229 writel(div_reg, bank->reg_base +
231 rockchip_gpio_writel_bit(bank, offset, 1,
235 rockchip_gpio_writel_bit(bank, offset, 1, reg->debounce);
237 if (div_debounce_support)
238 rockchip_gpio_writel_bit(bank, offset, 0,
241 rockchip_gpio_writel_bit(bank, offset, 0, reg->debounce);
244 raw_spin_unlock_irqrestore(&bank->slock, flags);
246 /* Enable or disable dbclk at last */
247 if (div_debounce_support) {
249 clk_prepare_enable(bank->db_clk);
251 clk_disable_unprepare(bank->db_clk);
257 static int rockchip_gpio_direction_input(struct gpio_chip *gc,
260 return rockchip_gpio_set_direction(gc, offset, true);
263 static int rockchip_gpio_direction_output(struct gpio_chip *gc,
264 unsigned int offset, int value)
266 rockchip_gpio_set(gc, offset, value);
268 return rockchip_gpio_set_direction(gc, offset, false);
272 * gpiolib set_config callback function. The setting of the pin
273 * mux function as 'gpio output' will be handled by the pinctrl subsystem
276 static int rockchip_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
277 unsigned long config)
279 enum pin_config_param param = pinconf_to_config_param(config);
282 case PIN_CONFIG_INPUT_DEBOUNCE:
283 rockchip_gpio_set_debounce(gc, offset, true);
285 * Rockchip's gpio could only support up to one period
286 * of the debounce clock(pclk), which is far away from
287 * satisftying the requirement, as pclk is usually near
288 * 100MHz shared by all peripherals. So the fact is it
289 * has crippled debounce capability could only be useful
290 * to prevent any spurious glitches from waking up the system
291 * if the gpio is conguired as wakeup interrupt source. Let's
292 * still return -ENOTSUPP as before, to make sure the caller
293 * of gpiod_set_debounce won't change its behaviour.
302 * gpiod_to_irq() callback function. Creates a mapping between a GPIO pin
303 * and a virtual IRQ, if not already present.
305 static int rockchip_gpio_to_irq(struct gpio_chip *gc, unsigned int offset)
307 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
313 virq = irq_create_mapping(bank->domain, offset);
315 return (virq) ? : -ENXIO;
318 static const struct gpio_chip rockchip_gpiolib_chip = {
319 .request = gpiochip_generic_request,
320 .free = gpiochip_generic_free,
321 .set = rockchip_gpio_set,
322 .get = rockchip_gpio_get,
323 .get_direction = rockchip_gpio_get_direction,
324 .direction_input = rockchip_gpio_direction_input,
325 .direction_output = rockchip_gpio_direction_output,
326 .set_config = rockchip_gpio_set_config,
327 .to_irq = rockchip_gpio_to_irq,
328 .owner = THIS_MODULE,
331 static void rockchip_irq_demux(struct irq_desc *desc)
333 struct irq_chip *chip = irq_desc_get_chip(desc);
334 struct rockchip_pin_bank *bank = irq_desc_get_handler_data(desc);
335 unsigned long pending;
338 dev_dbg(bank->dev, "got irq for bank %s\n", bank->name);
340 chained_irq_enter(chip, desc);
342 pending = readl_relaxed(bank->reg_base + bank->gpio_regs->int_status);
343 for_each_set_bit(irq, &pending, 32) {
344 dev_dbg(bank->dev, "handling irq %d\n", irq);
347 * Triggering IRQ on both rising and falling edge
348 * needs manual intervention.
350 if (bank->toggle_edge_mode & BIT(irq)) {
351 u32 data, data_old, polarity;
354 data = readl_relaxed(bank->reg_base +
355 bank->gpio_regs->ext_port);
357 raw_spin_lock_irqsave(&bank->slock, flags);
359 polarity = readl_relaxed(bank->reg_base +
360 bank->gpio_regs->int_polarity);
362 polarity &= ~BIT(irq);
364 polarity |= BIT(irq);
367 bank->gpio_regs->int_polarity);
369 raw_spin_unlock_irqrestore(&bank->slock, flags);
372 data = readl_relaxed(bank->reg_base +
373 bank->gpio_regs->ext_port);
374 } while ((data & BIT(irq)) != (data_old & BIT(irq)));
377 generic_handle_domain_irq(bank->domain, irq);
380 chained_irq_exit(chip, desc);
383 static int rockchip_irq_set_type(struct irq_data *d, unsigned int type)
385 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
386 struct rockchip_pin_bank *bank = gc->private;
387 u32 mask = BIT(d->hwirq);
394 raw_spin_lock_irqsave(&bank->slock, flags);
396 rockchip_gpio_writel_bit(bank, d->hwirq, 0,
397 bank->gpio_regs->port_ddr);
399 raw_spin_unlock_irqrestore(&bank->slock, flags);
401 if (type & IRQ_TYPE_EDGE_BOTH)
402 irq_set_handler_locked(d, handle_edge_irq);
404 irq_set_handler_locked(d, handle_level_irq);
406 raw_spin_lock_irqsave(&bank->slock, flags);
408 level = rockchip_gpio_readl(bank, bank->gpio_regs->int_type);
409 polarity = rockchip_gpio_readl(bank, bank->gpio_regs->int_polarity);
411 if (type == IRQ_TYPE_EDGE_BOTH) {
412 if (bank->gpio_type == GPIO_TYPE_V2) {
413 rockchip_gpio_writel_bit(bank, d->hwirq, 1,
414 bank->gpio_regs->int_bothedge);
417 bank->toggle_edge_mode |= mask;
421 * Determine gpio state. If 1 next interrupt should be
422 * low otherwise high.
424 data = readl(bank->reg_base + bank->gpio_regs->ext_port);
431 if (bank->gpio_type == GPIO_TYPE_V2) {
432 rockchip_gpio_writel_bit(bank, d->hwirq, 0,
433 bank->gpio_regs->int_bothedge);
435 bank->toggle_edge_mode &= ~mask;
438 case IRQ_TYPE_EDGE_RISING:
442 case IRQ_TYPE_EDGE_FALLING:
446 case IRQ_TYPE_LEVEL_HIGH:
450 case IRQ_TYPE_LEVEL_LOW:
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 int rockchip_irq_reqres(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 return gpiochip_reqres_irq(&bank->gpio_chip, d->hwirq);
476 static void rockchip_irq_relres(struct irq_data *d)
478 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
479 struct rockchip_pin_bank *bank = gc->private;
481 gpiochip_relres_irq(&bank->gpio_chip, d->hwirq);
484 static void rockchip_irq_suspend(struct irq_data *d)
486 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
487 struct rockchip_pin_bank *bank = gc->private;
489 bank->saved_masks = irq_reg_readl(gc, bank->gpio_regs->int_mask);
490 irq_reg_writel(gc, ~gc->wake_active, bank->gpio_regs->int_mask);
493 static void rockchip_irq_resume(struct irq_data *d)
495 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
496 struct rockchip_pin_bank *bank = gc->private;
498 irq_reg_writel(gc, bank->saved_masks, bank->gpio_regs->int_mask);
501 static void rockchip_irq_enable(struct irq_data *d)
503 irq_gc_mask_clr_bit(d);
506 static void rockchip_irq_disable(struct irq_data *d)
508 irq_gc_mask_set_bit(d);
511 static int rockchip_interrupts_register(struct rockchip_pin_bank *bank)
513 unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
514 struct irq_chip_generic *gc;
517 bank->domain = irq_domain_add_linear(bank->of_node, 32,
518 &irq_generic_chip_ops, NULL);
520 dev_warn(bank->dev, "could not init irq domain for bank %s\n",
525 ret = irq_alloc_domain_generic_chips(bank->domain, 32, 1,
530 dev_err(bank->dev, "could not alloc generic chips for bank %s\n",
532 irq_domain_remove(bank->domain);
536 gc = irq_get_domain_generic_chip(bank->domain, 0);
537 if (bank->gpio_type == GPIO_TYPE_V2) {
538 gc->reg_writel = gpio_writel_v2;
539 gc->reg_readl = gpio_readl_v2;
542 gc->reg_base = bank->reg_base;
544 gc->chip_types[0].regs.mask = bank->gpio_regs->int_mask;
545 gc->chip_types[0].regs.ack = bank->gpio_regs->port_eoi;
546 gc->chip_types[0].chip.irq_ack = irq_gc_ack_set_bit;
547 gc->chip_types[0].chip.irq_mask = irq_gc_mask_set_bit;
548 gc->chip_types[0].chip.irq_unmask = irq_gc_mask_clr_bit;
549 gc->chip_types[0].chip.irq_enable = rockchip_irq_enable;
550 gc->chip_types[0].chip.irq_disable = rockchip_irq_disable;
551 gc->chip_types[0].chip.irq_set_wake = irq_gc_set_wake;
552 gc->chip_types[0].chip.irq_suspend = rockchip_irq_suspend;
553 gc->chip_types[0].chip.irq_resume = rockchip_irq_resume;
554 gc->chip_types[0].chip.irq_set_type = rockchip_irq_set_type;
555 gc->chip_types[0].chip.irq_request_resources = rockchip_irq_reqres;
556 gc->chip_types[0].chip.irq_release_resources = rockchip_irq_relres;
557 gc->wake_enabled = IRQ_MSK(bank->nr_pins);
560 * Linux assumes that all interrupts start out disabled/masked.
561 * Our driver only uses the concept of masked and always keeps
562 * things enabled, so for us that's all masked and all enabled.
564 rockchip_gpio_writel(bank, 0xffffffff, bank->gpio_regs->int_mask);
565 rockchip_gpio_writel(bank, 0xffffffff, bank->gpio_regs->port_eoi);
566 rockchip_gpio_writel(bank, 0xffffffff, bank->gpio_regs->int_en);
567 gc->mask_cache = 0xffffffff;
569 irq_set_chained_handler_and_data(bank->irq,
570 rockchip_irq_demux, bank);
575 static int rockchip_gpiolib_register(struct rockchip_pin_bank *bank)
577 struct gpio_chip *gc;
580 bank->gpio_chip = rockchip_gpiolib_chip;
582 gc = &bank->gpio_chip;
583 gc->base = bank->pin_base;
584 gc->ngpio = bank->nr_pins;
585 gc->label = bank->name;
586 gc->parent = bank->dev;
588 ret = gpiochip_add_data(gc, bank);
590 dev_err(bank->dev, "failed to add gpiochip %s, %d\n",
596 * For DeviceTree-supported systems, the gpio core checks the
597 * pinctrl's device node for the "gpio-ranges" property.
598 * If it is present, it takes care of adding the pin ranges
599 * for the driver. In this case the driver can skip ahead.
601 * In order to remain compatible with older, existing DeviceTree
602 * files which don't set the "gpio-ranges" property or systems that
603 * utilize ACPI the driver has to call gpiochip_add_pin_range().
605 if (!of_property_read_bool(bank->of_node, "gpio-ranges")) {
606 struct device_node *pctlnp = of_get_parent(bank->of_node);
607 struct pinctrl_dev *pctldev = NULL;
612 pctldev = of_pinctrl_get(pctlnp);
617 ret = gpiochip_add_pin_range(gc, dev_name(pctldev->dev), 0,
618 gc->base, gc->ngpio);
620 dev_err(bank->dev, "Failed to add pin range\n");
625 ret = rockchip_interrupts_register(bank);
627 dev_err(bank->dev, "failed to register interrupt, %d\n", ret);
634 gpiochip_remove(&bank->gpio_chip);
639 static int rockchip_get_bank_data(struct rockchip_pin_bank *bank)
644 if (of_address_to_resource(bank->of_node, 0, &res)) {
645 dev_err(bank->dev, "cannot find IO resource for bank\n");
649 bank->reg_base = devm_ioremap_resource(bank->dev, &res);
650 if (IS_ERR(bank->reg_base))
651 return PTR_ERR(bank->reg_base);
653 bank->irq = irq_of_parse_and_map(bank->of_node, 0);
657 bank->clk = of_clk_get(bank->of_node, 0);
658 if (IS_ERR(bank->clk))
659 return PTR_ERR(bank->clk);
661 clk_prepare_enable(bank->clk);
662 id = readl(bank->reg_base + gpio_regs_v2.version_id);
664 /* If not gpio v2, that is default to v1. */
665 if (id == GPIO_TYPE_V2 || id == GPIO_TYPE_V2_1) {
666 bank->gpio_regs = &gpio_regs_v2;
667 bank->gpio_type = GPIO_TYPE_V2;
668 bank->db_clk = of_clk_get(bank->of_node, 1);
669 if (IS_ERR(bank->db_clk)) {
670 dev_err(bank->dev, "cannot find debounce clk\n");
671 clk_disable_unprepare(bank->clk);
675 bank->gpio_regs = &gpio_regs_v1;
676 bank->gpio_type = GPIO_TYPE_V1;
682 static struct rockchip_pin_bank *
683 rockchip_gpio_find_bank(struct pinctrl_dev *pctldev, int id)
685 struct rockchip_pinctrl *info;
686 struct rockchip_pin_bank *bank;
689 info = pinctrl_dev_get_drvdata(pctldev);
690 bank = info->ctrl->pin_banks;
691 for (i = 0; i < info->ctrl->nr_banks; i++, bank++) {
692 if (bank->bank_num == id) {
698 return found ? bank : NULL;
701 static int rockchip_gpio_probe(struct platform_device *pdev)
703 struct device *dev = &pdev->dev;
704 struct device_node *np = dev->of_node;
705 struct device_node *pctlnp = of_get_parent(np);
706 struct pinctrl_dev *pctldev = NULL;
707 struct rockchip_pin_bank *bank = NULL;
708 struct rockchip_pin_deferred *cfg;
715 pctldev = of_pinctrl_get(pctlnp);
717 return -EPROBE_DEFER;
719 id = of_alias_get_id(np, "gpio");
723 bank = rockchip_gpio_find_bank(pctldev, id);
730 raw_spin_lock_init(&bank->slock);
732 ret = rockchip_get_bank_data(bank);
737 * Prevent clashes with a deferred output setting
738 * being added right at this moment.
740 mutex_lock(&bank->deferred_lock);
742 ret = rockchip_gpiolib_register(bank);
744 clk_disable_unprepare(bank->clk);
745 mutex_unlock(&bank->deferred_lock);
749 while (!list_empty(&bank->deferred_pins)) {
750 cfg = list_first_entry(&bank->deferred_pins,
751 struct rockchip_pin_deferred, head);
752 list_del(&cfg->head);
754 switch (cfg->param) {
755 case PIN_CONFIG_OUTPUT:
756 ret = rockchip_gpio_direction_output(&bank->gpio_chip, cfg->pin, cfg->arg);
758 dev_warn(dev, "setting output pin %u to %u failed\n", cfg->pin,
761 case PIN_CONFIG_INPUT_ENABLE:
762 ret = rockchip_gpio_direction_input(&bank->gpio_chip, cfg->pin);
764 dev_warn(dev, "setting input pin %u failed\n", cfg->pin);
767 dev_warn(dev, "unknown deferred config param %d\n", cfg->param);
773 mutex_unlock(&bank->deferred_lock);
775 platform_set_drvdata(pdev, bank);
776 dev_info(dev, "probed %pOF\n", np);
781 static int rockchip_gpio_remove(struct platform_device *pdev)
783 struct rockchip_pin_bank *bank = platform_get_drvdata(pdev);
785 clk_disable_unprepare(bank->clk);
786 gpiochip_remove(&bank->gpio_chip);
791 static const struct of_device_id rockchip_gpio_match[] = {
792 { .compatible = "rockchip,gpio-bank", },
793 { .compatible = "rockchip,rk3188-gpio-bank0" },
797 static struct platform_driver rockchip_gpio_driver = {
798 .probe = rockchip_gpio_probe,
799 .remove = rockchip_gpio_remove,
801 .name = "rockchip-gpio",
802 .of_match_table = rockchip_gpio_match,
806 static int __init rockchip_gpio_init(void)
808 return platform_driver_register(&rockchip_gpio_driver);
810 postcore_initcall(rockchip_gpio_init);
812 static void __exit rockchip_gpio_exit(void)
814 platform_driver_unregister(&rockchip_gpio_driver);
816 module_exit(rockchip_gpio_exit);
818 MODULE_DESCRIPTION("Rockchip gpio driver");
819 MODULE_ALIAS("platform:rockchip-gpio");
820 MODULE_LICENSE("GPL v2");
821 MODULE_DEVICE_TABLE(of, rockchip_gpio_match);