18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Broadcom specific AMBA 38c2ecf20Sopenharmony_ci * GPIO driver 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright 2011, Broadcom Corporation 68c2ecf20Sopenharmony_ci * Copyright 2012, Hauke Mehrtens <hauke@hauke-m.de> 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Licensed under the GNU/GPL. See COPYING for details. 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/gpio/driver.h> 128c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 138c2ecf20Sopenharmony_ci#include <linux/export.h> 148c2ecf20Sopenharmony_ci#include <linux/bcma/bcma.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#include "bcma_private.h" 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#define BCMA_GPIO_MAX_PINS 32 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_cistatic int bcma_gpio_get_value(struct gpio_chip *chip, unsigned gpio) 218c2ecf20Sopenharmony_ci{ 228c2ecf20Sopenharmony_ci struct bcma_drv_cc *cc = gpiochip_get_data(chip); 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci return !!bcma_chipco_gpio_in(cc, 1 << gpio); 258c2ecf20Sopenharmony_ci} 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_cistatic void bcma_gpio_set_value(struct gpio_chip *chip, unsigned gpio, 288c2ecf20Sopenharmony_ci int value) 298c2ecf20Sopenharmony_ci{ 308c2ecf20Sopenharmony_ci struct bcma_drv_cc *cc = gpiochip_get_data(chip); 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci bcma_chipco_gpio_out(cc, 1 << gpio, value ? 1 << gpio : 0); 338c2ecf20Sopenharmony_ci} 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_cistatic int bcma_gpio_direction_input(struct gpio_chip *chip, unsigned gpio) 368c2ecf20Sopenharmony_ci{ 378c2ecf20Sopenharmony_ci struct bcma_drv_cc *cc = gpiochip_get_data(chip); 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci bcma_chipco_gpio_outen(cc, 1 << gpio, 0); 408c2ecf20Sopenharmony_ci return 0; 418c2ecf20Sopenharmony_ci} 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_cistatic int bcma_gpio_direction_output(struct gpio_chip *chip, unsigned gpio, 448c2ecf20Sopenharmony_ci int value) 458c2ecf20Sopenharmony_ci{ 468c2ecf20Sopenharmony_ci struct bcma_drv_cc *cc = gpiochip_get_data(chip); 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci bcma_chipco_gpio_outen(cc, 1 << gpio, 1 << gpio); 498c2ecf20Sopenharmony_ci bcma_chipco_gpio_out(cc, 1 << gpio, value ? 1 << gpio : 0); 508c2ecf20Sopenharmony_ci return 0; 518c2ecf20Sopenharmony_ci} 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_cistatic int bcma_gpio_request(struct gpio_chip *chip, unsigned gpio) 548c2ecf20Sopenharmony_ci{ 558c2ecf20Sopenharmony_ci struct bcma_drv_cc *cc = gpiochip_get_data(chip); 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci bcma_chipco_gpio_control(cc, 1 << gpio, 0); 588c2ecf20Sopenharmony_ci /* clear pulldown */ 598c2ecf20Sopenharmony_ci bcma_chipco_gpio_pulldown(cc, 1 << gpio, 0); 608c2ecf20Sopenharmony_ci /* Set pullup */ 618c2ecf20Sopenharmony_ci bcma_chipco_gpio_pullup(cc, 1 << gpio, 1 << gpio); 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci return 0; 648c2ecf20Sopenharmony_ci} 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_cistatic void bcma_gpio_free(struct gpio_chip *chip, unsigned gpio) 678c2ecf20Sopenharmony_ci{ 688c2ecf20Sopenharmony_ci struct bcma_drv_cc *cc = gpiochip_get_data(chip); 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci /* clear pullup */ 718c2ecf20Sopenharmony_ci bcma_chipco_gpio_pullup(cc, 1 << gpio, 0); 728c2ecf20Sopenharmony_ci} 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci#if IS_BUILTIN(CONFIG_BCM47XX) || IS_BUILTIN(CONFIG_ARCH_BCM_5301X) 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_cistatic void bcma_gpio_irq_unmask(struct irq_data *d) 778c2ecf20Sopenharmony_ci{ 788c2ecf20Sopenharmony_ci struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 798c2ecf20Sopenharmony_ci struct bcma_drv_cc *cc = gpiochip_get_data(gc); 808c2ecf20Sopenharmony_ci int gpio = irqd_to_hwirq(d); 818c2ecf20Sopenharmony_ci u32 val = bcma_chipco_gpio_in(cc, BIT(gpio)); 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci bcma_chipco_gpio_polarity(cc, BIT(gpio), val); 848c2ecf20Sopenharmony_ci bcma_chipco_gpio_intmask(cc, BIT(gpio), BIT(gpio)); 858c2ecf20Sopenharmony_ci} 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_cistatic void bcma_gpio_irq_mask(struct irq_data *d) 888c2ecf20Sopenharmony_ci{ 898c2ecf20Sopenharmony_ci struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 908c2ecf20Sopenharmony_ci struct bcma_drv_cc *cc = gpiochip_get_data(gc); 918c2ecf20Sopenharmony_ci int gpio = irqd_to_hwirq(d); 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci bcma_chipco_gpio_intmask(cc, BIT(gpio), 0); 948c2ecf20Sopenharmony_ci} 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_cistatic struct irq_chip bcma_gpio_irq_chip = { 978c2ecf20Sopenharmony_ci .name = "BCMA-GPIO", 988c2ecf20Sopenharmony_ci .irq_mask = bcma_gpio_irq_mask, 998c2ecf20Sopenharmony_ci .irq_unmask = bcma_gpio_irq_unmask, 1008c2ecf20Sopenharmony_ci}; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_cistatic irqreturn_t bcma_gpio_irq_handler(int irq, void *dev_id) 1038c2ecf20Sopenharmony_ci{ 1048c2ecf20Sopenharmony_ci struct bcma_drv_cc *cc = dev_id; 1058c2ecf20Sopenharmony_ci struct gpio_chip *gc = &cc->gpio; 1068c2ecf20Sopenharmony_ci u32 val = bcma_cc_read32(cc, BCMA_CC_GPIOIN); 1078c2ecf20Sopenharmony_ci u32 mask = bcma_cc_read32(cc, BCMA_CC_GPIOIRQ); 1088c2ecf20Sopenharmony_ci u32 pol = bcma_cc_read32(cc, BCMA_CC_GPIOPOL); 1098c2ecf20Sopenharmony_ci unsigned long irqs = (val ^ pol) & mask; 1108c2ecf20Sopenharmony_ci int gpio; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci if (!irqs) 1138c2ecf20Sopenharmony_ci return IRQ_NONE; 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci for_each_set_bit(gpio, &irqs, gc->ngpio) 1168c2ecf20Sopenharmony_ci generic_handle_irq(irq_find_mapping(gc->irq.domain, gpio)); 1178c2ecf20Sopenharmony_ci bcma_chipco_gpio_polarity(cc, irqs, val & irqs); 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci return IRQ_HANDLED; 1208c2ecf20Sopenharmony_ci} 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_cistatic int bcma_gpio_irq_init(struct bcma_drv_cc *cc) 1238c2ecf20Sopenharmony_ci{ 1248c2ecf20Sopenharmony_ci struct gpio_chip *chip = &cc->gpio; 1258c2ecf20Sopenharmony_ci struct gpio_irq_chip *girq = &chip->irq; 1268c2ecf20Sopenharmony_ci int hwirq, err; 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC) 1298c2ecf20Sopenharmony_ci return 0; 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci hwirq = bcma_core_irq(cc->core, 0); 1328c2ecf20Sopenharmony_ci err = request_irq(hwirq, bcma_gpio_irq_handler, IRQF_SHARED, "gpio", 1338c2ecf20Sopenharmony_ci cc); 1348c2ecf20Sopenharmony_ci if (err) 1358c2ecf20Sopenharmony_ci return err; 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci bcma_chipco_gpio_intmask(cc, ~0, 0); 1388c2ecf20Sopenharmony_ci bcma_cc_set32(cc, BCMA_CC_IRQMASK, BCMA_CC_IRQ_GPIO); 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci girq->chip = &bcma_gpio_irq_chip; 1418c2ecf20Sopenharmony_ci /* This will let us handle the parent IRQ in the driver */ 1428c2ecf20Sopenharmony_ci girq->parent_handler = NULL; 1438c2ecf20Sopenharmony_ci girq->num_parents = 0; 1448c2ecf20Sopenharmony_ci girq->parents = NULL; 1458c2ecf20Sopenharmony_ci girq->default_type = IRQ_TYPE_NONE; 1468c2ecf20Sopenharmony_ci girq->handler = handle_simple_irq; 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci return 0; 1498c2ecf20Sopenharmony_ci} 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_cistatic void bcma_gpio_irq_exit(struct bcma_drv_cc *cc) 1528c2ecf20Sopenharmony_ci{ 1538c2ecf20Sopenharmony_ci if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC) 1548c2ecf20Sopenharmony_ci return; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci bcma_cc_mask32(cc, BCMA_CC_IRQMASK, ~BCMA_CC_IRQ_GPIO); 1578c2ecf20Sopenharmony_ci free_irq(bcma_core_irq(cc->core, 0), cc); 1588c2ecf20Sopenharmony_ci} 1598c2ecf20Sopenharmony_ci#else 1608c2ecf20Sopenharmony_cistatic int bcma_gpio_irq_init(struct bcma_drv_cc *cc) 1618c2ecf20Sopenharmony_ci{ 1628c2ecf20Sopenharmony_ci return 0; 1638c2ecf20Sopenharmony_ci} 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_cistatic void bcma_gpio_irq_exit(struct bcma_drv_cc *cc) 1668c2ecf20Sopenharmony_ci{ 1678c2ecf20Sopenharmony_ci} 1688c2ecf20Sopenharmony_ci#endif 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ciint bcma_gpio_init(struct bcma_drv_cc *cc) 1718c2ecf20Sopenharmony_ci{ 1728c2ecf20Sopenharmony_ci struct bcma_bus *bus = cc->core->bus; 1738c2ecf20Sopenharmony_ci struct gpio_chip *chip = &cc->gpio; 1748c2ecf20Sopenharmony_ci int err; 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci chip->label = "bcma_gpio"; 1778c2ecf20Sopenharmony_ci chip->owner = THIS_MODULE; 1788c2ecf20Sopenharmony_ci chip->request = bcma_gpio_request; 1798c2ecf20Sopenharmony_ci chip->free = bcma_gpio_free; 1808c2ecf20Sopenharmony_ci chip->get = bcma_gpio_get_value; 1818c2ecf20Sopenharmony_ci chip->set = bcma_gpio_set_value; 1828c2ecf20Sopenharmony_ci chip->direction_input = bcma_gpio_direction_input; 1838c2ecf20Sopenharmony_ci chip->direction_output = bcma_gpio_direction_output; 1848c2ecf20Sopenharmony_ci chip->owner = THIS_MODULE; 1858c2ecf20Sopenharmony_ci chip->parent = bus->dev; 1868c2ecf20Sopenharmony_ci#if IS_BUILTIN(CONFIG_OF) 1878c2ecf20Sopenharmony_ci chip->of_node = cc->core->dev.of_node; 1888c2ecf20Sopenharmony_ci#endif 1898c2ecf20Sopenharmony_ci switch (bus->chipinfo.id) { 1908c2ecf20Sopenharmony_ci case BCMA_CHIP_ID_BCM4707: 1918c2ecf20Sopenharmony_ci case BCMA_CHIP_ID_BCM5357: 1928c2ecf20Sopenharmony_ci case BCMA_CHIP_ID_BCM53572: 1938c2ecf20Sopenharmony_ci case BCMA_CHIP_ID_BCM53573: 1948c2ecf20Sopenharmony_ci case BCMA_CHIP_ID_BCM47094: 1958c2ecf20Sopenharmony_ci chip->ngpio = 32; 1968c2ecf20Sopenharmony_ci break; 1978c2ecf20Sopenharmony_ci default: 1988c2ecf20Sopenharmony_ci chip->ngpio = 16; 1998c2ecf20Sopenharmony_ci } 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci /* 2028c2ecf20Sopenharmony_ci * Register SoC GPIO devices with absolute GPIO pin base. 2038c2ecf20Sopenharmony_ci * On MIPS, we don't have Device Tree and we can't use relative (per chip) 2048c2ecf20Sopenharmony_ci * GPIO numbers. 2058c2ecf20Sopenharmony_ci * On some ARM devices, user space may want to access some system GPIO 2068c2ecf20Sopenharmony_ci * pins directly, which is easier to do with a predictable GPIO base. 2078c2ecf20Sopenharmony_ci */ 2088c2ecf20Sopenharmony_ci if (IS_BUILTIN(CONFIG_BCM47XX) || 2098c2ecf20Sopenharmony_ci cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC) 2108c2ecf20Sopenharmony_ci chip->base = bus->num * BCMA_GPIO_MAX_PINS; 2118c2ecf20Sopenharmony_ci else 2128c2ecf20Sopenharmony_ci chip->base = -1; 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci err = bcma_gpio_irq_init(cc); 2158c2ecf20Sopenharmony_ci if (err) 2168c2ecf20Sopenharmony_ci return err; 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci err = gpiochip_add_data(chip, cc); 2198c2ecf20Sopenharmony_ci if (err) { 2208c2ecf20Sopenharmony_ci bcma_gpio_irq_exit(cc); 2218c2ecf20Sopenharmony_ci return err; 2228c2ecf20Sopenharmony_ci } 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci return 0; 2258c2ecf20Sopenharmony_ci} 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ciint bcma_gpio_unregister(struct bcma_drv_cc *cc) 2288c2ecf20Sopenharmony_ci{ 2298c2ecf20Sopenharmony_ci bcma_gpio_irq_exit(cc); 2308c2ecf20Sopenharmony_ci gpiochip_remove(&cc->gpio); 2318c2ecf20Sopenharmony_ci return 0; 2328c2ecf20Sopenharmony_ci} 233