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_irq(ssb_gpio_to_irq(&bus->gpio, gpio));
136 ssb_chipco_gpio_polarity(chipco, irqs, val & irqs);
141 static int ssb_gpio_irq_chipco_domain_init(struct ssb_bus *bus)
143 struct ssb_chipcommon *chipco = &bus->chipco;
144 struct gpio_chip *chip = &bus->gpio;
145 int gpio, hwirq, err;
147 if (bus->bustype != SSB_BUSTYPE_SSB)
150 bus->irq_domain = irq_domain_add_linear(NULL, chip->ngpio,
151 &irq_domain_simple_ops, chipco);
152 if (!bus->irq_domain) {
156 for (gpio = 0; gpio < chip->ngpio; gpio++) {
157 int irq = irq_create_mapping(bus->irq_domain, gpio);
159 irq_set_chip_data(irq, bus);
160 irq_set_chip_and_handler(irq, &ssb_gpio_irq_chipco_chip,
164 hwirq = ssb_mips_irq(bus->chipco.dev) + 2;
165 err = request_irq(hwirq, ssb_gpio_irq_chipco_handler, IRQF_SHARED,
170 ssb_chipco_gpio_intmask(&bus->chipco, ~0, 0);
171 chipco_set32(chipco, SSB_CHIPCO_IRQMASK, SSB_CHIPCO_IRQ_GPIO);
176 for (gpio = 0; gpio < chip->ngpio; gpio++) {
177 int irq = irq_find_mapping(bus->irq_domain, gpio);
179 irq_dispose_mapping(irq);
181 irq_domain_remove(bus->irq_domain);
186 static void ssb_gpio_irq_chipco_domain_exit(struct ssb_bus *bus)
188 struct ssb_chipcommon *chipco = &bus->chipco;
189 struct gpio_chip *chip = &bus->gpio;
192 if (bus->bustype != SSB_BUSTYPE_SSB)
195 chipco_mask32(chipco, SSB_CHIPCO_IRQMASK, ~SSB_CHIPCO_IRQ_GPIO);
196 free_irq(ssb_mips_irq(bus->chipco.dev) + 2, chipco);
197 for (gpio = 0; gpio < chip->ngpio; gpio++) {
198 int irq = irq_find_mapping(bus->irq_domain, gpio);
200 irq_dispose_mapping(irq);
202 irq_domain_remove(bus->irq_domain);
205 static int ssb_gpio_irq_chipco_domain_init(struct ssb_bus *bus)
210 static void ssb_gpio_irq_chipco_domain_exit(struct ssb_bus *bus)
215 static int ssb_gpio_chipco_init(struct ssb_bus *bus)
217 struct gpio_chip *chip = &bus->gpio;
220 chip->label = "ssb_chipco_gpio";
221 chip->owner = THIS_MODULE;
222 chip->request = ssb_gpio_chipco_request;
223 chip->free = ssb_gpio_chipco_free;
224 chip->get = ssb_gpio_chipco_get_value;
225 chip->set = ssb_gpio_chipco_set_value;
226 chip->direction_input = ssb_gpio_chipco_direction_input;
227 chip->direction_output = ssb_gpio_chipco_direction_output;
228 #if IS_ENABLED(CONFIG_SSB_EMBEDDED)
229 chip->to_irq = ssb_gpio_to_irq;
232 /* There is just one SoC in one device and its GPIO addresses should be
233 * deterministic to address them more easily. The other buses could get
234 * a random base number.
236 if (bus->bustype == SSB_BUSTYPE_SSB)
241 err = ssb_gpio_irq_chipco_domain_init(bus);
245 err = gpiochip_add_data(chip, bus);
247 ssb_gpio_irq_chipco_domain_exit(bus);
254 /**************************************************
256 **************************************************/
258 #ifdef CONFIG_SSB_DRIVER_EXTIF
260 static int ssb_gpio_extif_get_value(struct gpio_chip *chip, unsigned int gpio)
262 struct ssb_bus *bus = gpiochip_get_data(chip);
264 return !!ssb_extif_gpio_in(&bus->extif, 1 << gpio);
267 static void ssb_gpio_extif_set_value(struct gpio_chip *chip, unsigned int gpio,
270 struct ssb_bus *bus = gpiochip_get_data(chip);
272 ssb_extif_gpio_out(&bus->extif, 1 << gpio, value ? 1 << gpio : 0);
275 static int ssb_gpio_extif_direction_input(struct gpio_chip *chip,
278 struct ssb_bus *bus = gpiochip_get_data(chip);
280 ssb_extif_gpio_outen(&bus->extif, 1 << gpio, 0);
284 static int ssb_gpio_extif_direction_output(struct gpio_chip *chip,
285 unsigned int gpio, int value)
287 struct ssb_bus *bus = gpiochip_get_data(chip);
289 ssb_extif_gpio_outen(&bus->extif, 1 << gpio, 1 << gpio);
290 ssb_extif_gpio_out(&bus->extif, 1 << gpio, value ? 1 << gpio : 0);
294 #if IS_ENABLED(CONFIG_SSB_EMBEDDED)
295 static void ssb_gpio_irq_extif_mask(struct irq_data *d)
297 struct ssb_bus *bus = irq_data_get_irq_chip_data(d);
298 int gpio = irqd_to_hwirq(d);
300 ssb_extif_gpio_intmask(&bus->extif, BIT(gpio), 0);
303 static void ssb_gpio_irq_extif_unmask(struct irq_data *d)
305 struct ssb_bus *bus = irq_data_get_irq_chip_data(d);
306 int gpio = irqd_to_hwirq(d);
307 u32 val = ssb_extif_gpio_in(&bus->extif, BIT(gpio));
309 ssb_extif_gpio_polarity(&bus->extif, BIT(gpio), val);
310 ssb_extif_gpio_intmask(&bus->extif, BIT(gpio), BIT(gpio));
313 static struct irq_chip ssb_gpio_irq_extif_chip = {
314 .name = "SSB-GPIO-EXTIF",
315 .irq_mask = ssb_gpio_irq_extif_mask,
316 .irq_unmask = ssb_gpio_irq_extif_unmask,
319 static irqreturn_t ssb_gpio_irq_extif_handler(int irq, void *dev_id)
321 struct ssb_bus *bus = dev_id;
322 struct ssb_extif *extif = &bus->extif;
323 u32 val = ssb_read32(extif->dev, SSB_EXTIF_GPIO_IN);
324 u32 mask = ssb_read32(extif->dev, SSB_EXTIF_GPIO_INTMASK);
325 u32 pol = ssb_read32(extif->dev, SSB_EXTIF_GPIO_INTPOL);
326 unsigned long irqs = (val ^ pol) & mask;
332 for_each_set_bit(gpio, &irqs, bus->gpio.ngpio)
333 generic_handle_irq(ssb_gpio_to_irq(&bus->gpio, gpio));
334 ssb_extif_gpio_polarity(extif, irqs, val & irqs);
339 static int ssb_gpio_irq_extif_domain_init(struct ssb_bus *bus)
341 struct ssb_extif *extif = &bus->extif;
342 struct gpio_chip *chip = &bus->gpio;
343 int gpio, hwirq, err;
345 if (bus->bustype != SSB_BUSTYPE_SSB)
348 bus->irq_domain = irq_domain_add_linear(NULL, chip->ngpio,
349 &irq_domain_simple_ops, extif);
350 if (!bus->irq_domain) {
354 for (gpio = 0; gpio < chip->ngpio; gpio++) {
355 int irq = irq_create_mapping(bus->irq_domain, gpio);
357 irq_set_chip_data(irq, bus);
358 irq_set_chip_and_handler(irq, &ssb_gpio_irq_extif_chip,
362 hwirq = ssb_mips_irq(bus->extif.dev) + 2;
363 err = request_irq(hwirq, ssb_gpio_irq_extif_handler, IRQF_SHARED,
368 ssb_extif_gpio_intmask(&bus->extif, ~0, 0);
373 for (gpio = 0; gpio < chip->ngpio; gpio++) {
374 int irq = irq_find_mapping(bus->irq_domain, gpio);
376 irq_dispose_mapping(irq);
378 irq_domain_remove(bus->irq_domain);
383 static void ssb_gpio_irq_extif_domain_exit(struct ssb_bus *bus)
385 struct ssb_extif *extif = &bus->extif;
386 struct gpio_chip *chip = &bus->gpio;
389 if (bus->bustype != SSB_BUSTYPE_SSB)
392 free_irq(ssb_mips_irq(bus->extif.dev) + 2, extif);
393 for (gpio = 0; gpio < chip->ngpio; gpio++) {
394 int irq = irq_find_mapping(bus->irq_domain, gpio);
396 irq_dispose_mapping(irq);
398 irq_domain_remove(bus->irq_domain);
401 static int ssb_gpio_irq_extif_domain_init(struct ssb_bus *bus)
406 static void ssb_gpio_irq_extif_domain_exit(struct ssb_bus *bus)
411 static int ssb_gpio_extif_init(struct ssb_bus *bus)
413 struct gpio_chip *chip = &bus->gpio;
416 chip->label = "ssb_extif_gpio";
417 chip->owner = THIS_MODULE;
418 chip->get = ssb_gpio_extif_get_value;
419 chip->set = ssb_gpio_extif_set_value;
420 chip->direction_input = ssb_gpio_extif_direction_input;
421 chip->direction_output = ssb_gpio_extif_direction_output;
422 #if IS_ENABLED(CONFIG_SSB_EMBEDDED)
423 chip->to_irq = ssb_gpio_to_irq;
426 /* There is just one SoC in one device and its GPIO addresses should be
427 * deterministic to address them more easily. The other buses could get
428 * a random base number.
430 if (bus->bustype == SSB_BUSTYPE_SSB)
435 err = ssb_gpio_irq_extif_domain_init(bus);
439 err = gpiochip_add_data(chip, bus);
441 ssb_gpio_irq_extif_domain_exit(bus);
449 static int ssb_gpio_extif_init(struct ssb_bus *bus)
455 /**************************************************
457 **************************************************/
459 int ssb_gpio_init(struct ssb_bus *bus)
461 if (ssb_chipco_available(&bus->chipco))
462 return ssb_gpio_chipco_init(bus);
463 else if (ssb_extif_available(&bus->extif))
464 return ssb_gpio_extif_init(bus);
468 int ssb_gpio_unregister(struct ssb_bus *bus)
470 if (ssb_chipco_available(&bus->chipco) ||
471 ssb_extif_available(&bus->extif)) {
472 gpiochip_remove(&bus->gpio);