Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Broadcom specific AMBA
0003  * GPIO driver
0004  *
0005  * Copyright 2011, Broadcom Corporation
0006  * Copyright 2012, Hauke Mehrtens <hauke@hauke-m.de>
0007  *
0008  * Licensed under the GNU/GPL. See COPYING for details.
0009  */
0010 
0011 #include <linux/gpio/driver.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/export.h>
0014 #include <linux/property.h>
0015 
0016 #include <linux/bcma/bcma.h>
0017 
0018 #include "bcma_private.h"
0019 
0020 #define BCMA_GPIO_MAX_PINS  32
0021 
0022 static int bcma_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
0023 {
0024     struct bcma_drv_cc *cc = gpiochip_get_data(chip);
0025 
0026     return !!bcma_chipco_gpio_in(cc, 1 << gpio);
0027 }
0028 
0029 static void bcma_gpio_set_value(struct gpio_chip *chip, unsigned gpio,
0030                 int value)
0031 {
0032     struct bcma_drv_cc *cc = gpiochip_get_data(chip);
0033 
0034     bcma_chipco_gpio_out(cc, 1 << gpio, value ? 1 << gpio : 0);
0035 }
0036 
0037 static int bcma_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
0038 {
0039     struct bcma_drv_cc *cc = gpiochip_get_data(chip);
0040 
0041     bcma_chipco_gpio_outen(cc, 1 << gpio, 0);
0042     return 0;
0043 }
0044 
0045 static int bcma_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
0046                       int value)
0047 {
0048     struct bcma_drv_cc *cc = gpiochip_get_data(chip);
0049 
0050     bcma_chipco_gpio_outen(cc, 1 << gpio, 1 << gpio);
0051     bcma_chipco_gpio_out(cc, 1 << gpio, value ? 1 << gpio : 0);
0052     return 0;
0053 }
0054 
0055 static int bcma_gpio_request(struct gpio_chip *chip, unsigned gpio)
0056 {
0057     struct bcma_drv_cc *cc = gpiochip_get_data(chip);
0058 
0059     bcma_chipco_gpio_control(cc, 1 << gpio, 0);
0060     /* clear pulldown */
0061     bcma_chipco_gpio_pulldown(cc, 1 << gpio, 0);
0062     /* Set pullup */
0063     bcma_chipco_gpio_pullup(cc, 1 << gpio, 1 << gpio);
0064 
0065     return 0;
0066 }
0067 
0068 static void bcma_gpio_free(struct gpio_chip *chip, unsigned gpio)
0069 {
0070     struct bcma_drv_cc *cc = gpiochip_get_data(chip);
0071 
0072     /* clear pullup */
0073     bcma_chipco_gpio_pullup(cc, 1 << gpio, 0);
0074 }
0075 
0076 #if IS_BUILTIN(CONFIG_BCM47XX) || IS_BUILTIN(CONFIG_ARCH_BCM_5301X)
0077 
0078 static void bcma_gpio_irq_unmask(struct irq_data *d)
0079 {
0080     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0081     struct bcma_drv_cc *cc = gpiochip_get_data(gc);
0082     int gpio = irqd_to_hwirq(d);
0083     u32 val = bcma_chipco_gpio_in(cc, BIT(gpio));
0084 
0085     bcma_chipco_gpio_polarity(cc, BIT(gpio), val);
0086     bcma_chipco_gpio_intmask(cc, BIT(gpio), BIT(gpio));
0087 }
0088 
0089 static void bcma_gpio_irq_mask(struct irq_data *d)
0090 {
0091     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0092     struct bcma_drv_cc *cc = gpiochip_get_data(gc);
0093     int gpio = irqd_to_hwirq(d);
0094 
0095     bcma_chipco_gpio_intmask(cc, BIT(gpio), 0);
0096 }
0097 
0098 static struct irq_chip bcma_gpio_irq_chip = {
0099     .name       = "BCMA-GPIO",
0100     .irq_mask   = bcma_gpio_irq_mask,
0101     .irq_unmask = bcma_gpio_irq_unmask,
0102 };
0103 
0104 static irqreturn_t bcma_gpio_irq_handler(int irq, void *dev_id)
0105 {
0106     struct bcma_drv_cc *cc = dev_id;
0107     struct gpio_chip *gc = &cc->gpio;
0108     u32 val = bcma_cc_read32(cc, BCMA_CC_GPIOIN);
0109     u32 mask = bcma_cc_read32(cc, BCMA_CC_GPIOIRQ);
0110     u32 pol = bcma_cc_read32(cc, BCMA_CC_GPIOPOL);
0111     unsigned long irqs = (val ^ pol) & mask;
0112     int gpio;
0113 
0114     if (!irqs)
0115         return IRQ_NONE;
0116 
0117     for_each_set_bit(gpio, &irqs, gc->ngpio)
0118         generic_handle_irq(irq_find_mapping(gc->irq.domain, gpio));
0119     bcma_chipco_gpio_polarity(cc, irqs, val & irqs);
0120 
0121     return IRQ_HANDLED;
0122 }
0123 
0124 static int bcma_gpio_irq_init(struct bcma_drv_cc *cc)
0125 {
0126     struct gpio_chip *chip = &cc->gpio;
0127     struct gpio_irq_chip *girq = &chip->irq;
0128     int hwirq, err;
0129 
0130     if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC)
0131         return 0;
0132 
0133     hwirq = bcma_core_irq(cc->core, 0);
0134     err = request_irq(hwirq, bcma_gpio_irq_handler, IRQF_SHARED, "gpio",
0135               cc);
0136     if (err)
0137         return err;
0138 
0139     bcma_chipco_gpio_intmask(cc, ~0, 0);
0140     bcma_cc_set32(cc, BCMA_CC_IRQMASK, BCMA_CC_IRQ_GPIO);
0141 
0142     girq->chip = &bcma_gpio_irq_chip;
0143     /* This will let us handle the parent IRQ in the driver */
0144     girq->parent_handler = NULL;
0145     girq->num_parents = 0;
0146     girq->parents = NULL;
0147     girq->default_type = IRQ_TYPE_NONE;
0148     girq->handler = handle_simple_irq;
0149 
0150     return 0;
0151 }
0152 
0153 static void bcma_gpio_irq_exit(struct bcma_drv_cc *cc)
0154 {
0155     if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC)
0156         return;
0157 
0158     bcma_cc_mask32(cc, BCMA_CC_IRQMASK, ~BCMA_CC_IRQ_GPIO);
0159     free_irq(bcma_core_irq(cc->core, 0), cc);
0160 }
0161 #else
0162 static int bcma_gpio_irq_init(struct bcma_drv_cc *cc)
0163 {
0164     return 0;
0165 }
0166 
0167 static void bcma_gpio_irq_exit(struct bcma_drv_cc *cc)
0168 {
0169 }
0170 #endif
0171 
0172 int bcma_gpio_init(struct bcma_drv_cc *cc)
0173 {
0174     struct bcma_bus *bus = cc->core->bus;
0175     struct gpio_chip *chip = &cc->gpio;
0176     int err;
0177 
0178     chip->label     = "bcma_gpio";
0179     chip->owner     = THIS_MODULE;
0180     chip->request       = bcma_gpio_request;
0181     chip->free      = bcma_gpio_free;
0182     chip->get       = bcma_gpio_get_value;
0183     chip->set       = bcma_gpio_set_value;
0184     chip->direction_input   = bcma_gpio_direction_input;
0185     chip->direction_output  = bcma_gpio_direction_output;
0186     chip->parent        = bus->dev;
0187     chip->fwnode        = dev_fwnode(&cc->core->dev);
0188 
0189     switch (bus->chipinfo.id) {
0190     case BCMA_CHIP_ID_BCM4707:
0191     case BCMA_CHIP_ID_BCM5357:
0192     case BCMA_CHIP_ID_BCM53572:
0193     case BCMA_CHIP_ID_BCM53573:
0194     case BCMA_CHIP_ID_BCM47094:
0195         chip->ngpio = 32;
0196         break;
0197     default:
0198         chip->ngpio = 16;
0199     }
0200 
0201     /*
0202      * Register SoC GPIO devices with absolute GPIO pin base.
0203      * On MIPS, we don't have Device Tree and we can't use relative (per chip)
0204      * GPIO numbers.
0205      * On some ARM devices, user space may want to access some system GPIO
0206      * pins directly, which is easier to do with a predictable GPIO base.
0207      */
0208     if (IS_BUILTIN(CONFIG_BCM47XX) ||
0209         cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
0210         chip->base      = bus->num * BCMA_GPIO_MAX_PINS;
0211     else
0212         chip->base      = -1;
0213 
0214     err = bcma_gpio_irq_init(cc);
0215     if (err)
0216         return err;
0217 
0218     err = gpiochip_add_data(chip, cc);
0219     if (err) {
0220         bcma_gpio_irq_exit(cc);
0221         return err;
0222     }
0223 
0224     return 0;
0225 }
0226 
0227 int bcma_gpio_unregister(struct bcma_drv_cc *cc)
0228 {
0229     bcma_gpio_irq_exit(cc);
0230     gpiochip_remove(&cc->gpio);
0231     return 0;
0232 }