2 * Broadcom specific AMBA
5 * Copyright 2011, Broadcom Corporation
6 * Copyright 2012, Hauke Mehrtens <hauke@hauke-m.de>
8 * Licensed under the GNU/GPL. See COPYING for details.
11 #include <linux/gpio/driver.h>
12 #include <linux/interrupt.h>
13 #include <linux/export.h>
14 #include <linux/bcma/bcma.h>
16 #include "bcma_private.h"
18 #define BCMA_GPIO_MAX_PINS 32
20 static inline struct bcma_drv_cc
*bcma_gpio_get_cc(struct gpio_chip
*chip
)
22 return container_of(chip
, struct bcma_drv_cc
, gpio
);
25 static int bcma_gpio_get_value(struct gpio_chip
*chip
, unsigned gpio
)
27 struct bcma_drv_cc
*cc
= bcma_gpio_get_cc(chip
);
29 return !!bcma_chipco_gpio_in(cc
, 1 << gpio
);
32 static void bcma_gpio_set_value(struct gpio_chip
*chip
, unsigned gpio
,
35 struct bcma_drv_cc
*cc
= bcma_gpio_get_cc(chip
);
37 bcma_chipco_gpio_out(cc
, 1 << gpio
, value
? 1 << gpio
: 0);
40 static int bcma_gpio_direction_input(struct gpio_chip
*chip
, unsigned gpio
)
42 struct bcma_drv_cc
*cc
= bcma_gpio_get_cc(chip
);
44 bcma_chipco_gpio_outen(cc
, 1 << gpio
, 0);
48 static int bcma_gpio_direction_output(struct gpio_chip
*chip
, unsigned gpio
,
51 struct bcma_drv_cc
*cc
= bcma_gpio_get_cc(chip
);
53 bcma_chipco_gpio_outen(cc
, 1 << gpio
, 1 << gpio
);
54 bcma_chipco_gpio_out(cc
, 1 << gpio
, value
? 1 << gpio
: 0);
58 static int bcma_gpio_request(struct gpio_chip
*chip
, unsigned gpio
)
60 struct bcma_drv_cc
*cc
= bcma_gpio_get_cc(chip
);
62 bcma_chipco_gpio_control(cc
, 1 << gpio
, 0);
64 bcma_chipco_gpio_pulldown(cc
, 1 << gpio
, 0);
66 bcma_chipco_gpio_pullup(cc
, 1 << gpio
, 1 << gpio
);
71 static void bcma_gpio_free(struct gpio_chip
*chip
, unsigned gpio
)
73 struct bcma_drv_cc
*cc
= bcma_gpio_get_cc(chip
);
76 bcma_chipco_gpio_pullup(cc
, 1 << gpio
, 0);
79 #if IS_BUILTIN(CONFIG_BCM47XX) || IS_BUILTIN(CONFIG_ARCH_BCM_5301X)
81 static void bcma_gpio_irq_unmask(struct irq_data
*d
)
83 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
84 struct bcma_drv_cc
*cc
= bcma_gpio_get_cc(gc
);
85 int gpio
= irqd_to_hwirq(d
);
86 u32 val
= bcma_chipco_gpio_in(cc
, BIT(gpio
));
88 bcma_chipco_gpio_polarity(cc
, BIT(gpio
), val
);
89 bcma_chipco_gpio_intmask(cc
, BIT(gpio
), BIT(gpio
));
92 static void bcma_gpio_irq_mask(struct irq_data
*d
)
94 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
95 struct bcma_drv_cc
*cc
= bcma_gpio_get_cc(gc
);
96 int gpio
= irqd_to_hwirq(d
);
98 bcma_chipco_gpio_intmask(cc
, BIT(gpio
), 0);
101 static struct irq_chip bcma_gpio_irq_chip
= {
103 .irq_mask
= bcma_gpio_irq_mask
,
104 .irq_unmask
= bcma_gpio_irq_unmask
,
107 static irqreturn_t
bcma_gpio_irq_handler(int irq
, void *dev_id
)
109 struct bcma_drv_cc
*cc
= dev_id
;
110 struct gpio_chip
*gc
= &cc
->gpio
;
111 u32 val
= bcma_cc_read32(cc
, BCMA_CC_GPIOIN
);
112 u32 mask
= bcma_cc_read32(cc
, BCMA_CC_GPIOIRQ
);
113 u32 pol
= bcma_cc_read32(cc
, BCMA_CC_GPIOPOL
);
114 unsigned long irqs
= (val
^ pol
) & mask
;
120 for_each_set_bit(gpio
, &irqs
, gc
->ngpio
)
121 generic_handle_irq(irq_find_mapping(gc
->irqdomain
, gpio
));
122 bcma_chipco_gpio_polarity(cc
, irqs
, val
& irqs
);
127 static int bcma_gpio_irq_init(struct bcma_drv_cc
*cc
)
129 struct gpio_chip
*chip
= &cc
->gpio
;
132 if (cc
->core
->bus
->hosttype
!= BCMA_HOSTTYPE_SOC
)
135 hwirq
= bcma_core_irq(cc
->core
, 0);
136 err
= request_irq(hwirq
, bcma_gpio_irq_handler
, IRQF_SHARED
, "gpio",
141 bcma_chipco_gpio_intmask(cc
, ~0, 0);
142 bcma_cc_set32(cc
, BCMA_CC_IRQMASK
, BCMA_CC_IRQ_GPIO
);
144 err
= gpiochip_irqchip_add(chip
,
157 static void bcma_gpio_irq_exit(struct bcma_drv_cc
*cc
)
159 if (cc
->core
->bus
->hosttype
!= BCMA_HOSTTYPE_SOC
)
162 bcma_cc_mask32(cc
, BCMA_CC_IRQMASK
, ~BCMA_CC_IRQ_GPIO
);
163 free_irq(bcma_core_irq(cc
->core
, 0), cc
);
166 static int bcma_gpio_irq_init(struct bcma_drv_cc
*cc
)
171 static void bcma_gpio_irq_exit(struct bcma_drv_cc
*cc
)
176 int bcma_gpio_init(struct bcma_drv_cc
*cc
)
178 struct bcma_bus
*bus
= cc
->core
->bus
;
179 struct gpio_chip
*chip
= &cc
->gpio
;
182 chip
->label
= "bcma_gpio";
183 chip
->owner
= THIS_MODULE
;
184 chip
->request
= bcma_gpio_request
;
185 chip
->free
= bcma_gpio_free
;
186 chip
->get
= bcma_gpio_get_value
;
187 chip
->set
= bcma_gpio_set_value
;
188 chip
->direction_input
= bcma_gpio_direction_input
;
189 chip
->direction_output
= bcma_gpio_direction_output
;
190 chip
->owner
= THIS_MODULE
;
191 chip
->dev
= bcma_bus_get_host_dev(bus
);
192 #if IS_BUILTIN(CONFIG_OF)
193 if (cc
->core
->bus
->hosttype
== BCMA_HOSTTYPE_SOC
)
194 chip
->of_node
= cc
->core
->dev
.of_node
;
196 switch (bus
->chipinfo
.id
) {
197 case BCMA_CHIP_ID_BCM4707
:
198 case BCMA_CHIP_ID_BCM5357
:
199 case BCMA_CHIP_ID_BCM53572
:
207 * Register SoC GPIO devices with absolute GPIO pin base.
208 * On MIPS, we don't have Device Tree and we can't use relative (per chip)
210 * On some ARM devices, user space may want to access some system GPIO
211 * pins directly, which is easier to do with a predictable GPIO base.
213 if (IS_BUILTIN(CONFIG_BCM47XX
) ||
214 cc
->core
->bus
->hosttype
== BCMA_HOSTTYPE_SOC
)
215 chip
->base
= bus
->num
* BCMA_GPIO_MAX_PINS
;
219 err
= gpiochip_add(chip
);
223 err
= bcma_gpio_irq_init(cc
);
225 gpiochip_remove(chip
);
232 int bcma_gpio_unregister(struct bcma_drv_cc
*cc
)
234 bcma_gpio_irq_exit(cc
);
235 gpiochip_remove(&cc
->gpio
);