Loading...
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
11#include <linux/gpio/driver.h>
12#include <linux/interrupt.h>
13#include <linux/export.h>
14#include <linux/bcma/bcma.h>
15
16#include "bcma_private.h"
17
18#define BCMA_GPIO_MAX_PINS 32
19
20static int bcma_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
21{
22 struct bcma_drv_cc *cc = gpiochip_get_data(chip);
23
24 return !!bcma_chipco_gpio_in(cc, 1 << gpio);
25}
26
27static void bcma_gpio_set_value(struct gpio_chip *chip, unsigned gpio,
28 int value)
29{
30 struct bcma_drv_cc *cc = gpiochip_get_data(chip);
31
32 bcma_chipco_gpio_out(cc, 1 << gpio, value ? 1 << gpio : 0);
33}
34
35static int bcma_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
36{
37 struct bcma_drv_cc *cc = gpiochip_get_data(chip);
38
39 bcma_chipco_gpio_outen(cc, 1 << gpio, 0);
40 return 0;
41}
42
43static int bcma_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
44 int value)
45{
46 struct bcma_drv_cc *cc = gpiochip_get_data(chip);
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
53static int bcma_gpio_request(struct gpio_chip *chip, unsigned gpio)
54{
55 struct bcma_drv_cc *cc = gpiochip_get_data(chip);
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
66static void bcma_gpio_free(struct gpio_chip *chip, unsigned gpio)
67{
68 struct bcma_drv_cc *cc = gpiochip_get_data(chip);
69
70 /* clear pullup */
71 bcma_chipco_gpio_pullup(cc, 1 << gpio, 0);
72}
73
74#if IS_BUILTIN(CONFIG_BCM47XX) || IS_BUILTIN(CONFIG_ARCH_BCM_5301X)
75
76static void bcma_gpio_irq_unmask(struct irq_data *d)
77{
78 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
79 struct bcma_drv_cc *cc = gpiochip_get_data(gc);
80 int gpio = irqd_to_hwirq(d);
81 u32 val = bcma_chipco_gpio_in(cc, BIT(gpio));
82
83 bcma_chipco_gpio_polarity(cc, BIT(gpio), val);
84 bcma_chipco_gpio_intmask(cc, BIT(gpio), BIT(gpio));
85}
86
87static void bcma_gpio_irq_mask(struct irq_data *d)
88{
89 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
90 struct bcma_drv_cc *cc = gpiochip_get_data(gc);
91 int gpio = irqd_to_hwirq(d);
92
93 bcma_chipco_gpio_intmask(cc, BIT(gpio), 0);
94}
95
96static 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
102static irqreturn_t bcma_gpio_irq_handler(int irq, void *dev_id)
103{
104 struct bcma_drv_cc *cc = dev_id;
105 struct gpio_chip *gc = &cc->gpio;
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);
109 unsigned long irqs = (val ^ pol) & mask;
110 int gpio;
111
112 if (!irqs)
113 return IRQ_NONE;
114
115 for_each_set_bit(gpio, &irqs, gc->ngpio)
116 generic_handle_irq(irq_find_mapping(gc->irq.domain, gpio));
117 bcma_chipco_gpio_polarity(cc, irqs, val & irqs);
118
119 return IRQ_HANDLED;
120}
121
122static int bcma_gpio_irq_init(struct bcma_drv_cc *cc)
123{
124 struct gpio_chip *chip = &cc->gpio;
125 struct gpio_irq_chip *girq = &chip->irq;
126 int hwirq, err;
127
128 if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC)
129 return 0;
130
131 hwirq = bcma_core_irq(cc->core, 0);
132 err = request_irq(hwirq, bcma_gpio_irq_handler, IRQF_SHARED, "gpio",
133 cc);
134 if (err)
135 return err;
136
137 bcma_chipco_gpio_intmask(cc, ~0, 0);
138 bcma_cc_set32(cc, BCMA_CC_IRQMASK, BCMA_CC_IRQ_GPIO);
139
140 girq->chip = &bcma_gpio_irq_chip;
141 /* This will let us handle the parent IRQ in the driver */
142 girq->parent_handler = NULL;
143 girq->num_parents = 0;
144 girq->parents = NULL;
145 girq->default_type = IRQ_TYPE_NONE;
146 girq->handler = handle_simple_irq;
147
148 return 0;
149}
150
151static void bcma_gpio_irq_exit(struct bcma_drv_cc *cc)
152{
153 if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC)
154 return;
155
156 bcma_cc_mask32(cc, BCMA_CC_IRQMASK, ~BCMA_CC_IRQ_GPIO);
157 free_irq(bcma_core_irq(cc->core, 0), cc);
158}
159#else
160static int bcma_gpio_irq_init(struct bcma_drv_cc *cc)
161{
162 return 0;
163}
164
165static void bcma_gpio_irq_exit(struct bcma_drv_cc *cc)
166{
167}
168#endif
169
170int bcma_gpio_init(struct bcma_drv_cc *cc)
171{
172 struct bcma_bus *bus = cc->core->bus;
173 struct gpio_chip *chip = &cc->gpio;
174 int err;
175
176 chip->label = "bcma_gpio";
177 chip->owner = THIS_MODULE;
178 chip->request = bcma_gpio_request;
179 chip->free = bcma_gpio_free;
180 chip->get = bcma_gpio_get_value;
181 chip->set = bcma_gpio_set_value;
182 chip->direction_input = bcma_gpio_direction_input;
183 chip->direction_output = bcma_gpio_direction_output;
184 chip->owner = THIS_MODULE;
185 chip->parent = bus->dev;
186#if IS_BUILTIN(CONFIG_OF)
187 chip->of_node = cc->core->dev.of_node;
188#endif
189 switch (bus->chipinfo.id) {
190 case BCMA_CHIP_ID_BCM4707:
191 case BCMA_CHIP_ID_BCM5357:
192 case BCMA_CHIP_ID_BCM53572:
193 case BCMA_CHIP_ID_BCM53573:
194 case BCMA_CHIP_ID_BCM47094:
195 chip->ngpio = 32;
196 break;
197 default:
198 chip->ngpio = 16;
199 }
200
201 /*
202 * Register SoC GPIO devices with absolute GPIO pin base.
203 * On MIPS, we don't have Device Tree and we can't use relative (per chip)
204 * GPIO numbers.
205 * On some ARM devices, user space may want to access some system GPIO
206 * pins directly, which is easier to do with a predictable GPIO base.
207 */
208 if (IS_BUILTIN(CONFIG_BCM47XX) ||
209 cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
210 chip->base = bus->num * BCMA_GPIO_MAX_PINS;
211 else
212 chip->base = -1;
213
214 err = bcma_gpio_irq_init(cc);
215 if (err)
216 return err;
217
218 err = gpiochip_add_data(chip, cc);
219 if (err) {
220 bcma_gpio_irq_exit(cc);
221 return err;
222 }
223
224 return 0;
225}
226
227int bcma_gpio_unregister(struct bcma_drv_cc *cc)
228{
229 bcma_gpio_irq_exit(cc);
230 gpiochip_remove(&cc->gpio);
231 return 0;
232}
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
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>
17
18#include "bcma_private.h"
19
20static inline struct bcma_drv_cc *bcma_gpio_get_cc(struct gpio_chip *chip)
21{
22 return container_of(chip, struct bcma_drv_cc, gpio);
23}
24
25static int bcma_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
26{
27 struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
28
29 return !!bcma_chipco_gpio_in(cc, 1 << gpio);
30}
31
32static void bcma_gpio_set_value(struct gpio_chip *chip, unsigned gpio,
33 int value)
34{
35 struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
36
37 bcma_chipco_gpio_out(cc, 1 << gpio, value ? 1 << gpio : 0);
38}
39
40static int bcma_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
41{
42 struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
43
44 bcma_chipco_gpio_outen(cc, 1 << gpio, 0);
45 return 0;
46}
47
48static int bcma_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
49 int value)
50{
51 struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
52
53 bcma_chipco_gpio_outen(cc, 1 << gpio, 1 << gpio);
54 bcma_chipco_gpio_out(cc, 1 << gpio, value ? 1 << gpio : 0);
55 return 0;
56}
57
58static int bcma_gpio_request(struct gpio_chip *chip, unsigned gpio)
59{
60 struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
61
62 bcma_chipco_gpio_control(cc, 1 << gpio, 0);
63 /* clear pulldown */
64 bcma_chipco_gpio_pulldown(cc, 1 << gpio, 0);
65 /* Set pullup */
66 bcma_chipco_gpio_pullup(cc, 1 << gpio, 1 << gpio);
67
68 return 0;
69}
70
71static void bcma_gpio_free(struct gpio_chip *chip, unsigned gpio)
72{
73 struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
74
75 /* clear pullup */
76 bcma_chipco_gpio_pullup(cc, 1 << gpio, 0);
77}
78
79#if IS_BUILTIN(CONFIG_BCMA_HOST_SOC)
80static int bcma_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
81{
82 struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
83
84 if (cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
85 return irq_find_mapping(cc->irq_domain, gpio);
86 else
87 return -EINVAL;
88}
89
90static void bcma_gpio_irq_unmask(struct irq_data *d)
91{
92 struct bcma_drv_cc *cc = irq_data_get_irq_chip_data(d);
93 int gpio = irqd_to_hwirq(d);
94 u32 val = bcma_chipco_gpio_in(cc, BIT(gpio));
95
96 bcma_chipco_gpio_polarity(cc, BIT(gpio), val);
97 bcma_chipco_gpio_intmask(cc, BIT(gpio), BIT(gpio));
98}
99
100static void bcma_gpio_irq_mask(struct irq_data *d)
101{
102 struct bcma_drv_cc *cc = irq_data_get_irq_chip_data(d);
103 int gpio = irqd_to_hwirq(d);
104
105 bcma_chipco_gpio_intmask(cc, BIT(gpio), 0);
106}
107
108static struct irq_chip bcma_gpio_irq_chip = {
109 .name = "BCMA-GPIO",
110 .irq_mask = bcma_gpio_irq_mask,
111 .irq_unmask = bcma_gpio_irq_unmask,
112};
113
114static irqreturn_t bcma_gpio_irq_handler(int irq, void *dev_id)
115{
116 struct bcma_drv_cc *cc = dev_id;
117 u32 val = bcma_cc_read32(cc, BCMA_CC_GPIOIN);
118 u32 mask = bcma_cc_read32(cc, BCMA_CC_GPIOIRQ);
119 u32 pol = bcma_cc_read32(cc, BCMA_CC_GPIOPOL);
120 unsigned long irqs = (val ^ pol) & mask;
121 int gpio;
122
123 if (!irqs)
124 return IRQ_NONE;
125
126 for_each_set_bit(gpio, &irqs, cc->gpio.ngpio)
127 generic_handle_irq(bcma_gpio_to_irq(&cc->gpio, gpio));
128 bcma_chipco_gpio_polarity(cc, irqs, val & irqs);
129
130 return IRQ_HANDLED;
131}
132
133static int bcma_gpio_irq_domain_init(struct bcma_drv_cc *cc)
134{
135 struct gpio_chip *chip = &cc->gpio;
136 int gpio, hwirq, err;
137
138 if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC)
139 return 0;
140
141 cc->irq_domain = irq_domain_add_linear(NULL, chip->ngpio,
142 &irq_domain_simple_ops, cc);
143 if (!cc->irq_domain) {
144 err = -ENODEV;
145 goto err_irq_domain;
146 }
147 for (gpio = 0; gpio < chip->ngpio; gpio++) {
148 int irq = irq_create_mapping(cc->irq_domain, gpio);
149
150 irq_set_chip_data(irq, cc);
151 irq_set_chip_and_handler(irq, &bcma_gpio_irq_chip,
152 handle_simple_irq);
153 }
154
155 hwirq = bcma_core_irq(cc->core);
156 err = request_irq(hwirq, bcma_gpio_irq_handler, IRQF_SHARED, "gpio",
157 cc);
158 if (err)
159 goto err_req_irq;
160
161 bcma_chipco_gpio_intmask(cc, ~0, 0);
162 bcma_cc_set32(cc, BCMA_CC_IRQMASK, BCMA_CC_IRQ_GPIO);
163
164 return 0;
165
166err_req_irq:
167 for (gpio = 0; gpio < chip->ngpio; gpio++) {
168 int irq = irq_find_mapping(cc->irq_domain, gpio);
169
170 irq_dispose_mapping(irq);
171 }
172 irq_domain_remove(cc->irq_domain);
173err_irq_domain:
174 return err;
175}
176
177static void bcma_gpio_irq_domain_exit(struct bcma_drv_cc *cc)
178{
179 struct gpio_chip *chip = &cc->gpio;
180 int gpio;
181
182 if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC)
183 return;
184
185 bcma_cc_mask32(cc, BCMA_CC_IRQMASK, ~BCMA_CC_IRQ_GPIO);
186 free_irq(bcma_core_irq(cc->core), cc);
187 for (gpio = 0; gpio < chip->ngpio; gpio++) {
188 int irq = irq_find_mapping(cc->irq_domain, gpio);
189
190 irq_dispose_mapping(irq);
191 }
192 irq_domain_remove(cc->irq_domain);
193}
194#else
195static int bcma_gpio_irq_domain_init(struct bcma_drv_cc *cc)
196{
197 return 0;
198}
199
200static void bcma_gpio_irq_domain_exit(struct bcma_drv_cc *cc)
201{
202}
203#endif
204
205int bcma_gpio_init(struct bcma_drv_cc *cc)
206{
207 struct gpio_chip *chip = &cc->gpio;
208 int err;
209
210 chip->label = "bcma_gpio";
211 chip->owner = THIS_MODULE;
212 chip->request = bcma_gpio_request;
213 chip->free = bcma_gpio_free;
214 chip->get = bcma_gpio_get_value;
215 chip->set = bcma_gpio_set_value;
216 chip->direction_input = bcma_gpio_direction_input;
217 chip->direction_output = bcma_gpio_direction_output;
218#if IS_BUILTIN(CONFIG_BCMA_HOST_SOC)
219 chip->to_irq = bcma_gpio_to_irq;
220#endif
221 switch (cc->core->bus->chipinfo.id) {
222 case BCMA_CHIP_ID_BCM5357:
223 chip->ngpio = 32;
224 break;
225 default:
226 chip->ngpio = 16;
227 }
228
229 /* There is just one SoC in one device and its GPIO addresses should be
230 * deterministic to address them more easily. The other buses could get
231 * a random base number. */
232 if (cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
233 chip->base = 0;
234 else
235 chip->base = -1;
236
237 err = bcma_gpio_irq_domain_init(cc);
238 if (err)
239 return err;
240
241 err = gpiochip_add(chip);
242 if (err) {
243 bcma_gpio_irq_domain_exit(cc);
244 return err;
245 }
246
247 return 0;
248}
249
250int bcma_gpio_unregister(struct bcma_drv_cc *cc)
251{
252 bcma_gpio_irq_domain_exit(cc);
253 return gpiochip_remove(&cc->gpio);
254}