2 * Copyright (C) 2015 Broadcom Corporation
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation version 2.
8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9 * kind, whether express or implied; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * This file contains the Broadcom Northstar Plus (NSP) GPIO driver that
14 * supports the chipCommonA GPIO controller. Basic PINCONF such as bias,
15 * pull up/down, slew and drive strength are also supported in this driver.
17 * Pins from the chipCommonA GPIO can be individually muxed to GPIO function,
18 * through the interaction with the NSP IOMUX controller.
21 #include <linux/gpio/driver.h>
22 #include <linux/interrupt.h>
24 #include <linux/ioport.h>
25 #include <linux/kernel.h>
26 #include <linux/of_address.h>
27 #include <linux/of_device.h>
28 #include <linux/of_irq.h>
29 #include <linux/pinctrl/pinconf.h>
30 #include <linux/pinctrl/pinconf-generic.h>
31 #include <linux/pinctrl/pinctrl.h>
32 #include <linux/slab.h>
34 #include "../pinctrl-utils.h"
36 #define NSP_CHIP_A_INT_STATUS 0x00
37 #define NSP_CHIP_A_INT_MASK 0x04
38 #define NSP_GPIO_DATA_IN 0x40
39 #define NSP_GPIO_DATA_OUT 0x44
40 #define NSP_GPIO_OUT_EN 0x48
41 #define NSP_GPIO_INT_POLARITY 0x50
42 #define NSP_GPIO_INT_MASK 0x54
43 #define NSP_GPIO_EVENT 0x58
44 #define NSP_GPIO_EVENT_INT_MASK 0x5c
45 #define NSP_GPIO_EVENT_INT_POLARITY 0x64
46 #define NSP_CHIP_A_GPIO_INT_BIT 0x01
48 /* I/O parameters offset for chipcommon A GPIO */
49 #define NSP_GPIO_DRV_CTRL 0x00
50 #define NSP_GPIO_HYSTERESIS_EN 0x10
51 #define NSP_GPIO_SLEW_RATE_EN 0x14
52 #define NSP_PULL_UP_EN 0x18
53 #define NSP_PULL_DOWN_EN 0x1c
54 #define GPIO_DRV_STRENGTH_BITS 0x03
59 * @dev: pointer to device
60 * @base: I/O register base for nsp GPIO controller
61 * @io_ctrl: I/O register base for PINCONF support outside the GPIO block
63 * @pctl: pointer to pinctrl_dev
64 * @pctldesc: pinctrl descriptor
65 * @irq_domain: pointer to irq domain
66 * @lock: lock to protect access to I/O registers
71 void __iomem
*io_ctrl
;
73 struct pinctrl_dev
*pctl
;
74 struct pinctrl_desc pctldesc
;
75 struct irq_domain
*irq_domain
;
85 * Mapping from PINCONF pins to GPIO pins is 1-to-1
87 static inline unsigned nsp_pin_to_gpio(unsigned pin
)
93 * nsp_set_bit - set or clear one bit (corresponding to the GPIO pin) in a
96 * @nsp_gpio: nsp GPIO device
97 * @base_type: reg base to modify
98 * @reg: register offset
102 static inline void nsp_set_bit(struct nsp_gpio
*chip
, enum base_type address
,
103 unsigned int reg
, unsigned gpio
, bool set
)
106 void __iomem
*base_address
;
108 if (address
== IO_CTRL
)
109 base_address
= chip
->io_ctrl
;
111 base_address
= chip
->base
;
113 val
= readl(base_address
+ reg
);
119 writel(val
, base_address
+ reg
);
123 * nsp_get_bit - get one bit (corresponding to the GPIO pin) in a
126 static inline bool nsp_get_bit(struct nsp_gpio
*chip
, enum base_type address
,
127 unsigned int reg
, unsigned gpio
)
129 if (address
== IO_CTRL
)
130 return !!(readl(chip
->io_ctrl
+ reg
) & BIT(gpio
));
132 return !!(readl(chip
->base
+ reg
) & BIT(gpio
));
135 static irqreturn_t
nsp_gpio_irq_handler(int irq
, void *data
)
137 struct nsp_gpio
*chip
= (struct nsp_gpio
*)data
;
138 struct gpio_chip gc
= chip
->gc
;
140 unsigned long int_bits
= 0;
143 /* go through the entire GPIOs and handle all interrupts */
144 int_status
= readl(chip
->base
+ NSP_CHIP_A_INT_STATUS
);
145 if (int_status
& NSP_CHIP_A_GPIO_INT_BIT
) {
146 unsigned int event
, level
;
148 /* Get level and edge interrupts */
149 event
= readl(chip
->base
+ NSP_GPIO_EVENT_INT_MASK
) &
150 readl(chip
->base
+ NSP_GPIO_EVENT
);
151 level
= readl(chip
->base
+ NSP_GPIO_DATA_IN
) ^
152 readl(chip
->base
+ NSP_GPIO_INT_POLARITY
);
153 level
&= readl(chip
->base
+ NSP_GPIO_INT_MASK
);
154 int_bits
= level
| event
;
156 for_each_set_bit(bit
, &int_bits
, gc
.ngpio
) {
158 * Clear the interrupt before invoking the
159 * handler, so we do not leave any window
161 writel(BIT(bit
), chip
->base
+ NSP_GPIO_EVENT
);
163 irq_linear_revmap(chip
->irq_domain
, bit
));
167 return int_bits
? IRQ_HANDLED
: IRQ_NONE
;
170 static void nsp_gpio_irq_ack(struct irq_data
*d
)
172 struct nsp_gpio
*chip
= irq_data_get_irq_chip_data(d
);
173 unsigned gpio
= d
->hwirq
;
177 trigger_type
= irq_get_trigger_type(d
->irq
);
178 if (trigger_type
& (IRQ_TYPE_EDGE_FALLING
| IRQ_TYPE_EDGE_RISING
))
179 nsp_set_bit(chip
, REG
, NSP_GPIO_EVENT
, gpio
, val
);
183 * nsp_gpio_irq_set_mask - mask/unmask a GPIO interrupt
186 * @unmask: mask/unmask GPIO interrupt
188 static void nsp_gpio_irq_set_mask(struct irq_data
*d
, bool unmask
)
190 struct nsp_gpio
*chip
= irq_data_get_irq_chip_data(d
);
191 unsigned gpio
= d
->hwirq
;
194 trigger_type
= irq_get_trigger_type(d
->irq
);
195 if (trigger_type
& (IRQ_TYPE_EDGE_FALLING
| IRQ_TYPE_EDGE_RISING
))
196 nsp_set_bit(chip
, REG
, NSP_GPIO_EVENT_INT_MASK
, gpio
, unmask
);
198 nsp_set_bit(chip
, REG
, NSP_GPIO_INT_MASK
, gpio
, unmask
);
201 static void nsp_gpio_irq_mask(struct irq_data
*d
)
203 struct nsp_gpio
*chip
= irq_data_get_irq_chip_data(d
);
206 spin_lock_irqsave(&chip
->lock
, flags
);
207 nsp_gpio_irq_set_mask(d
, false);
208 spin_unlock_irqrestore(&chip
->lock
, flags
);
211 static void nsp_gpio_irq_unmask(struct irq_data
*d
)
213 struct nsp_gpio
*chip
= irq_data_get_irq_chip_data(d
);
216 spin_lock_irqsave(&chip
->lock
, flags
);
217 nsp_gpio_irq_set_mask(d
, true);
218 spin_unlock_irqrestore(&chip
->lock
, flags
);
221 static int nsp_gpio_irq_set_type(struct irq_data
*d
, unsigned int type
)
223 struct nsp_gpio
*chip
= irq_data_get_irq_chip_data(d
);
224 unsigned gpio
= d
->hwirq
;
229 spin_lock_irqsave(&chip
->lock
, flags
);
230 falling
= nsp_get_bit(chip
, REG
, NSP_GPIO_EVENT_INT_POLARITY
, gpio
);
231 level_low
= nsp_get_bit(chip
, REG
, NSP_GPIO_INT_POLARITY
, gpio
);
233 switch (type
& IRQ_TYPE_SENSE_MASK
) {
234 case IRQ_TYPE_EDGE_RISING
:
238 case IRQ_TYPE_EDGE_FALLING
:
242 case IRQ_TYPE_LEVEL_HIGH
:
246 case IRQ_TYPE_LEVEL_LOW
:
251 dev_err(chip
->dev
, "invalid GPIO IRQ type 0x%x\n",
253 spin_unlock_irqrestore(&chip
->lock
, flags
);
257 nsp_set_bit(chip
, REG
, NSP_GPIO_EVENT_INT_POLARITY
, gpio
, falling
);
258 nsp_set_bit(chip
, REG
, NSP_GPIO_INT_POLARITY
, gpio
, level_low
);
259 spin_unlock_irqrestore(&chip
->lock
, flags
);
261 dev_dbg(chip
->dev
, "gpio:%u level_low:%s falling:%s\n", gpio
,
262 level_low
? "true" : "false", falling
? "true" : "false");
266 static struct irq_chip nsp_gpio_irq_chip
= {
268 .irq_enable
= nsp_gpio_irq_unmask
,
269 .irq_disable
= nsp_gpio_irq_mask
,
270 .irq_ack
= nsp_gpio_irq_ack
,
271 .irq_mask
= nsp_gpio_irq_mask
,
272 .irq_unmask
= nsp_gpio_irq_unmask
,
273 .irq_set_type
= nsp_gpio_irq_set_type
,
277 * Request the nsp IOMUX pinmux controller to mux individual pins to GPIO
279 static int nsp_gpio_request(struct gpio_chip
*gc
, unsigned offset
)
281 unsigned gpio
= gc
->base
+ offset
;
283 return pinctrl_request_gpio(gpio
);
286 static void nsp_gpio_free(struct gpio_chip
*gc
, unsigned offset
)
288 unsigned gpio
= gc
->base
+ offset
;
290 pinctrl_free_gpio(gpio
);
293 static int nsp_gpio_direction_input(struct gpio_chip
*gc
, unsigned gpio
)
295 struct nsp_gpio
*chip
= gpiochip_get_data(gc
);
298 spin_lock_irqsave(&chip
->lock
, flags
);
299 nsp_set_bit(chip
, REG
, NSP_GPIO_OUT_EN
, gpio
, false);
300 spin_unlock_irqrestore(&chip
->lock
, flags
);
302 dev_dbg(chip
->dev
, "gpio:%u set input\n", gpio
);
306 static int nsp_gpio_direction_output(struct gpio_chip
*gc
, unsigned gpio
,
309 struct nsp_gpio
*chip
= gpiochip_get_data(gc
);
312 spin_lock_irqsave(&chip
->lock
, flags
);
313 nsp_set_bit(chip
, REG
, NSP_GPIO_OUT_EN
, gpio
, true);
314 nsp_set_bit(chip
, REG
, NSP_GPIO_DATA_OUT
, gpio
, !!(val
));
315 spin_unlock_irqrestore(&chip
->lock
, flags
);
317 dev_dbg(chip
->dev
, "gpio:%u set output, value:%d\n", gpio
, val
);
321 static void nsp_gpio_set(struct gpio_chip
*gc
, unsigned gpio
, int val
)
323 struct nsp_gpio
*chip
= gpiochip_get_data(gc
);
326 spin_lock_irqsave(&chip
->lock
, flags
);
327 nsp_set_bit(chip
, REG
, NSP_GPIO_DATA_OUT
, gpio
, !!(val
));
328 spin_unlock_irqrestore(&chip
->lock
, flags
);
330 dev_dbg(chip
->dev
, "gpio:%u set, value:%d\n", gpio
, val
);
333 static int nsp_gpio_get(struct gpio_chip
*gc
, unsigned gpio
)
335 struct nsp_gpio
*chip
= gpiochip_get_data(gc
);
337 return !!(readl(chip
->base
+ NSP_GPIO_DATA_IN
) & BIT(gpio
));
340 static int nsp_gpio_to_irq(struct gpio_chip
*gc
, unsigned offset
)
342 struct nsp_gpio
*chip
= gpiochip_get_data(gc
);
344 return irq_linear_revmap(chip
->irq_domain
, offset
);
347 static int nsp_get_groups_count(struct pinctrl_dev
*pctldev
)
353 * Only one group: "gpio_grp", since this local pinctrl device only performs
354 * GPIO specific PINCONF configurations
356 static const char *nsp_get_group_name(struct pinctrl_dev
*pctldev
,
362 static const struct pinctrl_ops nsp_pctrl_ops
= {
363 .get_groups_count
= nsp_get_groups_count
,
364 .get_group_name
= nsp_get_group_name
,
365 .dt_node_to_map
= pinconf_generic_dt_node_to_map_pin
,
366 .dt_free_map
= pinctrl_utils_free_map
,
369 static int nsp_gpio_set_slew(struct nsp_gpio
*chip
, unsigned gpio
, u16 slew
)
372 nsp_set_bit(chip
, IO_CTRL
, NSP_GPIO_SLEW_RATE_EN
, gpio
, true);
374 nsp_set_bit(chip
, IO_CTRL
, NSP_GPIO_SLEW_RATE_EN
, gpio
, false);
379 static int nsp_gpio_set_pull(struct nsp_gpio
*chip
, unsigned gpio
,
380 bool pull_up
, bool pull_down
)
384 spin_lock_irqsave(&chip
->lock
, flags
);
385 nsp_set_bit(chip
, IO_CTRL
, NSP_PULL_DOWN_EN
, gpio
, pull_down
);
386 nsp_set_bit(chip
, IO_CTRL
, NSP_PULL_UP_EN
, gpio
, pull_up
);
387 spin_unlock_irqrestore(&chip
->lock
, flags
);
389 dev_dbg(chip
->dev
, "gpio:%u set pullup:%d pulldown: %d\n",
390 gpio
, pull_up
, pull_down
);
394 static void nsp_gpio_get_pull(struct nsp_gpio
*chip
, unsigned gpio
,
395 bool *pull_up
, bool *pull_down
)
399 spin_lock_irqsave(&chip
->lock
, flags
);
400 *pull_up
= nsp_get_bit(chip
, IO_CTRL
, NSP_PULL_UP_EN
, gpio
);
401 *pull_down
= nsp_get_bit(chip
, IO_CTRL
, NSP_PULL_DOWN_EN
, gpio
);
402 spin_unlock_irqrestore(&chip
->lock
, flags
);
405 static int nsp_gpio_set_strength(struct nsp_gpio
*chip
, unsigned gpio
,
408 u32 offset
, shift
, i
;
412 /* make sure drive strength is supported */
413 if (strength
< 2 || strength
> 16 || (strength
% 2))
417 offset
= NSP_GPIO_DRV_CTRL
;
418 dev_dbg(chip
->dev
, "gpio:%u set drive strength:%d mA\n", gpio
,
420 spin_lock_irqsave(&chip
->lock
, flags
);
421 strength
= (strength
/ 2) - 1;
422 for (i
= GPIO_DRV_STRENGTH_BITS
; i
> 0; i
--) {
423 val
= readl(chip
->io_ctrl
+ offset
);
425 val
|= ((strength
>> (i
-1)) & 0x1) << shift
;
426 writel(val
, chip
->io_ctrl
+ offset
);
429 spin_unlock_irqrestore(&chip
->lock
, flags
);
434 static int nsp_gpio_get_strength(struct nsp_gpio
*chip
, unsigned gpio
,
437 unsigned int offset
, shift
;
442 offset
= NSP_GPIO_DRV_CTRL
;
445 spin_lock_irqsave(&chip
->lock
, flags
);
447 for (i
= (GPIO_DRV_STRENGTH_BITS
- 1); i
>= 0; i
--) {
448 val
= readl(chip
->io_ctrl
+ offset
) & BIT(shift
);
450 *strength
+= (val
<< i
);
455 *strength
= (*strength
+ 1) * 2;
456 spin_unlock_irqrestore(&chip
->lock
, flags
);
461 static int nsp_pin_config_group_get(struct pinctrl_dev
*pctldev
,
463 unsigned long *config
)
468 static int nsp_pin_config_group_set(struct pinctrl_dev
*pctldev
,
470 unsigned long *configs
, unsigned num_configs
)
475 static int nsp_pin_config_get(struct pinctrl_dev
*pctldev
, unsigned pin
,
476 unsigned long *config
)
478 struct nsp_gpio
*chip
= pinctrl_dev_get_drvdata(pctldev
);
479 enum pin_config_param param
= pinconf_to_config_param(*config
);
482 bool pull_up
, pull_down
;
485 gpio
= nsp_pin_to_gpio(pin
);
487 case PIN_CONFIG_BIAS_DISABLE
:
488 nsp_gpio_get_pull(chip
, gpio
, &pull_up
, &pull_down
);
489 if ((pull_up
== false) && (pull_down
== false))
494 case PIN_CONFIG_BIAS_PULL_UP
:
495 nsp_gpio_get_pull(chip
, gpio
, &pull_up
, &pull_down
);
501 case PIN_CONFIG_BIAS_PULL_DOWN
:
502 nsp_gpio_get_pull(chip
, gpio
, &pull_up
, &pull_down
);
508 case PIN_CONFIG_DRIVE_STRENGTH
:
509 ret
= nsp_gpio_get_strength(chip
, gpio
, &arg
);
512 *config
= pinconf_to_config_packed(param
, arg
);
520 static int nsp_pin_config_set(struct pinctrl_dev
*pctldev
, unsigned pin
,
521 unsigned long *configs
, unsigned num_configs
)
523 struct nsp_gpio
*chip
= pinctrl_dev_get_drvdata(pctldev
);
524 enum pin_config_param param
;
526 unsigned int i
, gpio
;
529 gpio
= nsp_pin_to_gpio(pin
);
530 for (i
= 0; i
< num_configs
; i
++) {
531 param
= pinconf_to_config_param(configs
[i
]);
532 arg
= pinconf_to_config_argument(configs
[i
]);
535 case PIN_CONFIG_BIAS_DISABLE
:
536 ret
= nsp_gpio_set_pull(chip
, gpio
, false, false);
541 case PIN_CONFIG_BIAS_PULL_UP
:
542 ret
= nsp_gpio_set_pull(chip
, gpio
, true, false);
547 case PIN_CONFIG_BIAS_PULL_DOWN
:
548 ret
= nsp_gpio_set_pull(chip
, gpio
, false, true);
553 case PIN_CONFIG_DRIVE_STRENGTH
:
554 ret
= nsp_gpio_set_strength(chip
, gpio
, arg
);
559 case PIN_CONFIG_SLEW_RATE
:
560 ret
= nsp_gpio_set_slew(chip
, gpio
, arg
);
566 dev_err(chip
->dev
, "invalid configuration\n");
575 static const struct pinconf_ops nsp_pconf_ops
= {
577 .pin_config_get
= nsp_pin_config_get
,
578 .pin_config_set
= nsp_pin_config_set
,
579 .pin_config_group_get
= nsp_pin_config_group_get
,
580 .pin_config_group_set
= nsp_pin_config_group_set
,
584 * NSP GPIO controller supports some PINCONF related configurations such as
585 * pull up, pull down, slew and drive strength, when the pin is configured
588 * Here a local pinctrl device is created with simple 1-to-1 pin mapping to the
591 static int nsp_gpio_register_pinconf(struct nsp_gpio
*chip
)
593 struct pinctrl_desc
*pctldesc
= &chip
->pctldesc
;
594 struct pinctrl_pin_desc
*pins
;
595 struct gpio_chip
*gc
= &chip
->gc
;
598 pins
= devm_kcalloc(chip
->dev
, gc
->ngpio
, sizeof(*pins
), GFP_KERNEL
);
601 for (i
= 0; i
< gc
->ngpio
; i
++) {
603 pins
[i
].name
= devm_kasprintf(chip
->dev
, GFP_KERNEL
,
608 pctldesc
->name
= dev_name(chip
->dev
);
609 pctldesc
->pctlops
= &nsp_pctrl_ops
;
610 pctldesc
->pins
= pins
;
611 pctldesc
->npins
= gc
->ngpio
;
612 pctldesc
->confops
= &nsp_pconf_ops
;
614 chip
->pctl
= devm_pinctrl_register(chip
->dev
, pctldesc
, chip
);
615 if (IS_ERR(chip
->pctl
)) {
616 dev_err(chip
->dev
, "unable to register pinctrl device\n");
617 return PTR_ERR(chip
->pctl
);
623 static const struct of_device_id nsp_gpio_of_match
[] = {
624 {.compatible
= "brcm,nsp-gpio-a",},
628 static int nsp_gpio_probe(struct platform_device
*pdev
)
630 struct device
*dev
= &pdev
->dev
;
631 struct resource
*res
;
632 struct nsp_gpio
*chip
;
633 struct gpio_chip
*gc
;
637 if (of_property_read_u32(pdev
->dev
.of_node
, "ngpios", &val
)) {
638 dev_err(&pdev
->dev
, "Missing ngpios OF property\n");
642 chip
= devm_kzalloc(dev
, sizeof(*chip
), GFP_KERNEL
);
647 platform_set_drvdata(pdev
, chip
);
649 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
650 chip
->base
= devm_ioremap_resource(dev
, res
);
651 if (IS_ERR(chip
->base
)) {
652 dev_err(dev
, "unable to map I/O memory\n");
653 return PTR_ERR(chip
->base
);
656 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
657 chip
->io_ctrl
= devm_ioremap_resource(dev
, res
);
658 if (IS_ERR(chip
->io_ctrl
)) {
659 dev_err(dev
, "unable to map I/O memory\n");
660 return PTR_ERR(chip
->io_ctrl
);
663 spin_lock_init(&chip
->lock
);
666 gc
->can_sleep
= false;
668 gc
->label
= dev_name(dev
);
670 gc
->of_node
= dev
->of_node
;
671 gc
->request
= nsp_gpio_request
;
672 gc
->free
= nsp_gpio_free
;
673 gc
->direction_input
= nsp_gpio_direction_input
;
674 gc
->direction_output
= nsp_gpio_direction_output
;
675 gc
->set
= nsp_gpio_set
;
676 gc
->get
= nsp_gpio_get
;
677 gc
->to_irq
= nsp_gpio_to_irq
;
679 /* optional GPIO interrupt support */
680 irq
= platform_get_irq(pdev
, 0);
682 /* Create irq domain so that each pin can be assigned an IRQ.*/
683 chip
->irq_domain
= irq_domain_add_linear(gc
->of_node
, gc
->ngpio
,
684 &irq_domain_simple_ops
,
686 if (!chip
->irq_domain
) {
687 dev_err(&pdev
->dev
, "Couldn't allocate IRQ domain\n");
691 /* Map each gpio to an IRQ and set the handler for gpiolib. */
692 for (count
= 0; count
< gc
->ngpio
; count
++) {
693 int irq
= irq_create_mapping(chip
->irq_domain
, count
);
695 irq_set_chip_and_handler(irq
, &nsp_gpio_irq_chip
,
697 irq_set_chip_data(irq
, chip
);
700 /* Install ISR for this GPIO controller. */
701 ret
= devm_request_irq(&pdev
->dev
, irq
, nsp_gpio_irq_handler
,
702 IRQF_SHARED
, "gpio-a", chip
);
704 dev_err(&pdev
->dev
, "Unable to request IRQ%d: %d\n",
706 goto err_rm_gpiochip
;
709 val
= readl(chip
->base
+ NSP_CHIP_A_INT_MASK
);
710 val
= val
| NSP_CHIP_A_GPIO_INT_BIT
;
711 writel(val
, (chip
->base
+ NSP_CHIP_A_INT_MASK
));
714 ret
= gpiochip_add_data(gc
, chip
);
716 dev_err(dev
, "unable to add GPIO chip\n");
720 ret
= nsp_gpio_register_pinconf(chip
);
722 dev_err(dev
, "unable to register pinconf\n");
723 goto err_rm_gpiochip
;
734 static struct platform_driver nsp_gpio_driver
= {
736 .name
= "nsp-gpio-a",
737 .of_match_table
= nsp_gpio_of_match
,
739 .probe
= nsp_gpio_probe
,
742 static int __init
nsp_gpio_init(void)
744 return platform_driver_register(&nsp_gpio_driver
);
746 arch_initcall_sync(nsp_gpio_init
);