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 <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/ssb/ssb.h>
18 #include "ssb_private.h"
21 /**************************************************
23 **************************************************/
25 static struct ssb_bus
*ssb_gpio_get_bus(struct gpio_chip
*chip
)
27 return container_of(chip
, struct ssb_bus
, gpio
);
30 #if IS_ENABLED(CONFIG_SSB_EMBEDDED)
31 static int ssb_gpio_to_irq(struct gpio_chip
*chip
, unsigned gpio
)
33 struct ssb_bus
*bus
= ssb_gpio_get_bus(chip
);
35 if (bus
->bustype
== SSB_BUSTYPE_SSB
)
36 return irq_find_mapping(bus
->irq_domain
, gpio
);
42 /**************************************************
44 **************************************************/
46 static int ssb_gpio_chipco_get_value(struct gpio_chip
*chip
, unsigned gpio
)
48 struct ssb_bus
*bus
= ssb_gpio_get_bus(chip
);
50 return !!ssb_chipco_gpio_in(&bus
->chipco
, 1 << gpio
);
53 static void ssb_gpio_chipco_set_value(struct gpio_chip
*chip
, unsigned gpio
,
56 struct ssb_bus
*bus
= ssb_gpio_get_bus(chip
);
58 ssb_chipco_gpio_out(&bus
->chipco
, 1 << gpio
, value
? 1 << gpio
: 0);
61 static int ssb_gpio_chipco_direction_input(struct gpio_chip
*chip
,
64 struct ssb_bus
*bus
= ssb_gpio_get_bus(chip
);
66 ssb_chipco_gpio_outen(&bus
->chipco
, 1 << gpio
, 0);
70 static int ssb_gpio_chipco_direction_output(struct gpio_chip
*chip
,
71 unsigned gpio
, int value
)
73 struct ssb_bus
*bus
= ssb_gpio_get_bus(chip
);
75 ssb_chipco_gpio_outen(&bus
->chipco
, 1 << gpio
, 1 << gpio
);
76 ssb_chipco_gpio_out(&bus
->chipco
, 1 << gpio
, value
? 1 << gpio
: 0);
80 static int ssb_gpio_chipco_request(struct gpio_chip
*chip
, unsigned gpio
)
82 struct ssb_bus
*bus
= ssb_gpio_get_bus(chip
);
84 ssb_chipco_gpio_control(&bus
->chipco
, 1 << gpio
, 0);
86 ssb_chipco_gpio_pulldown(&bus
->chipco
, 1 << gpio
, 0);
88 ssb_chipco_gpio_pullup(&bus
->chipco
, 1 << gpio
, 1 << gpio
);
93 static void ssb_gpio_chipco_free(struct gpio_chip
*chip
, unsigned gpio
)
95 struct ssb_bus
*bus
= ssb_gpio_get_bus(chip
);
98 ssb_chipco_gpio_pullup(&bus
->chipco
, 1 << gpio
, 0);
101 #if IS_ENABLED(CONFIG_SSB_EMBEDDED)
102 static void ssb_gpio_irq_chipco_mask(struct irq_data
*d
)
104 struct ssb_bus
*bus
= irq_data_get_irq_chip_data(d
);
105 int gpio
= irqd_to_hwirq(d
);
107 ssb_chipco_gpio_intmask(&bus
->chipco
, BIT(gpio
), 0);
110 static void ssb_gpio_irq_chipco_unmask(struct irq_data
*d
)
112 struct ssb_bus
*bus
= irq_data_get_irq_chip_data(d
);
113 int gpio
= irqd_to_hwirq(d
);
114 u32 val
= ssb_chipco_gpio_in(&bus
->chipco
, BIT(gpio
));
116 ssb_chipco_gpio_polarity(&bus
->chipco
, BIT(gpio
), val
);
117 ssb_chipco_gpio_intmask(&bus
->chipco
, BIT(gpio
), BIT(gpio
));
120 static struct irq_chip ssb_gpio_irq_chipco_chip
= {
121 .name
= "SSB-GPIO-CC",
122 .irq_mask
= ssb_gpio_irq_chipco_mask
,
123 .irq_unmask
= ssb_gpio_irq_chipco_unmask
,
126 static irqreturn_t
ssb_gpio_irq_chipco_handler(int irq
, void *dev_id
)
128 struct ssb_bus
*bus
= dev_id
;
129 struct ssb_chipcommon
*chipco
= &bus
->chipco
;
130 u32 val
= chipco_read32(chipco
, SSB_CHIPCO_GPIOIN
);
131 u32 mask
= chipco_read32(chipco
, SSB_CHIPCO_GPIOIRQ
);
132 u32 pol
= chipco_read32(chipco
, SSB_CHIPCO_GPIOPOL
);
133 unsigned long irqs
= (val
^ pol
) & mask
;
139 for_each_set_bit(gpio
, &irqs
, bus
->gpio
.ngpio
)
140 generic_handle_irq(ssb_gpio_to_irq(&bus
->gpio
, gpio
));
141 ssb_chipco_gpio_polarity(chipco
, irqs
, val
& irqs
);
146 static int ssb_gpio_irq_chipco_domain_init(struct ssb_bus
*bus
)
148 struct ssb_chipcommon
*chipco
= &bus
->chipco
;
149 struct gpio_chip
*chip
= &bus
->gpio
;
150 int gpio
, hwirq
, err
;
152 if (bus
->bustype
!= SSB_BUSTYPE_SSB
)
155 bus
->irq_domain
= irq_domain_add_linear(NULL
, chip
->ngpio
,
156 &irq_domain_simple_ops
, chipco
);
157 if (!bus
->irq_domain
) {
161 for (gpio
= 0; gpio
< chip
->ngpio
; gpio
++) {
162 int irq
= irq_create_mapping(bus
->irq_domain
, gpio
);
164 irq_set_chip_data(irq
, bus
);
165 irq_set_chip_and_handler(irq
, &ssb_gpio_irq_chipco_chip
,
169 hwirq
= ssb_mips_irq(bus
->chipco
.dev
) + 2;
170 err
= request_irq(hwirq
, ssb_gpio_irq_chipco_handler
, IRQF_SHARED
,
175 ssb_chipco_gpio_intmask(&bus
->chipco
, ~0, 0);
176 chipco_set32(chipco
, SSB_CHIPCO_IRQMASK
, SSB_CHIPCO_IRQ_GPIO
);
181 for (gpio
= 0; gpio
< chip
->ngpio
; gpio
++) {
182 int irq
= irq_find_mapping(bus
->irq_domain
, gpio
);
184 irq_dispose_mapping(irq
);
186 irq_domain_remove(bus
->irq_domain
);
191 static void ssb_gpio_irq_chipco_domain_exit(struct ssb_bus
*bus
)
193 struct ssb_chipcommon
*chipco
= &bus
->chipco
;
194 struct gpio_chip
*chip
= &bus
->gpio
;
197 if (bus
->bustype
!= SSB_BUSTYPE_SSB
)
200 chipco_mask32(chipco
, SSB_CHIPCO_IRQMASK
, ~SSB_CHIPCO_IRQ_GPIO
);
201 free_irq(ssb_mips_irq(bus
->chipco
.dev
) + 2, chipco
);
202 for (gpio
= 0; gpio
< chip
->ngpio
; gpio
++) {
203 int irq
= irq_find_mapping(bus
->irq_domain
, gpio
);
205 irq_dispose_mapping(irq
);
207 irq_domain_remove(bus
->irq_domain
);
210 static int ssb_gpio_irq_chipco_domain_init(struct ssb_bus
*bus
)
215 static void ssb_gpio_irq_chipco_domain_exit(struct ssb_bus
*bus
)
220 static int ssb_gpio_chipco_init(struct ssb_bus
*bus
)
222 struct gpio_chip
*chip
= &bus
->gpio
;
225 chip
->label
= "ssb_chipco_gpio";
226 chip
->owner
= THIS_MODULE
;
227 chip
->request
= ssb_gpio_chipco_request
;
228 chip
->free
= ssb_gpio_chipco_free
;
229 chip
->get
= ssb_gpio_chipco_get_value
;
230 chip
->set
= ssb_gpio_chipco_set_value
;
231 chip
->direction_input
= ssb_gpio_chipco_direction_input
;
232 chip
->direction_output
= ssb_gpio_chipco_direction_output
;
233 #if IS_ENABLED(CONFIG_SSB_EMBEDDED)
234 chip
->to_irq
= ssb_gpio_to_irq
;
237 /* There is just one SoC in one device and its GPIO addresses should be
238 * deterministic to address them more easily. The other buses could get
239 * a random base number. */
240 if (bus
->bustype
== SSB_BUSTYPE_SSB
)
245 err
= ssb_gpio_irq_chipco_domain_init(bus
);
249 err
= gpiochip_add(chip
);
251 ssb_gpio_irq_chipco_domain_exit(bus
);
258 /**************************************************
260 **************************************************/
262 #ifdef CONFIG_SSB_DRIVER_EXTIF
264 static int ssb_gpio_extif_get_value(struct gpio_chip
*chip
, unsigned gpio
)
266 struct ssb_bus
*bus
= ssb_gpio_get_bus(chip
);
268 return !!ssb_extif_gpio_in(&bus
->extif
, 1 << gpio
);
271 static void ssb_gpio_extif_set_value(struct gpio_chip
*chip
, unsigned gpio
,
274 struct ssb_bus
*bus
= ssb_gpio_get_bus(chip
);
276 ssb_extif_gpio_out(&bus
->extif
, 1 << gpio
, value
? 1 << gpio
: 0);
279 static int ssb_gpio_extif_direction_input(struct gpio_chip
*chip
,
282 struct ssb_bus
*bus
= ssb_gpio_get_bus(chip
);
284 ssb_extif_gpio_outen(&bus
->extif
, 1 << gpio
, 0);
288 static int ssb_gpio_extif_direction_output(struct gpio_chip
*chip
,
289 unsigned gpio
, int value
)
291 struct ssb_bus
*bus
= ssb_gpio_get_bus(chip
);
293 ssb_extif_gpio_outen(&bus
->extif
, 1 << gpio
, 1 << gpio
);
294 ssb_extif_gpio_out(&bus
->extif
, 1 << gpio
, value
? 1 << gpio
: 0);
298 #if IS_ENABLED(CONFIG_SSB_EMBEDDED)
299 static void ssb_gpio_irq_extif_mask(struct irq_data
*d
)
301 struct ssb_bus
*bus
= irq_data_get_irq_chip_data(d
);
302 int gpio
= irqd_to_hwirq(d
);
304 ssb_extif_gpio_intmask(&bus
->extif
, BIT(gpio
), 0);
307 static void ssb_gpio_irq_extif_unmask(struct irq_data
*d
)
309 struct ssb_bus
*bus
= irq_data_get_irq_chip_data(d
);
310 int gpio
= irqd_to_hwirq(d
);
311 u32 val
= ssb_extif_gpio_in(&bus
->extif
, BIT(gpio
));
313 ssb_extif_gpio_polarity(&bus
->extif
, BIT(gpio
), val
);
314 ssb_extif_gpio_intmask(&bus
->extif
, BIT(gpio
), BIT(gpio
));
317 static struct irq_chip ssb_gpio_irq_extif_chip
= {
318 .name
= "SSB-GPIO-EXTIF",
319 .irq_mask
= ssb_gpio_irq_extif_mask
,
320 .irq_unmask
= ssb_gpio_irq_extif_unmask
,
323 static irqreturn_t
ssb_gpio_irq_extif_handler(int irq
, void *dev_id
)
325 struct ssb_bus
*bus
= dev_id
;
326 struct ssb_extif
*extif
= &bus
->extif
;
327 u32 val
= ssb_read32(extif
->dev
, SSB_EXTIF_GPIO_IN
);
328 u32 mask
= ssb_read32(extif
->dev
, SSB_EXTIF_GPIO_INTMASK
);
329 u32 pol
= ssb_read32(extif
->dev
, SSB_EXTIF_GPIO_INTPOL
);
330 unsigned long irqs
= (val
^ pol
) & mask
;
336 for_each_set_bit(gpio
, &irqs
, bus
->gpio
.ngpio
)
337 generic_handle_irq(ssb_gpio_to_irq(&bus
->gpio
, gpio
));
338 ssb_extif_gpio_polarity(extif
, irqs
, val
& irqs
);
343 static int ssb_gpio_irq_extif_domain_init(struct ssb_bus
*bus
)
345 struct ssb_extif
*extif
= &bus
->extif
;
346 struct gpio_chip
*chip
= &bus
->gpio
;
347 int gpio
, hwirq
, err
;
349 if (bus
->bustype
!= SSB_BUSTYPE_SSB
)
352 bus
->irq_domain
= irq_domain_add_linear(NULL
, chip
->ngpio
,
353 &irq_domain_simple_ops
, extif
);
354 if (!bus
->irq_domain
) {
358 for (gpio
= 0; gpio
< chip
->ngpio
; gpio
++) {
359 int irq
= irq_create_mapping(bus
->irq_domain
, gpio
);
361 irq_set_chip_data(irq
, bus
);
362 irq_set_chip_and_handler(irq
, &ssb_gpio_irq_extif_chip
,
366 hwirq
= ssb_mips_irq(bus
->extif
.dev
) + 2;
367 err
= request_irq(hwirq
, ssb_gpio_irq_extif_handler
, IRQF_SHARED
,
372 ssb_extif_gpio_intmask(&bus
->extif
, ~0, 0);
377 for (gpio
= 0; gpio
< chip
->ngpio
; gpio
++) {
378 int irq
= irq_find_mapping(bus
->irq_domain
, gpio
);
380 irq_dispose_mapping(irq
);
382 irq_domain_remove(bus
->irq_domain
);
387 static void ssb_gpio_irq_extif_domain_exit(struct ssb_bus
*bus
)
389 struct ssb_extif
*extif
= &bus
->extif
;
390 struct gpio_chip
*chip
= &bus
->gpio
;
393 if (bus
->bustype
!= SSB_BUSTYPE_SSB
)
396 free_irq(ssb_mips_irq(bus
->extif
.dev
) + 2, extif
);
397 for (gpio
= 0; gpio
< chip
->ngpio
; gpio
++) {
398 int irq
= irq_find_mapping(bus
->irq_domain
, gpio
);
400 irq_dispose_mapping(irq
);
402 irq_domain_remove(bus
->irq_domain
);
405 static int ssb_gpio_irq_extif_domain_init(struct ssb_bus
*bus
)
410 static void ssb_gpio_irq_extif_domain_exit(struct ssb_bus
*bus
)
415 static int ssb_gpio_extif_init(struct ssb_bus
*bus
)
417 struct gpio_chip
*chip
= &bus
->gpio
;
420 chip
->label
= "ssb_extif_gpio";
421 chip
->owner
= THIS_MODULE
;
422 chip
->get
= ssb_gpio_extif_get_value
;
423 chip
->set
= ssb_gpio_extif_set_value
;
424 chip
->direction_input
= ssb_gpio_extif_direction_input
;
425 chip
->direction_output
= ssb_gpio_extif_direction_output
;
426 #if IS_ENABLED(CONFIG_SSB_EMBEDDED)
427 chip
->to_irq
= ssb_gpio_to_irq
;
430 /* There is just one SoC in one device and its GPIO addresses should be
431 * deterministic to address them more easily. The other buses could get
432 * a random base number. */
433 if (bus
->bustype
== SSB_BUSTYPE_SSB
)
438 err
= ssb_gpio_irq_extif_domain_init(bus
);
442 err
= gpiochip_add(chip
);
444 ssb_gpio_irq_extif_domain_exit(bus
);
452 static int ssb_gpio_extif_init(struct ssb_bus
*bus
)
458 /**************************************************
460 **************************************************/
462 int ssb_gpio_init(struct ssb_bus
*bus
)
464 if (ssb_chipco_available(&bus
->chipco
))
465 return ssb_gpio_chipco_init(bus
);
466 else if (ssb_extif_available(&bus
->extif
))
467 return ssb_gpio_extif_init(bus
);
474 int ssb_gpio_unregister(struct ssb_bus
*bus
)
476 if (ssb_chipco_available(&bus
->chipco
) ||
477 ssb_extif_available(&bus
->extif
)) {
478 return gpiochip_remove(&bus
->gpio
);