1
2
3
4
5
6
7
8
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 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
64 bcma_chipco_gpio_pulldown(cc, 1 << gpio, 0);
65
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
76 bcma_chipco_gpio_pullup(cc, 1 << gpio, 0);
77}
78
79#if IS_BUILTIN(CONFIG_BCM47XX) || IS_BUILTIN(CONFIG_ARCH_BCM_5301X)
80
81static void bcma_gpio_irq_unmask(struct irq_data *d)
82{
83 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
84 struct bcma_drv_cc *cc = bcma_gpio_get_cc(gc);
85 int gpio = irqd_to_hwirq(d);
86 u32 val = bcma_chipco_gpio_in(cc, BIT(gpio));
87
88 bcma_chipco_gpio_polarity(cc, BIT(gpio), val);
89 bcma_chipco_gpio_intmask(cc, BIT(gpio), BIT(gpio));
90}
91
92static void bcma_gpio_irq_mask(struct irq_data *d)
93{
94 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
95 struct bcma_drv_cc *cc = bcma_gpio_get_cc(gc);
96 int gpio = irqd_to_hwirq(d);
97
98 bcma_chipco_gpio_intmask(cc, BIT(gpio), 0);
99}
100
101static struct irq_chip bcma_gpio_irq_chip = {
102 .name = "BCMA-GPIO",
103 .irq_mask = bcma_gpio_irq_mask,
104 .irq_unmask = bcma_gpio_irq_unmask,
105};
106
107static irqreturn_t bcma_gpio_irq_handler(int irq, void *dev_id)
108{
109 struct bcma_drv_cc *cc = dev_id;
110 struct gpio_chip *gc = &cc->gpio;
111 u32 val = bcma_cc_read32(cc, BCMA_CC_GPIOIN);
112 u32 mask = bcma_cc_read32(cc, BCMA_CC_GPIOIRQ);
113 u32 pol = bcma_cc_read32(cc, BCMA_CC_GPIOPOL);
114 unsigned long irqs = (val ^ pol) & mask;
115 int gpio;
116
117 if (!irqs)
118 return IRQ_NONE;
119
120 for_each_set_bit(gpio, &irqs, gc->ngpio)
121 generic_handle_irq(irq_find_mapping(gc->irqdomain, gpio));
122 bcma_chipco_gpio_polarity(cc, irqs, val & irqs);
123
124 return IRQ_HANDLED;
125}
126
127static int bcma_gpio_irq_init(struct bcma_drv_cc *cc)
128{
129 struct gpio_chip *chip = &cc->gpio;
130 int hwirq, err;
131
132 if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC)
133 return 0;
134
135 hwirq = bcma_core_irq(cc->core, 0);
136 err = request_irq(hwirq, bcma_gpio_irq_handler, IRQF_SHARED, "gpio",
137 cc);
138 if (err)
139 return err;
140
141 bcma_chipco_gpio_intmask(cc, ~0, 0);
142 bcma_cc_set32(cc, BCMA_CC_IRQMASK, BCMA_CC_IRQ_GPIO);
143
144 err = gpiochip_irqchip_add(chip,
145 &bcma_gpio_irq_chip,
146 0,
147 handle_simple_irq,
148 IRQ_TYPE_NONE);
149 if (err) {
150 free_irq(hwirq, cc);
151 return err;
152 }
153
154 return 0;
155}
156
157static void bcma_gpio_irq_exit(struct bcma_drv_cc *cc)
158{
159 if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC)
160 return;
161
162 bcma_cc_mask32(cc, BCMA_CC_IRQMASK, ~BCMA_CC_IRQ_GPIO);
163 free_irq(bcma_core_irq(cc->core, 0), cc);
164}
165#else
166static int bcma_gpio_irq_init(struct bcma_drv_cc *cc)
167{
168 return 0;
169}
170
171static void bcma_gpio_irq_exit(struct bcma_drv_cc *cc)
172{
173}
174#endif
175
176int bcma_gpio_init(struct bcma_drv_cc *cc)
177{
178 struct bcma_bus *bus = cc->core->bus;
179 struct gpio_chip *chip = &cc->gpio;
180 int err;
181
182 chip->label = "bcma_gpio";
183 chip->owner = THIS_MODULE;
184 chip->request = bcma_gpio_request;
185 chip->free = bcma_gpio_free;
186 chip->get = bcma_gpio_get_value;
187 chip->set = bcma_gpio_set_value;
188 chip->direction_input = bcma_gpio_direction_input;
189 chip->direction_output = bcma_gpio_direction_output;
190 chip->owner = THIS_MODULE;
191#if 0
192 chip->parent = bcma_bus_get_host_dev(bus);
193#endif
194#if IS_BUILTIN(CONFIG_OF)
195 if (cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
196 chip->of_node = cc->core->dev.of_node;
197#endif
198 switch (bus->chipinfo.id) {
199 case BCMA_CHIP_ID_BCM4707:
200 case BCMA_CHIP_ID_BCM5357:
201 case BCMA_CHIP_ID_BCM53572:
202 case BCMA_CHIP_ID_BCM47094:
203 chip->ngpio = 32;
204 break;
205 default:
206 chip->ngpio = 16;
207 }
208
209
210
211
212
213
214
215
216 if (IS_BUILTIN(CONFIG_BCM47XX) ||
217 cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
218 chip->base = bus->num * BCMA_GPIO_MAX_PINS;
219 else
220 chip->base = -1;
221
222 err = gpiochip_add(chip);
223 if (err)
224 return err;
225
226 err = bcma_gpio_irq_init(cc);
227 if (err) {
228 gpiochip_remove(chip);
229 return err;
230 }
231
232 return 0;
233}
234
235int bcma_gpio_unregister(struct bcma_drv_cc *cc)
236{
237 bcma_gpio_irq_exit(cc);
238 gpiochip_remove(&cc->gpio);
239 return 0;
240}
241