1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (C) 2014-2017 Broadcom
5 * This file contains the Broadcom Northstar Plus (NSP) GPIO driver that
6 * supports the chipCommonA GPIO controller. Basic PINCONF such as bias,
7 * pull up/down, slew and drive strength are also supported in this driver.
9 * Pins from the chipCommonA GPIO can be individually muxed to GPIO function,
10 * through the interaction with the NSP IOMUX controller.
13 #include <linux/gpio/driver.h>
14 #include <linux/interrupt.h>
16 #include <linux/ioport.h>
17 #include <linux/kernel.h>
18 #include <linux/of_address.h>
19 #include <linux/of_device.h>
20 #include <linux/of_irq.h>
21 #include <linux/pinctrl/pinconf.h>
22 #include <linux/pinctrl/pinconf-generic.h>
23 #include <linux/pinctrl/pinctrl.h>
24 #include <linux/slab.h>
26 #include "../pinctrl-utils.h"
28 #define NSP_CHIP_A_INT_STATUS 0x00
29 #define NSP_CHIP_A_INT_MASK 0x04
30 #define NSP_GPIO_DATA_IN 0x40
31 #define NSP_GPIO_DATA_OUT 0x44
32 #define NSP_GPIO_OUT_EN 0x48
33 #define NSP_GPIO_INT_POLARITY 0x50
34 #define NSP_GPIO_INT_MASK 0x54
35 #define NSP_GPIO_EVENT 0x58
36 #define NSP_GPIO_EVENT_INT_MASK 0x5c
37 #define NSP_GPIO_EVENT_INT_POLARITY 0x64
38 #define NSP_CHIP_A_GPIO_INT_BIT 0x01
40 /* I/O parameters offset for chipcommon A GPIO */
41 #define NSP_GPIO_DRV_CTRL 0x00
42 #define NSP_GPIO_HYSTERESIS_EN 0x10
43 #define NSP_GPIO_SLEW_RATE_EN 0x14
44 #define NSP_PULL_UP_EN 0x18
45 #define NSP_PULL_DOWN_EN 0x1c
46 #define GPIO_DRV_STRENGTH_BITS 0x03
51 * @dev: pointer to device
52 * @base: I/O register base for nsp GPIO controller
53 * @io_ctrl: I/O register base for PINCONF support outside the GPIO block
55 * @pctl: pointer to pinctrl_dev
56 * @pctldesc: pinctrl descriptor
57 * @lock: lock to protect access to I/O registers
62 void __iomem *io_ctrl;
63 struct irq_chip irqchip;
65 struct pinctrl_dev *pctl;
66 struct pinctrl_desc pctldesc;
76 * Mapping from PINCONF pins to GPIO pins is 1-to-1
78 static inline unsigned nsp_pin_to_gpio(unsigned pin)
84 * nsp_set_bit - set or clear one bit (corresponding to the GPIO pin) in a
87 * @nsp_gpio: nsp GPIO device
88 * @base_type: reg base to modify
89 * @reg: register offset
93 static inline void nsp_set_bit(struct nsp_gpio *chip, enum base_type address,
94 unsigned int reg, unsigned gpio, bool set)
97 void __iomem *base_address;
99 if (address == IO_CTRL)
100 base_address = chip->io_ctrl;
102 base_address = chip->base;
104 val = readl(base_address + reg);
110 writel(val, base_address + reg);
114 * nsp_get_bit - get one bit (corresponding to the GPIO pin) in a
117 static inline bool nsp_get_bit(struct nsp_gpio *chip, enum base_type address,
118 unsigned int reg, unsigned gpio)
120 if (address == IO_CTRL)
121 return !!(readl(chip->io_ctrl + reg) & BIT(gpio));
123 return !!(readl(chip->base + reg) & BIT(gpio));
126 static irqreturn_t nsp_gpio_irq_handler(int irq, void *data)
128 struct gpio_chip *gc = (struct gpio_chip *)data;
129 struct nsp_gpio *chip = gpiochip_get_data(gc);
131 unsigned long int_bits = 0;
134 /* go through the entire GPIOs and handle all interrupts */
135 int_status = readl(chip->base + NSP_CHIP_A_INT_STATUS);
136 if (int_status & NSP_CHIP_A_GPIO_INT_BIT) {
137 unsigned int event, level;
139 /* Get level and edge interrupts */
140 event = readl(chip->base + NSP_GPIO_EVENT_INT_MASK) &
141 readl(chip->base + NSP_GPIO_EVENT);
142 level = readl(chip->base + NSP_GPIO_DATA_IN) ^
143 readl(chip->base + NSP_GPIO_INT_POLARITY);
144 level &= readl(chip->base + NSP_GPIO_INT_MASK);
145 int_bits = level | event;
147 for_each_set_bit(bit, &int_bits, gc->ngpio)
148 generic_handle_domain_irq(gc->irq.domain, bit);
151 return int_bits ? IRQ_HANDLED : IRQ_NONE;
154 static void nsp_gpio_irq_ack(struct irq_data *d)
156 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
157 struct nsp_gpio *chip = gpiochip_get_data(gc);
158 unsigned gpio = d->hwirq;
162 trigger_type = irq_get_trigger_type(d->irq);
163 if (trigger_type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
164 writel(val, chip->base + NSP_GPIO_EVENT);
168 * nsp_gpio_irq_set_mask - mask/unmask a GPIO interrupt
171 * @unmask: mask/unmask GPIO interrupt
173 static void nsp_gpio_irq_set_mask(struct irq_data *d, bool unmask)
175 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
176 struct nsp_gpio *chip = gpiochip_get_data(gc);
177 unsigned gpio = d->hwirq;
180 trigger_type = irq_get_trigger_type(d->irq);
181 if (trigger_type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
182 nsp_set_bit(chip, REG, NSP_GPIO_EVENT_INT_MASK, gpio, unmask);
184 nsp_set_bit(chip, REG, NSP_GPIO_INT_MASK, gpio, unmask);
187 static void nsp_gpio_irq_mask(struct irq_data *d)
189 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
190 struct nsp_gpio *chip = gpiochip_get_data(gc);
193 raw_spin_lock_irqsave(&chip->lock, flags);
194 nsp_gpio_irq_set_mask(d, false);
195 raw_spin_unlock_irqrestore(&chip->lock, flags);
198 static void nsp_gpio_irq_unmask(struct irq_data *d)
200 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
201 struct nsp_gpio *chip = gpiochip_get_data(gc);
204 raw_spin_lock_irqsave(&chip->lock, flags);
205 nsp_gpio_irq_set_mask(d, true);
206 raw_spin_unlock_irqrestore(&chip->lock, flags);
209 static int nsp_gpio_irq_set_type(struct irq_data *d, unsigned int type)
211 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
212 struct nsp_gpio *chip = gpiochip_get_data(gc);
213 unsigned gpio = d->hwirq;
218 raw_spin_lock_irqsave(&chip->lock, flags);
219 falling = nsp_get_bit(chip, REG, NSP_GPIO_EVENT_INT_POLARITY, gpio);
220 level_low = nsp_get_bit(chip, REG, NSP_GPIO_INT_POLARITY, gpio);
222 switch (type & IRQ_TYPE_SENSE_MASK) {
223 case IRQ_TYPE_EDGE_RISING:
227 case IRQ_TYPE_EDGE_FALLING:
231 case IRQ_TYPE_LEVEL_HIGH:
235 case IRQ_TYPE_LEVEL_LOW:
240 dev_err(chip->dev, "invalid GPIO IRQ type 0x%x\n",
242 raw_spin_unlock_irqrestore(&chip->lock, flags);
246 nsp_set_bit(chip, REG, NSP_GPIO_EVENT_INT_POLARITY, gpio, falling);
247 nsp_set_bit(chip, REG, NSP_GPIO_INT_POLARITY, gpio, level_low);
249 if (type & IRQ_TYPE_EDGE_BOTH)
250 irq_set_handler_locked(d, handle_edge_irq);
252 irq_set_handler_locked(d, handle_level_irq);
254 raw_spin_unlock_irqrestore(&chip->lock, flags);
256 dev_dbg(chip->dev, "gpio:%u level_low:%s falling:%s\n", gpio,
257 level_low ? "true" : "false", falling ? "true" : "false");
261 static int nsp_gpio_direction_input(struct gpio_chip *gc, unsigned gpio)
263 struct nsp_gpio *chip = gpiochip_get_data(gc);
266 raw_spin_lock_irqsave(&chip->lock, flags);
267 nsp_set_bit(chip, REG, NSP_GPIO_OUT_EN, gpio, false);
268 raw_spin_unlock_irqrestore(&chip->lock, flags);
270 dev_dbg(chip->dev, "gpio:%u set input\n", gpio);
274 static int nsp_gpio_direction_output(struct gpio_chip *gc, unsigned gpio,
277 struct nsp_gpio *chip = gpiochip_get_data(gc);
280 raw_spin_lock_irqsave(&chip->lock, flags);
281 nsp_set_bit(chip, REG, NSP_GPIO_OUT_EN, gpio, true);
282 nsp_set_bit(chip, REG, NSP_GPIO_DATA_OUT, gpio, !!(val));
283 raw_spin_unlock_irqrestore(&chip->lock, flags);
285 dev_dbg(chip->dev, "gpio:%u set output, value:%d\n", gpio, val);
289 static int nsp_gpio_get_direction(struct gpio_chip *gc, unsigned gpio)
291 struct nsp_gpio *chip = gpiochip_get_data(gc);
295 raw_spin_lock_irqsave(&chip->lock, flags);
296 val = nsp_get_bit(chip, REG, NSP_GPIO_OUT_EN, gpio);
297 raw_spin_unlock_irqrestore(&chip->lock, flags);
302 static void nsp_gpio_set(struct gpio_chip *gc, unsigned gpio, int val)
304 struct nsp_gpio *chip = gpiochip_get_data(gc);
307 raw_spin_lock_irqsave(&chip->lock, flags);
308 nsp_set_bit(chip, REG, NSP_GPIO_DATA_OUT, gpio, !!(val));
309 raw_spin_unlock_irqrestore(&chip->lock, flags);
311 dev_dbg(chip->dev, "gpio:%u set, value:%d\n", gpio, val);
314 static int nsp_gpio_get(struct gpio_chip *gc, unsigned gpio)
316 struct nsp_gpio *chip = gpiochip_get_data(gc);
318 return !!(readl(chip->base + NSP_GPIO_DATA_IN) & BIT(gpio));
321 static int nsp_get_groups_count(struct pinctrl_dev *pctldev)
327 * Only one group: "gpio_grp", since this local pinctrl device only performs
328 * GPIO specific PINCONF configurations
330 static const char *nsp_get_group_name(struct pinctrl_dev *pctldev,
336 static const struct pinctrl_ops nsp_pctrl_ops = {
337 .get_groups_count = nsp_get_groups_count,
338 .get_group_name = nsp_get_group_name,
339 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
340 .dt_free_map = pinctrl_utils_free_map,
343 static int nsp_gpio_set_slew(struct nsp_gpio *chip, unsigned gpio, u32 slew)
346 nsp_set_bit(chip, IO_CTRL, NSP_GPIO_SLEW_RATE_EN, gpio, true);
348 nsp_set_bit(chip, IO_CTRL, NSP_GPIO_SLEW_RATE_EN, gpio, false);
353 static int nsp_gpio_set_pull(struct nsp_gpio *chip, unsigned gpio,
354 bool pull_up, bool pull_down)
358 raw_spin_lock_irqsave(&chip->lock, flags);
359 nsp_set_bit(chip, IO_CTRL, NSP_PULL_DOWN_EN, gpio, pull_down);
360 nsp_set_bit(chip, IO_CTRL, NSP_PULL_UP_EN, gpio, pull_up);
361 raw_spin_unlock_irqrestore(&chip->lock, flags);
363 dev_dbg(chip->dev, "gpio:%u set pullup:%d pulldown: %d\n",
364 gpio, pull_up, pull_down);
368 static void nsp_gpio_get_pull(struct nsp_gpio *chip, unsigned gpio,
369 bool *pull_up, bool *pull_down)
373 raw_spin_lock_irqsave(&chip->lock, flags);
374 *pull_up = nsp_get_bit(chip, IO_CTRL, NSP_PULL_UP_EN, gpio);
375 *pull_down = nsp_get_bit(chip, IO_CTRL, NSP_PULL_DOWN_EN, gpio);
376 raw_spin_unlock_irqrestore(&chip->lock, flags);
379 static int nsp_gpio_set_strength(struct nsp_gpio *chip, unsigned gpio,
382 u32 offset, shift, i;
386 /* make sure drive strength is supported */
387 if (strength < 2 || strength > 16 || (strength % 2))
391 offset = NSP_GPIO_DRV_CTRL;
392 dev_dbg(chip->dev, "gpio:%u set drive strength:%d mA\n", gpio,
394 raw_spin_lock_irqsave(&chip->lock, flags);
395 strength = (strength / 2) - 1;
396 for (i = GPIO_DRV_STRENGTH_BITS; i > 0; i--) {
397 val = readl(chip->io_ctrl + offset);
399 val |= ((strength >> (i-1)) & 0x1) << shift;
400 writel(val, chip->io_ctrl + offset);
403 raw_spin_unlock_irqrestore(&chip->lock, flags);
408 static int nsp_gpio_get_strength(struct nsp_gpio *chip, unsigned gpio,
411 unsigned int offset, shift;
416 offset = NSP_GPIO_DRV_CTRL;
419 raw_spin_lock_irqsave(&chip->lock, flags);
421 for (i = (GPIO_DRV_STRENGTH_BITS - 1); i >= 0; i--) {
422 val = readl(chip->io_ctrl + offset) & BIT(shift);
424 *strength += (val << i);
429 *strength = (*strength + 1) * 2;
430 raw_spin_unlock_irqrestore(&chip->lock, flags);
435 static int nsp_pin_config_group_get(struct pinctrl_dev *pctldev,
437 unsigned long *config)
442 static int nsp_pin_config_group_set(struct pinctrl_dev *pctldev,
444 unsigned long *configs, unsigned num_configs)
449 static int nsp_pin_config_get(struct pinctrl_dev *pctldev, unsigned pin,
450 unsigned long *config)
452 struct nsp_gpio *chip = pinctrl_dev_get_drvdata(pctldev);
453 enum pin_config_param param = pinconf_to_config_param(*config);
456 bool pull_up, pull_down;
459 gpio = nsp_pin_to_gpio(pin);
461 case PIN_CONFIG_BIAS_DISABLE:
462 nsp_gpio_get_pull(chip, gpio, &pull_up, &pull_down);
463 if ((pull_up == false) && (pull_down == false))
468 case PIN_CONFIG_BIAS_PULL_UP:
469 nsp_gpio_get_pull(chip, gpio, &pull_up, &pull_down);
475 case PIN_CONFIG_BIAS_PULL_DOWN:
476 nsp_gpio_get_pull(chip, gpio, &pull_up, &pull_down);
482 case PIN_CONFIG_DRIVE_STRENGTH:
483 ret = nsp_gpio_get_strength(chip, gpio, &arg);
486 *config = pinconf_to_config_packed(param, arg);
494 static int nsp_pin_config_set(struct pinctrl_dev *pctldev, unsigned pin,
495 unsigned long *configs, unsigned num_configs)
497 struct nsp_gpio *chip = pinctrl_dev_get_drvdata(pctldev);
498 enum pin_config_param param;
500 unsigned int i, gpio;
503 gpio = nsp_pin_to_gpio(pin);
504 for (i = 0; i < num_configs; i++) {
505 param = pinconf_to_config_param(configs[i]);
506 arg = pinconf_to_config_argument(configs[i]);
509 case PIN_CONFIG_BIAS_DISABLE:
510 ret = nsp_gpio_set_pull(chip, gpio, false, false);
515 case PIN_CONFIG_BIAS_PULL_UP:
516 ret = nsp_gpio_set_pull(chip, gpio, true, false);
521 case PIN_CONFIG_BIAS_PULL_DOWN:
522 ret = nsp_gpio_set_pull(chip, gpio, false, true);
527 case PIN_CONFIG_DRIVE_STRENGTH:
528 ret = nsp_gpio_set_strength(chip, gpio, arg);
533 case PIN_CONFIG_SLEW_RATE:
534 ret = nsp_gpio_set_slew(chip, gpio, arg);
540 dev_err(chip->dev, "invalid configuration\n");
549 static const struct pinconf_ops nsp_pconf_ops = {
551 .pin_config_get = nsp_pin_config_get,
552 .pin_config_set = nsp_pin_config_set,
553 .pin_config_group_get = nsp_pin_config_group_get,
554 .pin_config_group_set = nsp_pin_config_group_set,
558 * NSP GPIO controller supports some PINCONF related configurations such as
559 * pull up, pull down, slew and drive strength, when the pin is configured
562 * Here a local pinctrl device is created with simple 1-to-1 pin mapping to the
565 static int nsp_gpio_register_pinconf(struct nsp_gpio *chip)
567 struct pinctrl_desc *pctldesc = &chip->pctldesc;
568 struct pinctrl_pin_desc *pins;
569 struct gpio_chip *gc = &chip->gc;
572 pins = devm_kcalloc(chip->dev, gc->ngpio, sizeof(*pins), GFP_KERNEL);
575 for (i = 0; i < gc->ngpio; i++) {
577 pins[i].name = devm_kasprintf(chip->dev, GFP_KERNEL,
582 pctldesc->name = dev_name(chip->dev);
583 pctldesc->pctlops = &nsp_pctrl_ops;
584 pctldesc->pins = pins;
585 pctldesc->npins = gc->ngpio;
586 pctldesc->confops = &nsp_pconf_ops;
588 chip->pctl = devm_pinctrl_register(chip->dev, pctldesc, chip);
589 if (IS_ERR(chip->pctl)) {
590 dev_err(chip->dev, "unable to register pinctrl device\n");
591 return PTR_ERR(chip->pctl);
597 static const struct of_device_id nsp_gpio_of_match[] = {
598 {.compatible = "brcm,nsp-gpio-a",},
602 static int nsp_gpio_probe(struct platform_device *pdev)
604 struct device *dev = &pdev->dev;
605 struct nsp_gpio *chip;
606 struct gpio_chip *gc;
610 if (of_property_read_u32(pdev->dev.of_node, "ngpios", &val)) {
611 dev_err(&pdev->dev, "Missing ngpios OF property\n");
615 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
620 platform_set_drvdata(pdev, chip);
622 chip->base = devm_platform_ioremap_resource(pdev, 0);
623 if (IS_ERR(chip->base)) {
624 dev_err(dev, "unable to map I/O memory\n");
625 return PTR_ERR(chip->base);
628 chip->io_ctrl = devm_platform_ioremap_resource(pdev, 1);
629 if (IS_ERR(chip->io_ctrl)) {
630 dev_err(dev, "unable to map I/O memory\n");
631 return PTR_ERR(chip->io_ctrl);
634 raw_spin_lock_init(&chip->lock);
637 gc->can_sleep = false;
639 gc->label = dev_name(dev);
641 gc->request = gpiochip_generic_request;
642 gc->free = gpiochip_generic_free;
643 gc->direction_input = nsp_gpio_direction_input;
644 gc->direction_output = nsp_gpio_direction_output;
645 gc->get_direction = nsp_gpio_get_direction;
646 gc->set = nsp_gpio_set;
647 gc->get = nsp_gpio_get;
649 /* optional GPIO interrupt support */
650 irq = platform_get_irq(pdev, 0);
652 struct gpio_irq_chip *girq;
653 struct irq_chip *irqc;
655 irqc = &chip->irqchip;
656 irqc->name = "gpio-a";
657 irqc->irq_ack = nsp_gpio_irq_ack;
658 irqc->irq_mask = nsp_gpio_irq_mask;
659 irqc->irq_unmask = nsp_gpio_irq_unmask;
660 irqc->irq_set_type = nsp_gpio_irq_set_type;
662 val = readl(chip->base + NSP_CHIP_A_INT_MASK);
663 val = val | NSP_CHIP_A_GPIO_INT_BIT;
664 writel(val, (chip->base + NSP_CHIP_A_INT_MASK));
666 /* Install ISR for this GPIO controller. */
667 ret = devm_request_irq(dev, irq, nsp_gpio_irq_handler,
668 IRQF_SHARED, "gpio-a", &chip->gc);
670 dev_err(&pdev->dev, "Unable to request IRQ%d: %d\n",
675 girq = &chip->gc.irq;
677 /* This will let us handle the parent IRQ in the driver */
678 girq->parent_handler = NULL;
679 girq->num_parents = 0;
680 girq->parents = NULL;
681 girq->default_type = IRQ_TYPE_NONE;
682 girq->handler = handle_bad_irq;
685 ret = devm_gpiochip_add_data(dev, gc, chip);
687 dev_err(dev, "unable to add GPIO chip\n");
691 ret = nsp_gpio_register_pinconf(chip);
693 dev_err(dev, "unable to register pinconf\n");
700 static struct platform_driver nsp_gpio_driver = {
702 .name = "nsp-gpio-a",
703 .of_match_table = nsp_gpio_of_match,
705 .probe = nsp_gpio_probe,
708 static int __init nsp_gpio_init(void)
710 return platform_driver_register(&nsp_gpio_driver);
712 arch_initcall_sync(nsp_gpio_init);