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.h>
12 #include <linux/irq.h>
13 #include <linux/interrupt.h>
14 #include <linux/irqdomain.h>
15 #include <linux/export.h>
16 #include <linux/bcma/bcma.h>
18 #include "bcma_private.h"
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_BCMA_HOST_SOC)
80 static int bcma_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
82 struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
84 if (cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
85 return irq_find_mapping(cc->irq_domain, gpio);
90 static void bcma_gpio_irq_unmask(struct irq_data *d)
92 struct bcma_drv_cc *cc = irq_data_get_irq_chip_data(d);
93 int gpio = irqd_to_hwirq(d);
95 bcma_chipco_gpio_intmask(cc, BIT(gpio), BIT(gpio));
98 static void bcma_gpio_irq_mask(struct irq_data *d)
100 struct bcma_drv_cc *cc = irq_data_get_irq_chip_data(d);
101 int gpio = irqd_to_hwirq(d);
103 bcma_chipco_gpio_intmask(cc, BIT(gpio), 0);
106 static struct irq_chip bcma_gpio_irq_chip = {
108 .irq_mask = bcma_gpio_irq_mask,
109 .irq_unmask = bcma_gpio_irq_unmask,
112 static irqreturn_t bcma_gpio_irq_handler(int irq, void *dev_id)
114 struct bcma_drv_cc *cc = dev_id;
115 u32 val = bcma_cc_read32(cc, BCMA_CC_GPIOIN);
116 u32 mask = bcma_cc_read32(cc, BCMA_CC_GPIOIRQ);
117 u32 pol = bcma_cc_read32(cc, BCMA_CC_GPIOPOL);
118 u32 irqs = (val ^ pol) & mask;
124 for_each_set_bit(gpio, (unsigned long *)&irqs, cc->gpio.ngpio)
125 generic_handle_irq(bcma_gpio_to_irq(&cc->gpio, gpio));
126 bcma_chipco_gpio_polarity(cc, irqs, val & irqs);
131 static int bcma_gpio_irq_domain_init(struct bcma_drv_cc *cc)
133 struct gpio_chip *chip = &cc->gpio;
134 int gpio, hwirq, err;
136 if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC)
139 cc->irq_domain = irq_domain_add_linear(NULL, chip->ngpio,
140 &irq_domain_simple_ops, cc);
141 if (!cc->irq_domain) {
145 for (gpio = 0; gpio < chip->ngpio; gpio++) {
146 int irq = irq_create_mapping(cc->irq_domain, gpio);
148 irq_set_chip_data(irq, cc);
149 irq_set_chip_and_handler(irq, &bcma_gpio_irq_chip,
153 hwirq = bcma_core_irq(cc->core);
154 err = request_irq(hwirq, bcma_gpio_irq_handler, IRQF_SHARED, "gpio",
159 bcma_cc_set32(cc, BCMA_CC_IRQMASK, BCMA_CC_IRQ_GPIO);
164 for (gpio = 0; gpio < chip->ngpio; gpio++) {
165 int irq = irq_find_mapping(cc->irq_domain, gpio);
167 irq_dispose_mapping(irq);
169 irq_domain_remove(cc->irq_domain);
174 static void bcma_gpio_irq_domain_exit(struct bcma_drv_cc *cc)
176 struct gpio_chip *chip = &cc->gpio;
179 if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC)
182 bcma_cc_mask32(cc, BCMA_CC_IRQMASK, ~BCMA_CC_IRQ_GPIO);
183 free_irq(bcma_core_irq(cc->core), cc);
184 for (gpio = 0; gpio < chip->ngpio; gpio++) {
185 int irq = irq_find_mapping(cc->irq_domain, gpio);
187 irq_dispose_mapping(irq);
189 irq_domain_remove(cc->irq_domain);
192 static int bcma_gpio_irq_domain_init(struct bcma_drv_cc *cc)
197 static void bcma_gpio_irq_domain_exit(struct bcma_drv_cc *cc)
202 int bcma_gpio_init(struct bcma_drv_cc *cc)
204 struct gpio_chip *chip = &cc->gpio;
207 chip->label = "bcma_gpio";
208 chip->owner = THIS_MODULE;
209 chip->request = bcma_gpio_request;
210 chip->free = bcma_gpio_free;
211 chip->get = bcma_gpio_get_value;
212 chip->set = bcma_gpio_set_value;
213 chip->direction_input = bcma_gpio_direction_input;
214 chip->direction_output = bcma_gpio_direction_output;
215 #if IS_BUILTIN(CONFIG_BCMA_HOST_SOC)
216 chip->to_irq = bcma_gpio_to_irq;
219 /* There is just one SoC in one device and its GPIO addresses should be
220 * deterministic to address them more easily. The other buses could get
221 * a random base number. */
222 if (cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
227 err = bcma_gpio_irq_domain_init(cc);
231 err = gpiochip_add(chip);
233 bcma_gpio_irq_domain_exit(cc);
240 int bcma_gpio_unregister(struct bcma_drv_cc *cc)
242 bcma_gpio_irq_domain_exit(cc);
243 return gpiochip_remove(&cc->gpio);