1cf0936b0SHauke Mehrtens /*
2cf0936b0SHauke Mehrtens * Broadcom specific AMBA
3cf0936b0SHauke Mehrtens * GPIO driver
4cf0936b0SHauke Mehrtens *
5cf0936b0SHauke Mehrtens * Copyright 2011, Broadcom Corporation
6cf0936b0SHauke Mehrtens * Copyright 2012, Hauke Mehrtens <hauke@hauke-m.de>
7cf0936b0SHauke Mehrtens *
8cf0936b0SHauke Mehrtens * Licensed under the GNU/GPL. See COPYING for details.
9cf0936b0SHauke Mehrtens */
10cf0936b0SHauke Mehrtens
1174f4e0ccSLinus Walleij #include <linux/gpio/driver.h>
122997609eSRafał Miłecki #include <linux/interrupt.h>
13cf0936b0SHauke Mehrtens #include <linux/export.h>
14f63bc788SAndy Shevchenko #include <linux/property.h>
15f63bc788SAndy Shevchenko
16cf0936b0SHauke Mehrtens #include <linux/bcma/bcma.h>
17cf0936b0SHauke Mehrtens
18cf0936b0SHauke Mehrtens #include "bcma_private.h"
19cf0936b0SHauke Mehrtens
20057fcd42SRafał Miłecki #define BCMA_GPIO_MAX_PINS 32
21057fcd42SRafał Miłecki
bcma_gpio_get_value(struct gpio_chip * chip,unsigned gpio)22cf0936b0SHauke Mehrtens static int bcma_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
23cf0936b0SHauke Mehrtens {
2478d455a2SLinus Walleij struct bcma_drv_cc *cc = gpiochip_get_data(chip);
25cf0936b0SHauke Mehrtens
26cf0936b0SHauke Mehrtens return !!bcma_chipco_gpio_in(cc, 1 << gpio);
27cf0936b0SHauke Mehrtens }
28cf0936b0SHauke Mehrtens
bcma_gpio_set_value(struct gpio_chip * chip,unsigned gpio,int value)29cf0936b0SHauke Mehrtens static void bcma_gpio_set_value(struct gpio_chip *chip, unsigned gpio,
30cf0936b0SHauke Mehrtens int value)
31cf0936b0SHauke Mehrtens {
3278d455a2SLinus Walleij struct bcma_drv_cc *cc = gpiochip_get_data(chip);
33cf0936b0SHauke Mehrtens
34cf0936b0SHauke Mehrtens bcma_chipco_gpio_out(cc, 1 << gpio, value ? 1 << gpio : 0);
35cf0936b0SHauke Mehrtens }
36cf0936b0SHauke Mehrtens
bcma_gpio_direction_input(struct gpio_chip * chip,unsigned gpio)37cf0936b0SHauke Mehrtens static int bcma_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
38cf0936b0SHauke Mehrtens {
3978d455a2SLinus Walleij struct bcma_drv_cc *cc = gpiochip_get_data(chip);
40cf0936b0SHauke Mehrtens
41cf0936b0SHauke Mehrtens bcma_chipco_gpio_outen(cc, 1 << gpio, 0);
42cf0936b0SHauke Mehrtens return 0;
43cf0936b0SHauke Mehrtens }
44cf0936b0SHauke Mehrtens
bcma_gpio_direction_output(struct gpio_chip * chip,unsigned gpio,int value)45cf0936b0SHauke Mehrtens static int bcma_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
46cf0936b0SHauke Mehrtens int value)
47cf0936b0SHauke Mehrtens {
4878d455a2SLinus Walleij struct bcma_drv_cc *cc = gpiochip_get_data(chip);
49cf0936b0SHauke Mehrtens
50cf0936b0SHauke Mehrtens bcma_chipco_gpio_outen(cc, 1 << gpio, 1 << gpio);
51cf0936b0SHauke Mehrtens bcma_chipco_gpio_out(cc, 1 << gpio, value ? 1 << gpio : 0);
52cf0936b0SHauke Mehrtens return 0;
53cf0936b0SHauke Mehrtens }
54cf0936b0SHauke Mehrtens
bcma_gpio_request(struct gpio_chip * chip,unsigned gpio)55cf0936b0SHauke Mehrtens static int bcma_gpio_request(struct gpio_chip *chip, unsigned gpio)
56cf0936b0SHauke Mehrtens {
5778d455a2SLinus Walleij struct bcma_drv_cc *cc = gpiochip_get_data(chip);
58cf0936b0SHauke Mehrtens
59cf0936b0SHauke Mehrtens bcma_chipco_gpio_control(cc, 1 << gpio, 0);
60cf0936b0SHauke Mehrtens /* clear pulldown */
61cf0936b0SHauke Mehrtens bcma_chipco_gpio_pulldown(cc, 1 << gpio, 0);
62cf0936b0SHauke Mehrtens /* Set pullup */
63cf0936b0SHauke Mehrtens bcma_chipco_gpio_pullup(cc, 1 << gpio, 1 << gpio);
64cf0936b0SHauke Mehrtens
65cf0936b0SHauke Mehrtens return 0;
66cf0936b0SHauke Mehrtens }
67cf0936b0SHauke Mehrtens
bcma_gpio_free(struct gpio_chip * chip,unsigned gpio)68cf0936b0SHauke Mehrtens static void bcma_gpio_free(struct gpio_chip *chip, unsigned gpio)
69cf0936b0SHauke Mehrtens {
7078d455a2SLinus Walleij struct bcma_drv_cc *cc = gpiochip_get_data(chip);
71cf0936b0SHauke Mehrtens
72cf0936b0SHauke Mehrtens /* clear pullup */
73cf0936b0SHauke Mehrtens bcma_chipco_gpio_pullup(cc, 1 << gpio, 0);
74cf0936b0SHauke Mehrtens }
75cf0936b0SHauke Mehrtens
760cbfc065SRafał Miłecki #if IS_BUILTIN(CONFIG_BCM47XX) || IS_BUILTIN(CONFIG_ARCH_BCM_5301X)
778f1ca268SHauke Mehrtens
bcma_gpio_irq_unmask(struct irq_data * d)782997609eSRafał Miłecki static void bcma_gpio_irq_unmask(struct irq_data *d)
792997609eSRafał Miłecki {
8074f4e0ccSLinus Walleij struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
8178d455a2SLinus Walleij struct bcma_drv_cc *cc = gpiochip_get_data(gc);
822997609eSRafał Miłecki int gpio = irqd_to_hwirq(d);
83978e55d2SHauke Mehrtens u32 val = bcma_chipco_gpio_in(cc, BIT(gpio));
842997609eSRafał Miłecki
85*55549d6aSLinus Walleij gpiochip_enable_irq(gc, gpio);
86978e55d2SHauke Mehrtens bcma_chipco_gpio_polarity(cc, BIT(gpio), val);
872997609eSRafał Miłecki bcma_chipco_gpio_intmask(cc, BIT(gpio), BIT(gpio));
882997609eSRafał Miłecki }
892997609eSRafał Miłecki
bcma_gpio_irq_mask(struct irq_data * d)902997609eSRafał Miłecki static void bcma_gpio_irq_mask(struct irq_data *d)
912997609eSRafał Miłecki {
9274f4e0ccSLinus Walleij struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
9378d455a2SLinus Walleij struct bcma_drv_cc *cc = gpiochip_get_data(gc);
942997609eSRafał Miłecki int gpio = irqd_to_hwirq(d);
952997609eSRafał Miłecki
962997609eSRafał Miłecki bcma_chipco_gpio_intmask(cc, BIT(gpio), 0);
97*55549d6aSLinus Walleij gpiochip_disable_irq(gc, gpio);
982997609eSRafał Miłecki }
992997609eSRafał Miłecki
100*55549d6aSLinus Walleij static const struct irq_chip bcma_gpio_irq_chip = {
1012997609eSRafał Miłecki .name = "BCMA-GPIO",
1022997609eSRafał Miłecki .irq_mask = bcma_gpio_irq_mask,
1032997609eSRafał Miłecki .irq_unmask = bcma_gpio_irq_unmask,
104*55549d6aSLinus Walleij .flags = IRQCHIP_IMMUTABLE,
105*55549d6aSLinus Walleij GPIOCHIP_IRQ_RESOURCE_HELPERS,
1062997609eSRafał Miłecki };
1072997609eSRafał Miłecki
bcma_gpio_irq_handler(int irq,void * dev_id)1082997609eSRafał Miłecki static irqreturn_t bcma_gpio_irq_handler(int irq, void *dev_id)
1092997609eSRafał Miłecki {
1102997609eSRafał Miłecki struct bcma_drv_cc *cc = dev_id;
11174f4e0ccSLinus Walleij struct gpio_chip *gc = &cc->gpio;
1122997609eSRafał Miłecki u32 val = bcma_cc_read32(cc, BCMA_CC_GPIOIN);
1132997609eSRafał Miłecki u32 mask = bcma_cc_read32(cc, BCMA_CC_GPIOIRQ);
1142997609eSRafał Miłecki u32 pol = bcma_cc_read32(cc, BCMA_CC_GPIOPOL);
115d5ab2adbSRafał Miłecki unsigned long irqs = (val ^ pol) & mask;
1162997609eSRafał Miłecki int gpio;
1172997609eSRafał Miłecki
1182997609eSRafał Miłecki if (!irqs)
1192997609eSRafał Miłecki return IRQ_NONE;
1202997609eSRafał Miłecki
12174f4e0ccSLinus Walleij for_each_set_bit(gpio, &irqs, gc->ngpio)
122f0fbe7bcSThierry Reding generic_handle_domain_irq_safe(gc->irq.domain, gpio);
1232997609eSRafał Miłecki bcma_chipco_gpio_polarity(cc, irqs, val & irqs);
1242997609eSRafał Miłecki
1252997609eSRafał Miłecki return IRQ_HANDLED;
1262997609eSRafał Miłecki }
1272997609eSRafał Miłecki
bcma_gpio_irq_init(struct bcma_drv_cc * cc)12874f4e0ccSLinus Walleij static int bcma_gpio_irq_init(struct bcma_drv_cc *cc)
1292997609eSRafał Miłecki {
1302997609eSRafał Miłecki struct gpio_chip *chip = &cc->gpio;
131a080ecb1SLinus Walleij struct gpio_irq_chip *girq = &chip->irq;
13274f4e0ccSLinus Walleij int hwirq, err;
1332997609eSRafał Miłecki
1342997609eSRafał Miłecki if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC)
1352997609eSRafał Miłecki return 0;
1362997609eSRafał Miłecki
13785eb92e8SHauke Mehrtens hwirq = bcma_core_irq(cc->core, 0);
1382997609eSRafał Miłecki err = request_irq(hwirq, bcma_gpio_irq_handler, IRQF_SHARED, "gpio",
1392997609eSRafał Miłecki cc);
1402997609eSRafał Miłecki if (err)
14174f4e0ccSLinus Walleij return err;
1422997609eSRafał Miłecki
143978e55d2SHauke Mehrtens bcma_chipco_gpio_intmask(cc, ~0, 0);
1442997609eSRafał Miłecki bcma_cc_set32(cc, BCMA_CC_IRQMASK, BCMA_CC_IRQ_GPIO);
1452997609eSRafał Miłecki
146*55549d6aSLinus Walleij gpio_irq_chip_set_chip(girq, &bcma_gpio_irq_chip);
147a080ecb1SLinus Walleij /* This will let us handle the parent IRQ in the driver */
148a080ecb1SLinus Walleij girq->parent_handler = NULL;
149a080ecb1SLinus Walleij girq->num_parents = 0;
150a080ecb1SLinus Walleij girq->parents = NULL;
151a080ecb1SLinus Walleij girq->default_type = IRQ_TYPE_NONE;
152a080ecb1SLinus Walleij girq->handler = handle_simple_irq;
1532997609eSRafał Miłecki
15474f4e0ccSLinus Walleij return 0;
15574f4e0ccSLinus Walleij }
1562997609eSRafał Miłecki
bcma_gpio_irq_exit(struct bcma_drv_cc * cc)15774f4e0ccSLinus Walleij static void bcma_gpio_irq_exit(struct bcma_drv_cc *cc)
15874f4e0ccSLinus Walleij {
1592997609eSRafał Miłecki if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC)
1602997609eSRafał Miłecki return;
1612997609eSRafał Miłecki
1622997609eSRafał Miłecki bcma_cc_mask32(cc, BCMA_CC_IRQMASK, ~BCMA_CC_IRQ_GPIO);
16385eb92e8SHauke Mehrtens free_irq(bcma_core_irq(cc->core, 0), cc);
1642997609eSRafał Miłecki }
1652997609eSRafał Miłecki #else
bcma_gpio_irq_init(struct bcma_drv_cc * cc)16674f4e0ccSLinus Walleij static int bcma_gpio_irq_init(struct bcma_drv_cc *cc)
1672997609eSRafał Miłecki {
1682997609eSRafał Miłecki return 0;
1692997609eSRafał Miłecki }
1702997609eSRafał Miłecki
bcma_gpio_irq_exit(struct bcma_drv_cc * cc)17174f4e0ccSLinus Walleij static void bcma_gpio_irq_exit(struct bcma_drv_cc *cc)
1722997609eSRafał Miłecki {
1732997609eSRafał Miłecki }
1742997609eSRafał Miłecki #endif
1752997609eSRafał Miłecki
bcma_gpio_init(struct bcma_drv_cc * cc)176cf0936b0SHauke Mehrtens int bcma_gpio_init(struct bcma_drv_cc *cc)
177cf0936b0SHauke Mehrtens {
178057fcd42SRafał Miłecki struct bcma_bus *bus = cc->core->bus;
179cf0936b0SHauke Mehrtens struct gpio_chip *chip = &cc->gpio;
1802997609eSRafał Miłecki int err;
181cf0936b0SHauke Mehrtens
182cf0936b0SHauke Mehrtens chip->label = "bcma_gpio";
183cf0936b0SHauke Mehrtens chip->owner = THIS_MODULE;
184cf0936b0SHauke Mehrtens chip->request = bcma_gpio_request;
185cf0936b0SHauke Mehrtens chip->free = bcma_gpio_free;
186cf0936b0SHauke Mehrtens chip->get = bcma_gpio_get_value;
187cf0936b0SHauke Mehrtens chip->set = bcma_gpio_set_value;
188cf0936b0SHauke Mehrtens chip->direction_input = bcma_gpio_direction_input;
189cf0936b0SHauke Mehrtens chip->direction_output = bcma_gpio_direction_output;
1905a1c18b7SRafał Miłecki chip->parent = bus->dev;
191f63bc788SAndy Shevchenko chip->fwnode = dev_fwnode(&cc->core->dev);
192f63bc788SAndy Shevchenko
193057fcd42SRafał Miłecki switch (bus->chipinfo.id) {
194f022ea52SFelix Fietkau case BCMA_CHIP_ID_BCM4707:
1950f8ca014SRafał Miłecki case BCMA_CHIP_ID_BCM5357:
196f0db59e1SRafał Miłecki case BCMA_CHIP_ID_BCM53572:
197459c3514SFlorian Fainelli case BCMA_CHIP_ID_BCM53573:
19861dba73cSRafał Miłecki case BCMA_CHIP_ID_BCM47094:
1990f8ca014SRafał Miłecki chip->ngpio = 32;
2000f8ca014SRafał Miłecki break;
2010f8ca014SRafał Miłecki default:
202cf0936b0SHauke Mehrtens chip->ngpio = 16;
2030f8ca014SRafał Miłecki }
2040f8ca014SRafał Miłecki
205057fcd42SRafał Miłecki /*
2062d57b712SFelix Fietkau * Register SoC GPIO devices with absolute GPIO pin base.
2072d57b712SFelix Fietkau * On MIPS, we don't have Device Tree and we can't use relative (per chip)
2082d57b712SFelix Fietkau * GPIO numbers.
2092d57b712SFelix Fietkau * On some ARM devices, user space may want to access some system GPIO
2102d57b712SFelix Fietkau * pins directly, which is easier to do with a predictable GPIO base.
211057fcd42SRafał Miłecki */
2122d57b712SFelix Fietkau if (IS_BUILTIN(CONFIG_BCM47XX) ||
2132d57b712SFelix Fietkau cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
214057fcd42SRafał Miłecki chip->base = bus->num * BCMA_GPIO_MAX_PINS;
2152d57b712SFelix Fietkau else
216cf0936b0SHauke Mehrtens chip->base = -1;
217cf0936b0SHauke Mehrtens
218a080ecb1SLinus Walleij err = bcma_gpio_irq_init(cc);
2192997609eSRafał Miłecki if (err)
2202997609eSRafał Miłecki return err;
2212997609eSRafał Miłecki
222a080ecb1SLinus Walleij err = gpiochip_add_data(chip, cc);
2232997609eSRafał Miłecki if (err) {
224a080ecb1SLinus Walleij bcma_gpio_irq_exit(cc);
2252997609eSRafał Miłecki return err;
2262997609eSRafał Miłecki }
2272997609eSRafał Miłecki
2282997609eSRafał Miłecki return 0;
229cf0936b0SHauke Mehrtens }
230c50ae947SHauke Mehrtens
bcma_gpio_unregister(struct bcma_drv_cc * cc)231c50ae947SHauke Mehrtens int bcma_gpio_unregister(struct bcma_drv_cc *cc)
232c50ae947SHauke Mehrtens {
23374f4e0ccSLinus Walleij bcma_gpio_irq_exit(cc);
23488d5e520Sabdoulaye berthe gpiochip_remove(&cc->gpio);
23588d5e520Sabdoulaye berthe return 0;
236c50ae947SHauke Mehrtens }
237