1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2013 MundoReader S.L.
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_irq.h>
21 #include <linux/pinctrl/consumer.h>
22 #include <linux/pinctrl/pinconf-generic.h>
23 #include <linux/platform_device.h>
24 #include <linux/regmap.h>
26 #include "../pinctrl/core.h"
27 #include "../pinctrl/pinctrl-rockchip.h"
31 * Bits [31:24] - Major Version
32 * Bits [23:16] - Minor Version
33 * Bits [15:0] - Revision Number
35 #define GPIO_TYPE_V1 (0) /* GPIO Version ID reserved */
36 #define GPIO_TYPE_V2 (0x01000C2B)
37 #define GPIO_TYPE_V2_1 (0x0101157C)
38 #define GPIO_TYPE_V2_2 (0x010219C8)
40 static const struct rockchip_gpio_regs gpio_regs_v1 = {
48 .int_rawstatus = 0x44,
54 static const struct rockchip_gpio_regs gpio_regs_v2 = {
63 .int_rawstatus = 0x58,
66 .dbclk_div_con = 0x48,
72 static inline void gpio_writel_v2(u32 val, void __iomem *reg)
74 writel((val & 0xffff) | 0xffff0000, reg);
75 writel((val >> 16) | 0xffff0000, reg + 0x4);
78 static inline u32 gpio_readl_v2(void __iomem *reg)
80 return readl(reg + 0x4) << 16 | readl(reg);
83 static inline void rockchip_gpio_writel(struct rockchip_pin_bank *bank,
84 u32 value, unsigned int offset)
86 void __iomem *reg = bank->reg_base + offset;
88 if (bank->gpio_type == GPIO_TYPE_V2)
89 gpio_writel_v2(value, reg);
94 static inline u32 rockchip_gpio_readl(struct rockchip_pin_bank *bank,
97 void __iomem *reg = bank->reg_base + offset;
100 if (bank->gpio_type == GPIO_TYPE_V2)
101 value = gpio_readl_v2(reg);
108 static inline void rockchip_gpio_writel_bit(struct rockchip_pin_bank *bank,
112 void __iomem *reg = bank->reg_base + offset;
115 if (bank->gpio_type == GPIO_TYPE_V2) {
117 data = BIT(bit % 16) | BIT(bit % 16 + 16);
119 data = BIT(bit % 16 + 16);
120 writel(data, bit >= 16 ? reg + 0x4 : reg);
130 static inline u32 rockchip_gpio_readl_bit(struct rockchip_pin_bank *bank,
131 u32 bit, unsigned int offset)
133 void __iomem *reg = bank->reg_base + offset;
136 if (bank->gpio_type == GPIO_TYPE_V2) {
137 data = readl(bit >= 16 ? reg + 0x4 : reg);
147 static int rockchip_gpio_get_direction(struct gpio_chip *chip,
150 struct rockchip_pin_bank *bank = gpiochip_get_data(chip);
153 data = rockchip_gpio_readl_bit(bank, offset, bank->gpio_regs->port_ddr);
155 return GPIO_LINE_DIRECTION_OUT;
157 return GPIO_LINE_DIRECTION_IN;
160 static int rockchip_gpio_set_direction(struct gpio_chip *chip,
161 unsigned int offset, bool input)
163 struct rockchip_pin_bank *bank = gpiochip_get_data(chip);
165 u32 data = input ? 0 : 1;
169 pinctrl_gpio_direction_input(chip, offset);
171 pinctrl_gpio_direction_output(chip, offset);
173 raw_spin_lock_irqsave(&bank->slock, flags);
174 rockchip_gpio_writel_bit(bank, offset, data, bank->gpio_regs->port_ddr);
175 raw_spin_unlock_irqrestore(&bank->slock, flags);
180 static void rockchip_gpio_set(struct gpio_chip *gc, unsigned int offset,
183 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
186 raw_spin_lock_irqsave(&bank->slock, flags);
187 rockchip_gpio_writel_bit(bank, offset, value, bank->gpio_regs->port_dr);
188 raw_spin_unlock_irqrestore(&bank->slock, flags);
191 static int rockchip_gpio_get(struct gpio_chip *gc, unsigned int offset)
193 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
196 data = readl(bank->reg_base + bank->gpio_regs->ext_port);
203 static int rockchip_gpio_set_debounce(struct gpio_chip *gc,
205 unsigned int debounce)
207 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
208 const struct rockchip_gpio_regs *reg = bank->gpio_regs;
209 unsigned long flags, div_reg, freq, max_debounce;
210 bool div_debounce_support;
211 unsigned int cur_div_reg;
214 if (bank->gpio_type == GPIO_TYPE_V2 && !IS_ERR(bank->db_clk)) {
215 div_debounce_support = true;
216 freq = clk_get_rate(bank->db_clk);
217 max_debounce = (GENMASK(23, 0) + 1) * 2 * 1000000 / freq;
218 if (debounce > max_debounce)
221 div = debounce * freq;
222 div_reg = DIV_ROUND_CLOSEST_ULL(div, 2 * USEC_PER_SEC) - 1;
224 div_debounce_support = false;
227 raw_spin_lock_irqsave(&bank->slock, flags);
229 /* Only the v1 needs to configure div_en and div_con for dbclk */
231 if (div_debounce_support) {
232 /* Configure the max debounce from consumers */
233 cur_div_reg = readl(bank->reg_base +
235 if (cur_div_reg < div_reg)
236 writel(div_reg, bank->reg_base +
238 rockchip_gpio_writel_bit(bank, offset, 1,
242 rockchip_gpio_writel_bit(bank, offset, 1, reg->debounce);
244 if (div_debounce_support)
245 rockchip_gpio_writel_bit(bank, offset, 0,
248 rockchip_gpio_writel_bit(bank, offset, 0, reg->debounce);
251 raw_spin_unlock_irqrestore(&bank->slock, flags);
253 /* Enable or disable dbclk at last */
254 if (div_debounce_support) {
256 clk_prepare_enable(bank->db_clk);
258 clk_disable_unprepare(bank->db_clk);
264 static int rockchip_gpio_direction_input(struct gpio_chip *gc,
267 return rockchip_gpio_set_direction(gc, offset, true);
270 static int rockchip_gpio_direction_output(struct gpio_chip *gc,
271 unsigned int offset, int value)
273 rockchip_gpio_set(gc, offset, value);
275 return rockchip_gpio_set_direction(gc, offset, false);
279 * gpiolib set_config callback function. The setting of the pin
280 * mux function as 'gpio output' will be handled by the pinctrl subsystem
283 static int rockchip_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
284 unsigned long config)
286 enum pin_config_param param = pinconf_to_config_param(config);
289 case PIN_CONFIG_INPUT_DEBOUNCE:
290 rockchip_gpio_set_debounce(gc, offset, true);
292 * Rockchip's gpio could only support up to one period
293 * of the debounce clock(pclk), which is far away from
294 * satisftying the requirement, as pclk is usually near
295 * 100MHz shared by all peripherals. So the fact is it
296 * has crippled debounce capability could only be useful
297 * to prevent any spurious glitches from waking up the system
298 * if the gpio is conguired as wakeup interrupt source. Let's
299 * still return -ENOTSUPP as before, to make sure the caller
300 * of gpiod_set_debounce won't change its behaviour.
309 * gpiod_to_irq() callback function. Creates a mapping between a GPIO pin
310 * and a virtual IRQ, if not already present.
312 static int rockchip_gpio_to_irq(struct gpio_chip *gc, unsigned int offset)
314 struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
320 virq = irq_create_mapping(bank->domain, offset);
322 return (virq) ? : -ENXIO;
325 static const struct gpio_chip rockchip_gpiolib_chip = {
326 .request = gpiochip_generic_request,
327 .free = gpiochip_generic_free,
328 .set = rockchip_gpio_set,
329 .get = rockchip_gpio_get,
330 .get_direction = rockchip_gpio_get_direction,
331 .direction_input = rockchip_gpio_direction_input,
332 .direction_output = rockchip_gpio_direction_output,
333 .set_config = rockchip_gpio_set_config,
334 .to_irq = rockchip_gpio_to_irq,
335 .owner = THIS_MODULE,
338 static void rockchip_irq_demux(struct irq_desc *desc)
340 struct irq_chip *chip = irq_desc_get_chip(desc);
341 struct rockchip_pin_bank *bank = irq_desc_get_handler_data(desc);
342 unsigned long pending;
345 dev_dbg(bank->dev, "got irq for bank %s\n", bank->name);
347 chained_irq_enter(chip, desc);
349 pending = readl_relaxed(bank->reg_base + bank->gpio_regs->int_status);
350 for_each_set_bit(irq, &pending, 32) {
351 dev_dbg(bank->dev, "handling irq %d\n", irq);
354 * Triggering IRQ on both rising and falling edge
355 * needs manual intervention.
357 if (bank->toggle_edge_mode & BIT(irq)) {
358 u32 data, data_old, polarity;
361 data = readl_relaxed(bank->reg_base +
362 bank->gpio_regs->ext_port);
364 raw_spin_lock_irqsave(&bank->slock, flags);
366 polarity = readl_relaxed(bank->reg_base +
367 bank->gpio_regs->int_polarity);
369 polarity &= ~BIT(irq);
371 polarity |= BIT(irq);
374 bank->gpio_regs->int_polarity);
376 raw_spin_unlock_irqrestore(&bank->slock, flags);
379 data = readl_relaxed(bank->reg_base +
380 bank->gpio_regs->ext_port);
381 } while ((data & BIT(irq)) != (data_old & BIT(irq)));
384 generic_handle_domain_irq(bank->domain, irq);
387 chained_irq_exit(chip, desc);
390 static int rockchip_irq_set_type(struct irq_data *d, unsigned int type)
392 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
393 struct rockchip_pin_bank *bank = gc->private;
394 u32 mask = BIT(d->hwirq);
401 raw_spin_lock_irqsave(&bank->slock, flags);
403 rockchip_gpio_writel_bit(bank, d->hwirq, 0,
404 bank->gpio_regs->port_ddr);
406 raw_spin_unlock_irqrestore(&bank->slock, flags);
408 if (type & IRQ_TYPE_EDGE_BOTH)
409 irq_set_handler_locked(d, handle_edge_irq);
411 irq_set_handler_locked(d, handle_level_irq);
413 raw_spin_lock_irqsave(&bank->slock, flags);
415 level = rockchip_gpio_readl(bank, bank->gpio_regs->int_type);
416 polarity = rockchip_gpio_readl(bank, bank->gpio_regs->int_polarity);
418 if (type == IRQ_TYPE_EDGE_BOTH) {
419 if (bank->gpio_type == GPIO_TYPE_V2) {
420 rockchip_gpio_writel_bit(bank, d->hwirq, 1,
421 bank->gpio_regs->int_bothedge);
424 bank->toggle_edge_mode |= mask;
428 * Determine gpio state. If 1 next interrupt should be
429 * low otherwise high.
431 data = readl(bank->reg_base + bank->gpio_regs->ext_port);
438 if (bank->gpio_type == GPIO_TYPE_V2) {
439 rockchip_gpio_writel_bit(bank, d->hwirq, 0,
440 bank->gpio_regs->int_bothedge);
442 bank->toggle_edge_mode &= ~mask;
445 case IRQ_TYPE_EDGE_RISING:
449 case IRQ_TYPE_EDGE_FALLING:
453 case IRQ_TYPE_LEVEL_HIGH:
457 case IRQ_TYPE_LEVEL_LOW:
467 rockchip_gpio_writel(bank, level, bank->gpio_regs->int_type);
468 rockchip_gpio_writel(bank, polarity, bank->gpio_regs->int_polarity);
470 raw_spin_unlock_irqrestore(&bank->slock, flags);
475 static int rockchip_irq_reqres(struct irq_data *d)
477 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
478 struct rockchip_pin_bank *bank = gc->private;
480 return gpiochip_reqres_irq(&bank->gpio_chip, d->hwirq);
483 static void rockchip_irq_relres(struct irq_data *d)
485 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
486 struct rockchip_pin_bank *bank = gc->private;
488 gpiochip_relres_irq(&bank->gpio_chip, d->hwirq);
491 static void rockchip_irq_suspend(struct irq_data *d)
493 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
494 struct rockchip_pin_bank *bank = gc->private;
496 bank->saved_masks = irq_reg_readl(gc, bank->gpio_regs->int_mask);
497 irq_reg_writel(gc, ~gc->wake_active, bank->gpio_regs->int_mask);
500 static void rockchip_irq_resume(struct irq_data *d)
502 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
503 struct rockchip_pin_bank *bank = gc->private;
505 irq_reg_writel(gc, bank->saved_masks, bank->gpio_regs->int_mask);
508 static void rockchip_irq_enable(struct irq_data *d)
510 irq_gc_mask_clr_bit(d);
513 static void rockchip_irq_disable(struct irq_data *d)
515 irq_gc_mask_set_bit(d);
518 static int rockchip_interrupts_register(struct rockchip_pin_bank *bank)
520 unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
521 struct irq_chip_generic *gc;
524 bank->domain = irq_domain_add_linear(bank->of_node, 32,
525 &irq_generic_chip_ops, NULL);
527 dev_warn(bank->dev, "could not init irq domain for bank %s\n",
532 ret = irq_alloc_domain_generic_chips(bank->domain, 32, 1,
537 dev_err(bank->dev, "could not alloc generic chips for bank %s\n",
539 irq_domain_remove(bank->domain);
543 gc = irq_get_domain_generic_chip(bank->domain, 0);
544 if (bank->gpio_type == GPIO_TYPE_V2) {
545 gc->reg_writel = gpio_writel_v2;
546 gc->reg_readl = gpio_readl_v2;
549 gc->reg_base = bank->reg_base;
551 gc->chip_types[0].regs.mask = bank->gpio_regs->int_mask;
552 gc->chip_types[0].regs.ack = bank->gpio_regs->port_eoi;
553 gc->chip_types[0].chip.irq_ack = irq_gc_ack_set_bit;
554 gc->chip_types[0].chip.irq_mask = irq_gc_mask_set_bit;
555 gc->chip_types[0].chip.irq_unmask = irq_gc_mask_clr_bit;
556 gc->chip_types[0].chip.irq_enable = rockchip_irq_enable;
557 gc->chip_types[0].chip.irq_disable = rockchip_irq_disable;
558 gc->chip_types[0].chip.irq_set_wake = irq_gc_set_wake;
559 gc->chip_types[0].chip.irq_suspend = rockchip_irq_suspend;
560 gc->chip_types[0].chip.irq_resume = rockchip_irq_resume;
561 gc->chip_types[0].chip.irq_set_type = rockchip_irq_set_type;
562 gc->chip_types[0].chip.irq_request_resources = rockchip_irq_reqres;
563 gc->chip_types[0].chip.irq_release_resources = rockchip_irq_relres;
564 gc->wake_enabled = IRQ_MSK(bank->nr_pins);
567 * Linux assumes that all interrupts start out disabled/masked.
568 * Our driver only uses the concept of masked and always keeps
569 * things enabled, so for us that's all masked and all enabled.
571 rockchip_gpio_writel(bank, 0xffffffff, bank->gpio_regs->int_mask);
572 rockchip_gpio_writel(bank, 0xffffffff, bank->gpio_regs->port_eoi);
573 rockchip_gpio_writel(bank, 0xffffffff, bank->gpio_regs->int_en);
574 gc->mask_cache = 0xffffffff;
576 irq_set_chained_handler_and_data(bank->irq,
577 rockchip_irq_demux, bank);
582 static int rockchip_gpiolib_register(struct rockchip_pin_bank *bank)
584 struct gpio_chip *gc;
587 bank->gpio_chip = rockchip_gpiolib_chip;
589 gc = &bank->gpio_chip;
590 gc->base = bank->pin_base;
591 gc->ngpio = bank->nr_pins;
592 gc->label = bank->name;
593 gc->parent = bank->dev;
595 ret = gpiochip_add_data(gc, bank);
597 dev_err(bank->dev, "failed to add gpiochip %s, %d\n",
603 * For DeviceTree-supported systems, the gpio core checks the
604 * pinctrl's device node for the "gpio-ranges" property.
605 * If it is present, it takes care of adding the pin ranges
606 * for the driver. In this case the driver can skip ahead.
608 * In order to remain compatible with older, existing DeviceTree
609 * files which don't set the "gpio-ranges" property or systems that
610 * utilize ACPI the driver has to call gpiochip_add_pin_range().
612 if (!of_property_present(bank->of_node, "gpio-ranges")) {
613 struct device_node *pctlnp = of_get_parent(bank->of_node);
614 struct pinctrl_dev *pctldev = NULL;
619 pctldev = of_pinctrl_get(pctlnp);
624 ret = gpiochip_add_pin_range(gc, dev_name(pctldev->dev), 0,
625 gc->base, gc->ngpio);
627 dev_err(bank->dev, "Failed to add pin range\n");
632 ret = rockchip_interrupts_register(bank);
634 dev_err(bank->dev, "failed to register interrupt, %d\n", ret);
641 gpiochip_remove(&bank->gpio_chip);
646 static int rockchip_get_bank_data(struct rockchip_pin_bank *bank)
651 if (of_address_to_resource(bank->of_node, 0, &res)) {
652 dev_err(bank->dev, "cannot find IO resource for bank\n");
656 bank->reg_base = devm_ioremap_resource(bank->dev, &res);
657 if (IS_ERR(bank->reg_base))
658 return PTR_ERR(bank->reg_base);
660 bank->irq = irq_of_parse_and_map(bank->of_node, 0);
664 bank->clk = of_clk_get(bank->of_node, 0);
665 if (IS_ERR(bank->clk))
666 return PTR_ERR(bank->clk);
668 clk_prepare_enable(bank->clk);
669 id = readl(bank->reg_base + gpio_regs_v2.version_id);
675 bank->gpio_regs = &gpio_regs_v2;
676 bank->gpio_type = GPIO_TYPE_V2;
677 bank->db_clk = of_clk_get(bank->of_node, 1);
678 if (IS_ERR(bank->db_clk)) {
679 dev_err(bank->dev, "cannot find debounce clk\n");
680 clk_disable_unprepare(bank->clk);
685 bank->gpio_regs = &gpio_regs_v1;
686 bank->gpio_type = GPIO_TYPE_V1;
689 dev_err(bank->dev, "unsupported version ID: 0x%08x\n", id);
696 static struct rockchip_pin_bank *
697 rockchip_gpio_find_bank(struct pinctrl_dev *pctldev, int id)
699 struct rockchip_pinctrl *info;
700 struct rockchip_pin_bank *bank;
703 info = pinctrl_dev_get_drvdata(pctldev);
704 bank = info->ctrl->pin_banks;
705 for (i = 0; i < info->ctrl->nr_banks; i++, bank++) {
706 if (bank->bank_num == id) {
712 return found ? bank : NULL;
715 static int rockchip_gpio_probe(struct platform_device *pdev)
717 struct device *dev = &pdev->dev;
718 struct device_node *np = dev->of_node;
719 struct device_node *pctlnp = of_get_parent(np);
720 struct pinctrl_dev *pctldev = NULL;
721 struct rockchip_pin_bank *bank = NULL;
722 struct rockchip_pin_deferred *cfg;
729 pctldev = of_pinctrl_get(pctlnp);
732 return -EPROBE_DEFER;
734 id = of_alias_get_id(np, "gpio");
738 bank = rockchip_gpio_find_bank(pctldev, id);
745 raw_spin_lock_init(&bank->slock);
747 ret = rockchip_get_bank_data(bank);
752 * Prevent clashes with a deferred output setting
753 * being added right at this moment.
755 mutex_lock(&bank->deferred_lock);
757 ret = rockchip_gpiolib_register(bank);
759 clk_disable_unprepare(bank->clk);
760 mutex_unlock(&bank->deferred_lock);
764 while (!list_empty(&bank->deferred_pins)) {
765 cfg = list_first_entry(&bank->deferred_pins,
766 struct rockchip_pin_deferred, head);
767 list_del(&cfg->head);
769 switch (cfg->param) {
770 case PIN_CONFIG_OUTPUT:
771 ret = rockchip_gpio_direction_output(&bank->gpio_chip, cfg->pin, cfg->arg);
773 dev_warn(dev, "setting output pin %u to %u failed\n", cfg->pin,
776 case PIN_CONFIG_INPUT_ENABLE:
777 ret = rockchip_gpio_direction_input(&bank->gpio_chip, cfg->pin);
779 dev_warn(dev, "setting input pin %u failed\n", cfg->pin);
782 dev_warn(dev, "unknown deferred config param %d\n", cfg->param);
788 mutex_unlock(&bank->deferred_lock);
790 platform_set_drvdata(pdev, bank);
791 dev_info(dev, "probed %pOF\n", np);
796 static void rockchip_gpio_remove(struct platform_device *pdev)
798 struct rockchip_pin_bank *bank = platform_get_drvdata(pdev);
800 clk_disable_unprepare(bank->clk);
801 gpiochip_remove(&bank->gpio_chip);
804 static const struct of_device_id rockchip_gpio_match[] = {
805 { .compatible = "rockchip,gpio-bank", },
806 { .compatible = "rockchip,rk3188-gpio-bank0" },
810 static struct platform_driver rockchip_gpio_driver = {
811 .probe = rockchip_gpio_probe,
812 .remove = rockchip_gpio_remove,
814 .name = "rockchip-gpio",
815 .of_match_table = rockchip_gpio_match,
819 static int __init rockchip_gpio_init(void)
821 return platform_driver_register(&rockchip_gpio_driver);
823 postcore_initcall(rockchip_gpio_init);
825 static void __exit rockchip_gpio_exit(void)
827 platform_driver_unregister(&rockchip_gpio_driver);
829 module_exit(rockchip_gpio_exit);
831 MODULE_DESCRIPTION("Rockchip gpio driver");
832 MODULE_ALIAS("platform:rockchip-gpio");
833 MODULE_LICENSE("GPL v2");
834 MODULE_DEVICE_TABLE(of, rockchip_gpio_match);