2 * Sonics Silicon Backplane
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 "ssb_private.h"
13 #include <linux/gpio/driver.h>
14 #include <linux/irq.h>
15 #include <linux/interrupt.h>
16 #include <linux/irqdomain.h>
17 #include <linux/export.h>
18 #include <linux/ssb/ssb.h>
21 /**************************************************
23 **************************************************/
25 #if IS_ENABLED(CONFIG_SSB_EMBEDDED)
26 static int ssb_gpio_to_irq(struct gpio_chip
*chip
, unsigned int gpio
)
28 struct ssb_bus
*bus
= gpiochip_get_data(chip
);
30 if (bus
->bustype
== SSB_BUSTYPE_SSB
)
31 return irq_find_mapping(bus
->irq_domain
, gpio
);
37 /**************************************************
39 **************************************************/
41 static int ssb_gpio_chipco_get_value(struct gpio_chip
*chip
, unsigned int gpio
)
43 struct ssb_bus
*bus
= gpiochip_get_data(chip
);
45 return !!ssb_chipco_gpio_in(&bus
->chipco
, 1 << gpio
);
48 static void ssb_gpio_chipco_set_value(struct gpio_chip
*chip
, unsigned int gpio
,
51 struct ssb_bus
*bus
= gpiochip_get_data(chip
);
53 ssb_chipco_gpio_out(&bus
->chipco
, 1 << gpio
, value
? 1 << gpio
: 0);
56 static int ssb_gpio_chipco_direction_input(struct gpio_chip
*chip
,
59 struct ssb_bus
*bus
= gpiochip_get_data(chip
);
61 ssb_chipco_gpio_outen(&bus
->chipco
, 1 << gpio
, 0);
65 static int ssb_gpio_chipco_direction_output(struct gpio_chip
*chip
,
66 unsigned int gpio
, int value
)
68 struct ssb_bus
*bus
= gpiochip_get_data(chip
);
70 ssb_chipco_gpio_outen(&bus
->chipco
, 1 << gpio
, 1 << gpio
);
71 ssb_chipco_gpio_out(&bus
->chipco
, 1 << gpio
, value
? 1 << gpio
: 0);
75 static int ssb_gpio_chipco_request(struct gpio_chip
*chip
, unsigned int gpio
)
77 struct ssb_bus
*bus
= gpiochip_get_data(chip
);
79 ssb_chipco_gpio_control(&bus
->chipco
, 1 << gpio
, 0);
81 ssb_chipco_gpio_pulldown(&bus
->chipco
, 1 << gpio
, 0);
83 ssb_chipco_gpio_pullup(&bus
->chipco
, 1 << gpio
, 1 << gpio
);
88 static void ssb_gpio_chipco_free(struct gpio_chip
*chip
, unsigned int gpio
)
90 struct ssb_bus
*bus
= gpiochip_get_data(chip
);
93 ssb_chipco_gpio_pullup(&bus
->chipco
, 1 << gpio
, 0);
96 #if IS_ENABLED(CONFIG_SSB_EMBEDDED)
97 static void ssb_gpio_irq_chipco_mask(struct irq_data
*d
)
99 struct ssb_bus
*bus
= irq_data_get_irq_chip_data(d
);
100 int gpio
= irqd_to_hwirq(d
);
102 ssb_chipco_gpio_intmask(&bus
->chipco
, BIT(gpio
), 0);
105 static void ssb_gpio_irq_chipco_unmask(struct irq_data
*d
)
107 struct ssb_bus
*bus
= irq_data_get_irq_chip_data(d
);
108 int gpio
= irqd_to_hwirq(d
);
109 u32 val
= ssb_chipco_gpio_in(&bus
->chipco
, BIT(gpio
));
111 ssb_chipco_gpio_polarity(&bus
->chipco
, BIT(gpio
), val
);
112 ssb_chipco_gpio_intmask(&bus
->chipco
, BIT(gpio
), BIT(gpio
));
115 static struct irq_chip ssb_gpio_irq_chipco_chip
= {
116 .name
= "SSB-GPIO-CC",
117 .irq_mask
= ssb_gpio_irq_chipco_mask
,
118 .irq_unmask
= ssb_gpio_irq_chipco_unmask
,
121 static irqreturn_t
ssb_gpio_irq_chipco_handler(int irq
, void *dev_id
)
123 struct ssb_bus
*bus
= dev_id
;
124 struct ssb_chipcommon
*chipco
= &bus
->chipco
;
125 u32 val
= chipco_read32(chipco
, SSB_CHIPCO_GPIOIN
);
126 u32 mask
= chipco_read32(chipco
, SSB_CHIPCO_GPIOIRQ
);
127 u32 pol
= chipco_read32(chipco
, SSB_CHIPCO_GPIOPOL
);
128 unsigned long irqs
= (val
^ pol
) & mask
;
134 for_each_set_bit(gpio
, &irqs
, bus
->gpio
.ngpio
)
135 generic_handle_domain_irq_safe(bus
->irq_domain
, gpio
);
137 ssb_chipco_gpio_polarity(chipco
, irqs
, val
& irqs
);
142 static int ssb_gpio_irq_chipco_domain_init(struct ssb_bus
*bus
)
144 struct ssb_chipcommon
*chipco
= &bus
->chipco
;
145 struct gpio_chip
*chip
= &bus
->gpio
;
146 int gpio
, hwirq
, err
;
148 if (bus
->bustype
!= SSB_BUSTYPE_SSB
)
151 bus
->irq_domain
= irq_domain_add_linear(NULL
, chip
->ngpio
,
152 &irq_domain_simple_ops
, chipco
);
153 if (!bus
->irq_domain
) {
157 for (gpio
= 0; gpio
< chip
->ngpio
; gpio
++) {
158 int irq
= irq_create_mapping(bus
->irq_domain
, gpio
);
160 irq_set_chip_data(irq
, bus
);
161 irq_set_chip_and_handler(irq
, &ssb_gpio_irq_chipco_chip
,
165 hwirq
= ssb_mips_irq(bus
->chipco
.dev
) + 2;
166 err
= request_irq(hwirq
, ssb_gpio_irq_chipco_handler
, IRQF_SHARED
,
171 ssb_chipco_gpio_intmask(&bus
->chipco
, ~0, 0);
172 chipco_set32(chipco
, SSB_CHIPCO_IRQMASK
, SSB_CHIPCO_IRQ_GPIO
);
177 for (gpio
= 0; gpio
< chip
->ngpio
; gpio
++) {
178 int irq
= irq_find_mapping(bus
->irq_domain
, gpio
);
180 irq_dispose_mapping(irq
);
182 irq_domain_remove(bus
->irq_domain
);
187 static void ssb_gpio_irq_chipco_domain_exit(struct ssb_bus
*bus
)
189 struct ssb_chipcommon
*chipco
= &bus
->chipco
;
190 struct gpio_chip
*chip
= &bus
->gpio
;
193 if (bus
->bustype
!= SSB_BUSTYPE_SSB
)
196 chipco_mask32(chipco
, SSB_CHIPCO_IRQMASK
, ~SSB_CHIPCO_IRQ_GPIO
);
197 free_irq(ssb_mips_irq(bus
->chipco
.dev
) + 2, chipco
);
198 for (gpio
= 0; gpio
< chip
->ngpio
; gpio
++) {
199 int irq
= irq_find_mapping(bus
->irq_domain
, gpio
);
201 irq_dispose_mapping(irq
);
203 irq_domain_remove(bus
->irq_domain
);
206 static int ssb_gpio_irq_chipco_domain_init(struct ssb_bus
*bus
)
211 static void ssb_gpio_irq_chipco_domain_exit(struct ssb_bus
*bus
)
216 static int ssb_gpio_chipco_init(struct ssb_bus
*bus
)
218 struct gpio_chip
*chip
= &bus
->gpio
;
221 chip
->label
= "ssb_chipco_gpio";
222 chip
->owner
= THIS_MODULE
;
223 chip
->request
= ssb_gpio_chipco_request
;
224 chip
->free
= ssb_gpio_chipco_free
;
225 chip
->get
= ssb_gpio_chipco_get_value
;
226 chip
->set
= ssb_gpio_chipco_set_value
;
227 chip
->direction_input
= ssb_gpio_chipco_direction_input
;
228 chip
->direction_output
= ssb_gpio_chipco_direction_output
;
229 #if IS_ENABLED(CONFIG_SSB_EMBEDDED)
230 chip
->to_irq
= ssb_gpio_to_irq
;
233 /* There is just one SoC in one device and its GPIO addresses should be
234 * deterministic to address them more easily. The other buses could get
235 * a random base number.
237 if (bus
->bustype
== SSB_BUSTYPE_SSB
)
242 err
= ssb_gpio_irq_chipco_domain_init(bus
);
246 err
= gpiochip_add_data(chip
, bus
);
248 ssb_gpio_irq_chipco_domain_exit(bus
);
255 /**************************************************
257 **************************************************/
259 #ifdef CONFIG_SSB_DRIVER_EXTIF
261 static int ssb_gpio_extif_get_value(struct gpio_chip
*chip
, unsigned int gpio
)
263 struct ssb_bus
*bus
= gpiochip_get_data(chip
);
265 return !!ssb_extif_gpio_in(&bus
->extif
, 1 << gpio
);
268 static void ssb_gpio_extif_set_value(struct gpio_chip
*chip
, unsigned int gpio
,
271 struct ssb_bus
*bus
= gpiochip_get_data(chip
);
273 ssb_extif_gpio_out(&bus
->extif
, 1 << gpio
, value
? 1 << gpio
: 0);
276 static int ssb_gpio_extif_direction_input(struct gpio_chip
*chip
,
279 struct ssb_bus
*bus
= gpiochip_get_data(chip
);
281 ssb_extif_gpio_outen(&bus
->extif
, 1 << gpio
, 0);
285 static int ssb_gpio_extif_direction_output(struct gpio_chip
*chip
,
286 unsigned int gpio
, int value
)
288 struct ssb_bus
*bus
= gpiochip_get_data(chip
);
290 ssb_extif_gpio_outen(&bus
->extif
, 1 << gpio
, 1 << gpio
);
291 ssb_extif_gpio_out(&bus
->extif
, 1 << gpio
, value
? 1 << gpio
: 0);
295 #if IS_ENABLED(CONFIG_SSB_EMBEDDED)
296 static void ssb_gpio_irq_extif_mask(struct irq_data
*d
)
298 struct ssb_bus
*bus
= irq_data_get_irq_chip_data(d
);
299 int gpio
= irqd_to_hwirq(d
);
301 ssb_extif_gpio_intmask(&bus
->extif
, BIT(gpio
), 0);
304 static void ssb_gpio_irq_extif_unmask(struct irq_data
*d
)
306 struct ssb_bus
*bus
= irq_data_get_irq_chip_data(d
);
307 int gpio
= irqd_to_hwirq(d
);
308 u32 val
= ssb_extif_gpio_in(&bus
->extif
, BIT(gpio
));
310 ssb_extif_gpio_polarity(&bus
->extif
, BIT(gpio
), val
);
311 ssb_extif_gpio_intmask(&bus
->extif
, BIT(gpio
), BIT(gpio
));
314 static struct irq_chip ssb_gpio_irq_extif_chip
= {
315 .name
= "SSB-GPIO-EXTIF",
316 .irq_mask
= ssb_gpio_irq_extif_mask
,
317 .irq_unmask
= ssb_gpio_irq_extif_unmask
,
320 static irqreturn_t
ssb_gpio_irq_extif_handler(int irq
, void *dev_id
)
322 struct ssb_bus
*bus
= dev_id
;
323 struct ssb_extif
*extif
= &bus
->extif
;
324 u32 val
= ssb_read32(extif
->dev
, SSB_EXTIF_GPIO_IN
);
325 u32 mask
= ssb_read32(extif
->dev
, SSB_EXTIF_GPIO_INTMASK
);
326 u32 pol
= ssb_read32(extif
->dev
, SSB_EXTIF_GPIO_INTPOL
);
327 unsigned long irqs
= (val
^ pol
) & mask
;
333 for_each_set_bit(gpio
, &irqs
, bus
->gpio
.ngpio
)
334 generic_handle_domain_irq_safe(bus
->irq_domain
, gpio
);
336 ssb_extif_gpio_polarity(extif
, irqs
, val
& irqs
);
341 static int ssb_gpio_irq_extif_domain_init(struct ssb_bus
*bus
)
343 struct ssb_extif
*extif
= &bus
->extif
;
344 struct gpio_chip
*chip
= &bus
->gpio
;
345 int gpio
, hwirq
, err
;
347 if (bus
->bustype
!= SSB_BUSTYPE_SSB
)
350 bus
->irq_domain
= irq_domain_add_linear(NULL
, chip
->ngpio
,
351 &irq_domain_simple_ops
, extif
);
352 if (!bus
->irq_domain
) {
356 for (gpio
= 0; gpio
< chip
->ngpio
; gpio
++) {
357 int irq
= irq_create_mapping(bus
->irq_domain
, gpio
);
359 irq_set_chip_data(irq
, bus
);
360 irq_set_chip_and_handler(irq
, &ssb_gpio_irq_extif_chip
,
364 hwirq
= ssb_mips_irq(bus
->extif
.dev
) + 2;
365 err
= request_irq(hwirq
, ssb_gpio_irq_extif_handler
, IRQF_SHARED
,
370 ssb_extif_gpio_intmask(&bus
->extif
, ~0, 0);
375 for (gpio
= 0; gpio
< chip
->ngpio
; gpio
++) {
376 int irq
= irq_find_mapping(bus
->irq_domain
, gpio
);
378 irq_dispose_mapping(irq
);
380 irq_domain_remove(bus
->irq_domain
);
385 static void ssb_gpio_irq_extif_domain_exit(struct ssb_bus
*bus
)
387 struct ssb_extif
*extif
= &bus
->extif
;
388 struct gpio_chip
*chip
= &bus
->gpio
;
391 if (bus
->bustype
!= SSB_BUSTYPE_SSB
)
394 free_irq(ssb_mips_irq(bus
->extif
.dev
) + 2, extif
);
395 for (gpio
= 0; gpio
< chip
->ngpio
; gpio
++) {
396 int irq
= irq_find_mapping(bus
->irq_domain
, gpio
);
398 irq_dispose_mapping(irq
);
400 irq_domain_remove(bus
->irq_domain
);
403 static int ssb_gpio_irq_extif_domain_init(struct ssb_bus
*bus
)
408 static void ssb_gpio_irq_extif_domain_exit(struct ssb_bus
*bus
)
413 static int ssb_gpio_extif_init(struct ssb_bus
*bus
)
415 struct gpio_chip
*chip
= &bus
->gpio
;
418 chip
->label
= "ssb_extif_gpio";
419 chip
->owner
= THIS_MODULE
;
420 chip
->get
= ssb_gpio_extif_get_value
;
421 chip
->set
= ssb_gpio_extif_set_value
;
422 chip
->direction_input
= ssb_gpio_extif_direction_input
;
423 chip
->direction_output
= ssb_gpio_extif_direction_output
;
424 #if IS_ENABLED(CONFIG_SSB_EMBEDDED)
425 chip
->to_irq
= ssb_gpio_to_irq
;
428 /* There is just one SoC in one device and its GPIO addresses should be
429 * deterministic to address them more easily. The other buses could get
430 * a random base number.
432 if (bus
->bustype
== SSB_BUSTYPE_SSB
)
437 err
= ssb_gpio_irq_extif_domain_init(bus
);
441 err
= gpiochip_add_data(chip
, bus
);
443 ssb_gpio_irq_extif_domain_exit(bus
);
451 static int ssb_gpio_extif_init(struct ssb_bus
*bus
)
457 /**************************************************
459 **************************************************/
461 int ssb_gpio_init(struct ssb_bus
*bus
)
463 if (ssb_chipco_available(&bus
->chipco
))
464 return ssb_gpio_chipco_init(bus
);
465 else if (ssb_extif_available(&bus
->extif
))
466 return ssb_gpio_extif_init(bus
);
470 int ssb_gpio_unregister(struct ssb_bus
*bus
)
472 if (ssb_chipco_available(&bus
->chipco
) ||
473 ssb_extif_available(&bus
->extif
)) {
474 gpiochip_remove(&bus
->gpio
);