Hauke Mehrtens | cf0936b | 2012-11-20 22:24:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Broadcom specific AMBA |
| 3 | * GPIO driver |
| 4 | * |
| 5 | * Copyright 2011, Broadcom Corporation |
| 6 | * Copyright 2012, Hauke Mehrtens <hauke@hauke-m.de> |
| 7 | * |
| 8 | * Licensed under the GNU/GPL. See COPYING for details. |
| 9 | */ |
| 10 | |
Linus Walleij | 74f4e0c | 2015-08-14 00:21:45 +0200 | [diff] [blame] | 11 | #include <linux/gpio/driver.h> |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 12 | #include <linux/interrupt.h> |
Hauke Mehrtens | cf0936b | 2012-11-20 22:24:30 +0000 | [diff] [blame] | 13 | #include <linux/export.h> |
| 14 | #include <linux/bcma/bcma.h> |
| 15 | |
| 16 | #include "bcma_private.h" |
| 17 | |
Rafał Miłecki | 057fcd4 | 2015-03-15 19:43:14 +0100 | [diff] [blame] | 18 | #define BCMA_GPIO_MAX_PINS 32 |
| 19 | |
Hauke Mehrtens | cf0936b | 2012-11-20 22:24:30 +0000 | [diff] [blame] | 20 | static int bcma_gpio_get_value(struct gpio_chip *chip, unsigned gpio) |
| 21 | { |
Linus Walleij | 78d455a | 2015-12-08 16:17:02 +0100 | [diff] [blame] | 22 | struct bcma_drv_cc *cc = gpiochip_get_data(chip); |
Hauke Mehrtens | cf0936b | 2012-11-20 22:24:30 +0000 | [diff] [blame] | 23 | |
| 24 | return !!bcma_chipco_gpio_in(cc, 1 << gpio); |
| 25 | } |
| 26 | |
| 27 | static void bcma_gpio_set_value(struct gpio_chip *chip, unsigned gpio, |
| 28 | int value) |
| 29 | { |
Linus Walleij | 78d455a | 2015-12-08 16:17:02 +0100 | [diff] [blame] | 30 | struct bcma_drv_cc *cc = gpiochip_get_data(chip); |
Hauke Mehrtens | cf0936b | 2012-11-20 22:24:30 +0000 | [diff] [blame] | 31 | |
| 32 | bcma_chipco_gpio_out(cc, 1 << gpio, value ? 1 << gpio : 0); |
| 33 | } |
| 34 | |
| 35 | static int bcma_gpio_direction_input(struct gpio_chip *chip, unsigned gpio) |
| 36 | { |
Linus Walleij | 78d455a | 2015-12-08 16:17:02 +0100 | [diff] [blame] | 37 | struct bcma_drv_cc *cc = gpiochip_get_data(chip); |
Hauke Mehrtens | cf0936b | 2012-11-20 22:24:30 +0000 | [diff] [blame] | 38 | |
| 39 | bcma_chipco_gpio_outen(cc, 1 << gpio, 0); |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static int bcma_gpio_direction_output(struct gpio_chip *chip, unsigned gpio, |
| 44 | int value) |
| 45 | { |
Linus Walleij | 78d455a | 2015-12-08 16:17:02 +0100 | [diff] [blame] | 46 | struct bcma_drv_cc *cc = gpiochip_get_data(chip); |
Hauke Mehrtens | cf0936b | 2012-11-20 22:24:30 +0000 | [diff] [blame] | 47 | |
| 48 | bcma_chipco_gpio_outen(cc, 1 << gpio, 1 << gpio); |
| 49 | bcma_chipco_gpio_out(cc, 1 << gpio, value ? 1 << gpio : 0); |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static int bcma_gpio_request(struct gpio_chip *chip, unsigned gpio) |
| 54 | { |
Linus Walleij | 78d455a | 2015-12-08 16:17:02 +0100 | [diff] [blame] | 55 | struct bcma_drv_cc *cc = gpiochip_get_data(chip); |
Hauke Mehrtens | cf0936b | 2012-11-20 22:24:30 +0000 | [diff] [blame] | 56 | |
| 57 | bcma_chipco_gpio_control(cc, 1 << gpio, 0); |
| 58 | /* clear pulldown */ |
| 59 | bcma_chipco_gpio_pulldown(cc, 1 << gpio, 0); |
| 60 | /* Set pullup */ |
| 61 | bcma_chipco_gpio_pullup(cc, 1 << gpio, 1 << gpio); |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static void bcma_gpio_free(struct gpio_chip *chip, unsigned gpio) |
| 67 | { |
Linus Walleij | 78d455a | 2015-12-08 16:17:02 +0100 | [diff] [blame] | 68 | struct bcma_drv_cc *cc = gpiochip_get_data(chip); |
Hauke Mehrtens | cf0936b | 2012-11-20 22:24:30 +0000 | [diff] [blame] | 69 | |
| 70 | /* clear pullup */ |
| 71 | bcma_chipco_gpio_pullup(cc, 1 << gpio, 0); |
| 72 | } |
| 73 | |
Rafał Miłecki | 0cbfc06 | 2015-02-20 11:49:05 +0100 | [diff] [blame] | 74 | #if IS_BUILTIN(CONFIG_BCM47XX) || IS_BUILTIN(CONFIG_ARCH_BCM_5301X) |
Hauke Mehrtens | 8f1ca26 | 2013-01-26 21:39:44 +0100 | [diff] [blame] | 75 | |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 76 | static void bcma_gpio_irq_unmask(struct irq_data *d) |
| 77 | { |
Linus Walleij | 74f4e0c | 2015-08-14 00:21:45 +0200 | [diff] [blame] | 78 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 78d455a | 2015-12-08 16:17:02 +0100 | [diff] [blame] | 79 | struct bcma_drv_cc *cc = gpiochip_get_data(gc); |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 80 | int gpio = irqd_to_hwirq(d); |
Hauke Mehrtens | 978e55d | 2014-01-02 19:01:08 +0100 | [diff] [blame] | 81 | u32 val = bcma_chipco_gpio_in(cc, BIT(gpio)); |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 82 | |
Hauke Mehrtens | 978e55d | 2014-01-02 19:01:08 +0100 | [diff] [blame] | 83 | bcma_chipco_gpio_polarity(cc, BIT(gpio), val); |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 84 | bcma_chipco_gpio_intmask(cc, BIT(gpio), BIT(gpio)); |
| 85 | } |
| 86 | |
| 87 | static void bcma_gpio_irq_mask(struct irq_data *d) |
| 88 | { |
Linus Walleij | 74f4e0c | 2015-08-14 00:21:45 +0200 | [diff] [blame] | 89 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 78d455a | 2015-12-08 16:17:02 +0100 | [diff] [blame] | 90 | struct bcma_drv_cc *cc = gpiochip_get_data(gc); |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 91 | int gpio = irqd_to_hwirq(d); |
| 92 | |
| 93 | bcma_chipco_gpio_intmask(cc, BIT(gpio), 0); |
| 94 | } |
| 95 | |
| 96 | static struct irq_chip bcma_gpio_irq_chip = { |
| 97 | .name = "BCMA-GPIO", |
| 98 | .irq_mask = bcma_gpio_irq_mask, |
| 99 | .irq_unmask = bcma_gpio_irq_unmask, |
| 100 | }; |
| 101 | |
| 102 | static irqreturn_t bcma_gpio_irq_handler(int irq, void *dev_id) |
| 103 | { |
| 104 | struct bcma_drv_cc *cc = dev_id; |
Linus Walleij | 74f4e0c | 2015-08-14 00:21:45 +0200 | [diff] [blame] | 105 | struct gpio_chip *gc = &cc->gpio; |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 106 | u32 val = bcma_cc_read32(cc, BCMA_CC_GPIOIN); |
| 107 | u32 mask = bcma_cc_read32(cc, BCMA_CC_GPIOIRQ); |
| 108 | u32 pol = bcma_cc_read32(cc, BCMA_CC_GPIOPOL); |
Rafał Miłecki | d5ab2ad | 2014-01-13 20:05:17 +0100 | [diff] [blame] | 109 | unsigned long irqs = (val ^ pol) & mask; |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 110 | int gpio; |
| 111 | |
| 112 | if (!irqs) |
| 113 | return IRQ_NONE; |
| 114 | |
Linus Walleij | 74f4e0c | 2015-08-14 00:21:45 +0200 | [diff] [blame] | 115 | for_each_set_bit(gpio, &irqs, gc->ngpio) |
| 116 | generic_handle_irq(irq_find_mapping(gc->irqdomain, gpio)); |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 117 | bcma_chipco_gpio_polarity(cc, irqs, val & irqs); |
| 118 | |
| 119 | return IRQ_HANDLED; |
| 120 | } |
| 121 | |
Linus Walleij | 74f4e0c | 2015-08-14 00:21:45 +0200 | [diff] [blame] | 122 | static int bcma_gpio_irq_init(struct bcma_drv_cc *cc) |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 123 | { |
| 124 | struct gpio_chip *chip = &cc->gpio; |
Linus Walleij | 74f4e0c | 2015-08-14 00:21:45 +0200 | [diff] [blame] | 125 | int hwirq, err; |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 126 | |
| 127 | if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC) |
| 128 | return 0; |
| 129 | |
Hauke Mehrtens | 85eb92e | 2014-11-01 16:54:55 +0100 | [diff] [blame] | 130 | hwirq = bcma_core_irq(cc->core, 0); |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 131 | err = request_irq(hwirq, bcma_gpio_irq_handler, IRQF_SHARED, "gpio", |
| 132 | cc); |
| 133 | if (err) |
Linus Walleij | 74f4e0c | 2015-08-14 00:21:45 +0200 | [diff] [blame] | 134 | return err; |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 135 | |
Hauke Mehrtens | 978e55d | 2014-01-02 19:01:08 +0100 | [diff] [blame] | 136 | bcma_chipco_gpio_intmask(cc, ~0, 0); |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 137 | bcma_cc_set32(cc, BCMA_CC_IRQMASK, BCMA_CC_IRQ_GPIO); |
| 138 | |
Linus Walleij | 74f4e0c | 2015-08-14 00:21:45 +0200 | [diff] [blame] | 139 | err = gpiochip_irqchip_add(chip, |
| 140 | &bcma_gpio_irq_chip, |
| 141 | 0, |
| 142 | handle_simple_irq, |
| 143 | IRQ_TYPE_NONE); |
| 144 | if (err) { |
| 145 | free_irq(hwirq, cc); |
| 146 | return err; |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 147 | } |
Linus Walleij | 74f4e0c | 2015-08-14 00:21:45 +0200 | [diff] [blame] | 148 | |
| 149 | return 0; |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 150 | } |
| 151 | |
Linus Walleij | 74f4e0c | 2015-08-14 00:21:45 +0200 | [diff] [blame] | 152 | static void bcma_gpio_irq_exit(struct bcma_drv_cc *cc) |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 153 | { |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 154 | if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC) |
| 155 | return; |
| 156 | |
| 157 | bcma_cc_mask32(cc, BCMA_CC_IRQMASK, ~BCMA_CC_IRQ_GPIO); |
Hauke Mehrtens | 85eb92e | 2014-11-01 16:54:55 +0100 | [diff] [blame] | 158 | free_irq(bcma_core_irq(cc->core, 0), cc); |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 159 | } |
| 160 | #else |
Linus Walleij | 74f4e0c | 2015-08-14 00:21:45 +0200 | [diff] [blame] | 161 | static int bcma_gpio_irq_init(struct bcma_drv_cc *cc) |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 162 | { |
| 163 | return 0; |
| 164 | } |
| 165 | |
Linus Walleij | 74f4e0c | 2015-08-14 00:21:45 +0200 | [diff] [blame] | 166 | static void bcma_gpio_irq_exit(struct bcma_drv_cc *cc) |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 167 | { |
| 168 | } |
| 169 | #endif |
| 170 | |
Hauke Mehrtens | cf0936b | 2012-11-20 22:24:30 +0000 | [diff] [blame] | 171 | int bcma_gpio_init(struct bcma_drv_cc *cc) |
| 172 | { |
Rafał Miłecki | 057fcd4 | 2015-03-15 19:43:14 +0100 | [diff] [blame] | 173 | struct bcma_bus *bus = cc->core->bus; |
Hauke Mehrtens | cf0936b | 2012-11-20 22:24:30 +0000 | [diff] [blame] | 174 | struct gpio_chip *chip = &cc->gpio; |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 175 | int err; |
Hauke Mehrtens | cf0936b | 2012-11-20 22:24:30 +0000 | [diff] [blame] | 176 | |
| 177 | chip->label = "bcma_gpio"; |
| 178 | chip->owner = THIS_MODULE; |
| 179 | chip->request = bcma_gpio_request; |
| 180 | chip->free = bcma_gpio_free; |
| 181 | chip->get = bcma_gpio_get_value; |
| 182 | chip->set = bcma_gpio_set_value; |
| 183 | chip->direction_input = bcma_gpio_direction_input; |
| 184 | chip->direction_output = bcma_gpio_direction_output; |
Linus Walleij | 74f4e0c | 2015-08-14 00:21:45 +0200 | [diff] [blame] | 185 | chip->owner = THIS_MODULE; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 186 | chip->parent = bcma_bus_get_host_dev(bus); |
Rafał Miłecki | a0196d1 | 2014-09-30 12:55:48 +0200 | [diff] [blame] | 187 | #if IS_BUILTIN(CONFIG_OF) |
Rafał Miłecki | a4bb5b1 | 2017-02-14 23:03:46 +0100 | [diff] [blame] | 188 | chip->of_node = cc->core->dev.of_node; |
Rafał Miłecki | a0196d1 | 2014-09-30 12:55:48 +0200 | [diff] [blame] | 189 | #endif |
Rafał Miłecki | 057fcd4 | 2015-03-15 19:43:14 +0100 | [diff] [blame] | 190 | switch (bus->chipinfo.id) { |
Felix Fietkau | f022ea5 | 2015-04-15 15:07:53 +0200 | [diff] [blame] | 191 | case BCMA_CHIP_ID_BCM4707: |
Rafał Miłecki | 0f8ca01 | 2014-03-20 21:09:07 +0100 | [diff] [blame] | 192 | case BCMA_CHIP_ID_BCM5357: |
Rafał Miłecki | f0db59e | 2014-06-05 20:20:44 +0200 | [diff] [blame] | 193 | case BCMA_CHIP_ID_BCM53572: |
Florian Fainelli | 459c351 | 2017-06-30 12:02:57 -0700 | [diff] [blame] | 194 | case BCMA_CHIP_ID_BCM53573: |
Rafał Miłecki | 61dba73 | 2016-01-24 16:37:33 +0100 | [diff] [blame] | 195 | case BCMA_CHIP_ID_BCM47094: |
Rafał Miłecki | 0f8ca01 | 2014-03-20 21:09:07 +0100 | [diff] [blame] | 196 | chip->ngpio = 32; |
| 197 | break; |
| 198 | default: |
| 199 | chip->ngpio = 16; |
| 200 | } |
| 201 | |
Rafał Miłecki | 057fcd4 | 2015-03-15 19:43:14 +0100 | [diff] [blame] | 202 | /* |
Felix Fietkau | 2d57b71 | 2015-04-15 15:07:52 +0200 | [diff] [blame] | 203 | * Register SoC GPIO devices with absolute GPIO pin base. |
| 204 | * On MIPS, we don't have Device Tree and we can't use relative (per chip) |
| 205 | * GPIO numbers. |
| 206 | * On some ARM devices, user space may want to access some system GPIO |
| 207 | * pins directly, which is easier to do with a predictable GPIO base. |
Rafał Miłecki | 057fcd4 | 2015-03-15 19:43:14 +0100 | [diff] [blame] | 208 | */ |
Felix Fietkau | 2d57b71 | 2015-04-15 15:07:52 +0200 | [diff] [blame] | 209 | if (IS_BUILTIN(CONFIG_BCM47XX) || |
| 210 | cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC) |
| 211 | chip->base = bus->num * BCMA_GPIO_MAX_PINS; |
| 212 | else |
| 213 | chip->base = -1; |
Hauke Mehrtens | cf0936b | 2012-11-20 22:24:30 +0000 | [diff] [blame] | 214 | |
Linus Walleij | 78d455a | 2015-12-08 16:17:02 +0100 | [diff] [blame] | 215 | err = gpiochip_add_data(chip, cc); |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 216 | if (err) |
| 217 | return err; |
| 218 | |
Linus Walleij | 74f4e0c | 2015-08-14 00:21:45 +0200 | [diff] [blame] | 219 | err = bcma_gpio_irq_init(cc); |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 220 | if (err) { |
Linus Walleij | 74f4e0c | 2015-08-14 00:21:45 +0200 | [diff] [blame] | 221 | gpiochip_remove(chip); |
Rafał Miłecki | 2997609 | 2013-12-12 13:46:03 +0100 | [diff] [blame] | 222 | return err; |
| 223 | } |
| 224 | |
| 225 | return 0; |
Hauke Mehrtens | cf0936b | 2012-11-20 22:24:30 +0000 | [diff] [blame] | 226 | } |
Hauke Mehrtens | c50ae94 | 2013-02-03 23:25:33 +0100 | [diff] [blame] | 227 | |
| 228 | int bcma_gpio_unregister(struct bcma_drv_cc *cc) |
| 229 | { |
Linus Walleij | 74f4e0c | 2015-08-14 00:21:45 +0200 | [diff] [blame] | 230 | bcma_gpio_irq_exit(cc); |
abdoulaye berthe | 88d5e52 | 2014-07-12 22:30:14 +0200 | [diff] [blame] | 231 | gpiochip_remove(&cc->gpio); |
| 232 | return 0; |
Hauke Mehrtens | c50ae94 | 2013-02-03 23:25:33 +0100 | [diff] [blame] | 233 | } |