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;
64 struct pinctrl_dev *pctl;
65 struct pinctrl_desc pctldesc;
75 * Mapping from PINCONF pins to GPIO pins is 1-to-1
77 static inline unsigned nsp_pin_to_gpio(unsigned pin)
83 * nsp_set_bit - set or clear one bit (corresponding to the GPIO pin) in a
86 * @nsp_gpio: nsp GPIO device
87 * @base_type: reg base to modify
88 * @reg: register offset
92 static inline void nsp_set_bit(struct nsp_gpio *chip, enum base_type address,
93 unsigned int reg, unsigned gpio, bool set)
96 void __iomem *base_address;
98 if (address == IO_CTRL)
99 base_address = chip->io_ctrl;
101 base_address = chip->base;
103 val = readl(base_address + reg);
109 writel(val, base_address + reg);
113 * nsp_get_bit - get one bit (corresponding to the GPIO pin) in a
116 static inline bool nsp_get_bit(struct nsp_gpio *chip, enum base_type address,
117 unsigned int reg, unsigned gpio)
119 if (address == IO_CTRL)
120 return !!(readl(chip->io_ctrl + reg) & BIT(gpio));
122 return !!(readl(chip->base + reg) & BIT(gpio));
125 static irqreturn_t nsp_gpio_irq_handler(int irq, void *data)
127 struct gpio_chip *gc = (struct gpio_chip *)data;
128 struct nsp_gpio *chip = gpiochip_get_data(gc);
130 unsigned long int_bits = 0;
133 /* go through the entire GPIOs and handle all interrupts */
134 int_status = readl(chip->base + NSP_CHIP_A_INT_STATUS);
135 if (int_status & NSP_CHIP_A_GPIO_INT_BIT) {
136 unsigned int event, level;
138 /* Get level and edge interrupts */
139 event = readl(chip->base + NSP_GPIO_EVENT_INT_MASK) &
140 readl(chip->base + NSP_GPIO_EVENT);
141 level = readl(chip->base + NSP_GPIO_DATA_IN) ^
142 readl(chip->base + NSP_GPIO_INT_POLARITY);
143 level &= readl(chip->base + NSP_GPIO_INT_MASK);
144 int_bits = level | event;
146 for_each_set_bit(bit, &int_bits, gc->ngpio)
147 generic_handle_domain_irq(gc->irq.domain, bit);
150 return int_bits ? IRQ_HANDLED : IRQ_NONE;
153 static void nsp_gpio_irq_ack(struct irq_data *d)
155 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
156 struct nsp_gpio *chip = gpiochip_get_data(gc);
157 unsigned gpio = d->hwirq;
161 trigger_type = irq_get_trigger_type(d->irq);
162 if (trigger_type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
163 writel(val, chip->base + NSP_GPIO_EVENT);
167 * nsp_gpio_irq_set_mask - mask/unmask a GPIO interrupt
170 * @unmask: mask/unmask GPIO interrupt
172 static void nsp_gpio_irq_set_mask(struct irq_data *d, bool unmask)
174 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
175 struct nsp_gpio *chip = gpiochip_get_data(gc);
176 unsigned gpio = d->hwirq;
179 trigger_type = irq_get_trigger_type(d->irq);
180 if (trigger_type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
181 nsp_set_bit(chip, REG, NSP_GPIO_EVENT_INT_MASK, gpio, unmask);
183 nsp_set_bit(chip, REG, NSP_GPIO_INT_MASK, gpio, unmask);
186 static void nsp_gpio_irq_mask(struct irq_data *d)
188 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
189 struct nsp_gpio *chip = gpiochip_get_data(gc);
192 raw_spin_lock_irqsave(&chip->lock, flags);
193 nsp_gpio_irq_set_mask(d, false);
194 raw_spin_unlock_irqrestore(&chip->lock, flags);
195 gpiochip_disable_irq(gc, irqd_to_hwirq(d));
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 gpiochip_enable_irq(gc, irqd_to_hwirq(d));
205 raw_spin_lock_irqsave(&chip->lock, flags);
206 nsp_gpio_irq_set_mask(d, true);
207 raw_spin_unlock_irqrestore(&chip->lock, flags);
210 static int nsp_gpio_irq_set_type(struct irq_data *d, unsigned int type)
212 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
213 struct nsp_gpio *chip = gpiochip_get_data(gc);
214 unsigned gpio = d->hwirq;
219 raw_spin_lock_irqsave(&chip->lock, flags);
220 falling = nsp_get_bit(chip, REG, NSP_GPIO_EVENT_INT_POLARITY, gpio);
221 level_low = nsp_get_bit(chip, REG, NSP_GPIO_INT_POLARITY, gpio);
223 switch (type & IRQ_TYPE_SENSE_MASK) {
224 case IRQ_TYPE_EDGE_RISING:
228 case IRQ_TYPE_EDGE_FALLING:
232 case IRQ_TYPE_LEVEL_HIGH:
236 case IRQ_TYPE_LEVEL_LOW:
241 dev_err(chip->dev, "invalid GPIO IRQ type 0x%x\n",
243 raw_spin_unlock_irqrestore(&chip->lock, flags);
247 nsp_set_bit(chip, REG, NSP_GPIO_EVENT_INT_POLARITY, gpio, falling);
248 nsp_set_bit(chip, REG, NSP_GPIO_INT_POLARITY, gpio, level_low);
250 if (type & IRQ_TYPE_EDGE_BOTH)
251 irq_set_handler_locked(d, handle_edge_irq);
253 irq_set_handler_locked(d, handle_level_irq);
255 raw_spin_unlock_irqrestore(&chip->lock, flags);
257 dev_dbg(chip->dev, "gpio:%u level_low:%s falling:%s\n", gpio,
258 level_low ? "true" : "false", falling ? "true" : "false");
262 static const struct irq_chip nsp_gpio_irq_chip = {
264 .irq_ack = nsp_gpio_irq_ack,
265 .irq_mask = nsp_gpio_irq_mask,
266 .irq_unmask = nsp_gpio_irq_unmask,
267 .irq_set_type = nsp_gpio_irq_set_type,
268 .flags = IRQCHIP_IMMUTABLE,
269 GPIOCHIP_IRQ_RESOURCE_HELPERS,
272 static int nsp_gpio_direction_input(struct gpio_chip *gc, unsigned gpio)
274 struct nsp_gpio *chip = gpiochip_get_data(gc);
277 raw_spin_lock_irqsave(&chip->lock, flags);
278 nsp_set_bit(chip, REG, NSP_GPIO_OUT_EN, gpio, false);
279 raw_spin_unlock_irqrestore(&chip->lock, flags);
281 dev_dbg(chip->dev, "gpio:%u set input\n", gpio);
285 static int nsp_gpio_direction_output(struct gpio_chip *gc, unsigned gpio,
288 struct nsp_gpio *chip = gpiochip_get_data(gc);
291 raw_spin_lock_irqsave(&chip->lock, flags);
292 nsp_set_bit(chip, REG, NSP_GPIO_OUT_EN, gpio, true);
293 nsp_set_bit(chip, REG, NSP_GPIO_DATA_OUT, gpio, !!(val));
294 raw_spin_unlock_irqrestore(&chip->lock, flags);
296 dev_dbg(chip->dev, "gpio:%u set output, value:%d\n", gpio, val);
300 static int nsp_gpio_get_direction(struct gpio_chip *gc, unsigned gpio)
302 struct nsp_gpio *chip = gpiochip_get_data(gc);
306 raw_spin_lock_irqsave(&chip->lock, flags);
307 val = nsp_get_bit(chip, REG, NSP_GPIO_OUT_EN, gpio);
308 raw_spin_unlock_irqrestore(&chip->lock, flags);
313 static void nsp_gpio_set(struct gpio_chip *gc, unsigned gpio, int val)
315 struct nsp_gpio *chip = gpiochip_get_data(gc);
318 raw_spin_lock_irqsave(&chip->lock, flags);
319 nsp_set_bit(chip, REG, NSP_GPIO_DATA_OUT, gpio, !!(val));
320 raw_spin_unlock_irqrestore(&chip->lock, flags);
322 dev_dbg(chip->dev, "gpio:%u set, value:%d\n", gpio, val);
325 static int nsp_gpio_get(struct gpio_chip *gc, unsigned gpio)
327 struct nsp_gpio *chip = gpiochip_get_data(gc);
329 return !!(readl(chip->base + NSP_GPIO_DATA_IN) & BIT(gpio));
332 static int nsp_get_groups_count(struct pinctrl_dev *pctldev)
338 * Only one group: "gpio_grp", since this local pinctrl device only performs
339 * GPIO specific PINCONF configurations
341 static const char *nsp_get_group_name(struct pinctrl_dev *pctldev,
347 static const struct pinctrl_ops nsp_pctrl_ops = {
348 .get_groups_count = nsp_get_groups_count,
349 .get_group_name = nsp_get_group_name,
350 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
351 .dt_free_map = pinctrl_utils_free_map,
354 static int nsp_gpio_set_slew(struct nsp_gpio *chip, unsigned gpio, u32 slew)
357 nsp_set_bit(chip, IO_CTRL, NSP_GPIO_SLEW_RATE_EN, gpio, true);
359 nsp_set_bit(chip, IO_CTRL, NSP_GPIO_SLEW_RATE_EN, gpio, false);
364 static int nsp_gpio_set_pull(struct nsp_gpio *chip, unsigned gpio,
365 bool pull_up, bool pull_down)
369 raw_spin_lock_irqsave(&chip->lock, flags);
370 nsp_set_bit(chip, IO_CTRL, NSP_PULL_DOWN_EN, gpio, pull_down);
371 nsp_set_bit(chip, IO_CTRL, NSP_PULL_UP_EN, gpio, pull_up);
372 raw_spin_unlock_irqrestore(&chip->lock, flags);
374 dev_dbg(chip->dev, "gpio:%u set pullup:%d pulldown: %d\n",
375 gpio, pull_up, pull_down);
379 static void nsp_gpio_get_pull(struct nsp_gpio *chip, unsigned gpio,
380 bool *pull_up, bool *pull_down)
384 raw_spin_lock_irqsave(&chip->lock, flags);
385 *pull_up = nsp_get_bit(chip, IO_CTRL, NSP_PULL_UP_EN, gpio);
386 *pull_down = nsp_get_bit(chip, IO_CTRL, NSP_PULL_DOWN_EN, gpio);
387 raw_spin_unlock_irqrestore(&chip->lock, flags);
390 static int nsp_gpio_set_strength(struct nsp_gpio *chip, unsigned gpio,
393 u32 offset, shift, i;
397 /* make sure drive strength is supported */
398 if (strength < 2 || strength > 16 || (strength % 2))
402 offset = NSP_GPIO_DRV_CTRL;
403 dev_dbg(chip->dev, "gpio:%u set drive strength:%d mA\n", gpio,
405 raw_spin_lock_irqsave(&chip->lock, flags);
406 strength = (strength / 2) - 1;
407 for (i = GPIO_DRV_STRENGTH_BITS; i > 0; i--) {
408 val = readl(chip->io_ctrl + offset);
410 val |= ((strength >> (i-1)) & 0x1) << shift;
411 writel(val, chip->io_ctrl + offset);
414 raw_spin_unlock_irqrestore(&chip->lock, flags);
419 static int nsp_gpio_get_strength(struct nsp_gpio *chip, unsigned gpio,
422 unsigned int offset, shift;
427 offset = NSP_GPIO_DRV_CTRL;
430 raw_spin_lock_irqsave(&chip->lock, flags);
432 for (i = (GPIO_DRV_STRENGTH_BITS - 1); i >= 0; i--) {
433 val = readl(chip->io_ctrl + offset) & BIT(shift);
435 *strength += (val << i);
440 *strength = (*strength + 1) * 2;
441 raw_spin_unlock_irqrestore(&chip->lock, flags);
446 static int nsp_pin_config_group_get(struct pinctrl_dev *pctldev,
448 unsigned long *config)
453 static int nsp_pin_config_group_set(struct pinctrl_dev *pctldev,
455 unsigned long *configs, unsigned num_configs)
460 static int nsp_pin_config_get(struct pinctrl_dev *pctldev, unsigned pin,
461 unsigned long *config)
463 struct nsp_gpio *chip = pinctrl_dev_get_drvdata(pctldev);
464 enum pin_config_param param = pinconf_to_config_param(*config);
467 bool pull_up, pull_down;
470 gpio = nsp_pin_to_gpio(pin);
472 case PIN_CONFIG_BIAS_DISABLE:
473 nsp_gpio_get_pull(chip, gpio, &pull_up, &pull_down);
474 if ((pull_up == false) && (pull_down == false))
479 case PIN_CONFIG_BIAS_PULL_UP:
480 nsp_gpio_get_pull(chip, gpio, &pull_up, &pull_down);
486 case PIN_CONFIG_BIAS_PULL_DOWN:
487 nsp_gpio_get_pull(chip, gpio, &pull_up, &pull_down);
493 case PIN_CONFIG_DRIVE_STRENGTH:
494 ret = nsp_gpio_get_strength(chip, gpio, &arg);
497 *config = pinconf_to_config_packed(param, arg);
505 static int nsp_pin_config_set(struct pinctrl_dev *pctldev, unsigned pin,
506 unsigned long *configs, unsigned num_configs)
508 struct nsp_gpio *chip = pinctrl_dev_get_drvdata(pctldev);
509 enum pin_config_param param;
511 unsigned int i, gpio;
514 gpio = nsp_pin_to_gpio(pin);
515 for (i = 0; i < num_configs; i++) {
516 param = pinconf_to_config_param(configs[i]);
517 arg = pinconf_to_config_argument(configs[i]);
520 case PIN_CONFIG_BIAS_DISABLE:
521 ret = nsp_gpio_set_pull(chip, gpio, false, false);
526 case PIN_CONFIG_BIAS_PULL_UP:
527 ret = nsp_gpio_set_pull(chip, gpio, true, false);
532 case PIN_CONFIG_BIAS_PULL_DOWN:
533 ret = nsp_gpio_set_pull(chip, gpio, false, true);
538 case PIN_CONFIG_DRIVE_STRENGTH:
539 ret = nsp_gpio_set_strength(chip, gpio, arg);
544 case PIN_CONFIG_SLEW_RATE:
545 ret = nsp_gpio_set_slew(chip, gpio, arg);
551 dev_err(chip->dev, "invalid configuration\n");
560 static const struct pinconf_ops nsp_pconf_ops = {
562 .pin_config_get = nsp_pin_config_get,
563 .pin_config_set = nsp_pin_config_set,
564 .pin_config_group_get = nsp_pin_config_group_get,
565 .pin_config_group_set = nsp_pin_config_group_set,
569 * NSP GPIO controller supports some PINCONF related configurations such as
570 * pull up, pull down, slew and drive strength, when the pin is configured
573 * Here a local pinctrl device is created with simple 1-to-1 pin mapping to the
576 static int nsp_gpio_register_pinconf(struct nsp_gpio *chip)
578 struct pinctrl_desc *pctldesc = &chip->pctldesc;
579 struct pinctrl_pin_desc *pins;
580 struct gpio_chip *gc = &chip->gc;
583 pins = devm_kcalloc(chip->dev, gc->ngpio, sizeof(*pins), GFP_KERNEL);
586 for (i = 0; i < gc->ngpio; i++) {
588 pins[i].name = devm_kasprintf(chip->dev, GFP_KERNEL,
593 pctldesc->name = dev_name(chip->dev);
594 pctldesc->pctlops = &nsp_pctrl_ops;
595 pctldesc->pins = pins;
596 pctldesc->npins = gc->ngpio;
597 pctldesc->confops = &nsp_pconf_ops;
599 chip->pctl = devm_pinctrl_register(chip->dev, pctldesc, chip);
600 if (IS_ERR(chip->pctl)) {
601 dev_err(chip->dev, "unable to register pinctrl device\n");
602 return PTR_ERR(chip->pctl);
608 static const struct of_device_id nsp_gpio_of_match[] = {
609 {.compatible = "brcm,nsp-gpio-a",},
613 static int nsp_gpio_probe(struct platform_device *pdev)
615 struct device *dev = &pdev->dev;
616 struct nsp_gpio *chip;
617 struct gpio_chip *gc;
621 if (of_property_read_u32(pdev->dev.of_node, "ngpios", &val)) {
622 dev_err(&pdev->dev, "Missing ngpios OF property\n");
626 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
631 platform_set_drvdata(pdev, chip);
633 chip->base = devm_platform_ioremap_resource(pdev, 0);
634 if (IS_ERR(chip->base)) {
635 dev_err(dev, "unable to map I/O memory\n");
636 return PTR_ERR(chip->base);
639 chip->io_ctrl = devm_platform_ioremap_resource(pdev, 1);
640 if (IS_ERR(chip->io_ctrl)) {
641 dev_err(dev, "unable to map I/O memory\n");
642 return PTR_ERR(chip->io_ctrl);
645 raw_spin_lock_init(&chip->lock);
648 gc->can_sleep = false;
650 gc->label = dev_name(dev);
652 gc->request = gpiochip_generic_request;
653 gc->free = gpiochip_generic_free;
654 gc->direction_input = nsp_gpio_direction_input;
655 gc->direction_output = nsp_gpio_direction_output;
656 gc->get_direction = nsp_gpio_get_direction;
657 gc->set = nsp_gpio_set;
658 gc->get = nsp_gpio_get;
660 /* optional GPIO interrupt support */
661 irq = platform_get_irq(pdev, 0);
663 struct gpio_irq_chip *girq;
665 val = readl(chip->base + NSP_CHIP_A_INT_MASK);
666 val = val | NSP_CHIP_A_GPIO_INT_BIT;
667 writel(val, (chip->base + NSP_CHIP_A_INT_MASK));
669 /* Install ISR for this GPIO controller. */
670 ret = devm_request_irq(dev, irq, nsp_gpio_irq_handler,
671 IRQF_SHARED, "gpio-a", &chip->gc);
673 dev_err(&pdev->dev, "Unable to request IRQ%d: %d\n",
678 girq = &chip->gc.irq;
679 gpio_irq_chip_set_chip(girq, &nsp_gpio_irq_chip);
680 /* This will let us handle the parent IRQ in the driver */
681 girq->parent_handler = NULL;
682 girq->num_parents = 0;
683 girq->parents = NULL;
684 girq->default_type = IRQ_TYPE_NONE;
685 girq->handler = handle_bad_irq;
688 ret = devm_gpiochip_add_data(dev, gc, chip);
690 dev_err(dev, "unable to add GPIO chip\n");
694 ret = nsp_gpio_register_pinconf(chip);
696 dev_err(dev, "unable to register pinconf\n");
703 static struct platform_driver nsp_gpio_driver = {
705 .name = "nsp-gpio-a",
706 .of_match_table = nsp_gpio_of_match,
708 .probe = nsp_gpio_probe,
711 static int __init nsp_gpio_init(void)
713 return platform_driver_register(&nsp_gpio_driver);
715 arch_initcall_sync(nsp_gpio_init);