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/export.h>
13 #include <linux/ssb/ssb.h>
15 #include "ssb_private.h"
17 static struct ssb_bus *ssb_gpio_get_bus(struct gpio_chip *chip)
19 return container_of(chip, struct ssb_bus, gpio);
22 static int ssb_gpio_chipco_get_value(struct gpio_chip *chip, unsigned gpio)
24 struct ssb_bus *bus = ssb_gpio_get_bus(chip);
26 return !!ssb_chipco_gpio_in(&bus->chipco, 1 << gpio);
29 static void ssb_gpio_chipco_set_value(struct gpio_chip *chip, unsigned gpio,
32 struct ssb_bus *bus = ssb_gpio_get_bus(chip);
34 ssb_chipco_gpio_out(&bus->chipco, 1 << gpio, value ? 1 << gpio : 0);
37 static int ssb_gpio_chipco_direction_input(struct gpio_chip *chip,
40 struct ssb_bus *bus = ssb_gpio_get_bus(chip);
42 ssb_chipco_gpio_outen(&bus->chipco, 1 << gpio, 0);
46 static int ssb_gpio_chipco_direction_output(struct gpio_chip *chip,
47 unsigned gpio, int value)
49 struct ssb_bus *bus = ssb_gpio_get_bus(chip);
51 ssb_chipco_gpio_outen(&bus->chipco, 1 << gpio, 1 << gpio);
52 ssb_chipco_gpio_out(&bus->chipco, 1 << gpio, value ? 1 << gpio : 0);
56 static int ssb_gpio_chipco_request(struct gpio_chip *chip, unsigned gpio)
58 struct ssb_bus *bus = ssb_gpio_get_bus(chip);
60 ssb_chipco_gpio_control(&bus->chipco, 1 << gpio, 0);
62 ssb_chipco_gpio_pulldown(&bus->chipco, 1 << gpio, 0);
64 ssb_chipco_gpio_pullup(&bus->chipco, 1 << gpio, 1 << gpio);
69 static void ssb_gpio_chipco_free(struct gpio_chip *chip, unsigned gpio)
71 struct ssb_bus *bus = ssb_gpio_get_bus(chip);
74 ssb_chipco_gpio_pullup(&bus->chipco, 1 << gpio, 0);
77 static int ssb_gpio_chipco_init(struct ssb_bus *bus)
79 struct gpio_chip *chip = &bus->gpio;
81 chip->label = "ssb_chipco_gpio";
82 chip->owner = THIS_MODULE;
83 chip->request = ssb_gpio_chipco_request;
84 chip->free = ssb_gpio_chipco_free;
85 chip->get = ssb_gpio_chipco_get_value;
86 chip->set = ssb_gpio_chipco_set_value;
87 chip->direction_input = ssb_gpio_chipco_direction_input;
88 chip->direction_output = ssb_gpio_chipco_direction_output;
90 /* There is just one SoC in one device and its GPIO addresses should be
91 * deterministic to address them more easily. The other buses could get
92 * a random base number. */
93 if (bus->bustype == SSB_BUSTYPE_SSB)
98 return gpiochip_add(chip);
101 #ifdef CONFIG_SSB_DRIVER_EXTIF
103 static int ssb_gpio_extif_get_value(struct gpio_chip *chip, unsigned gpio)
105 struct ssb_bus *bus = ssb_gpio_get_bus(chip);
107 return !!ssb_extif_gpio_in(&bus->extif, 1 << gpio);
110 static void ssb_gpio_extif_set_value(struct gpio_chip *chip, unsigned gpio,
113 struct ssb_bus *bus = ssb_gpio_get_bus(chip);
115 ssb_extif_gpio_out(&bus->extif, 1 << gpio, value ? 1 << gpio : 0);
118 static int ssb_gpio_extif_direction_input(struct gpio_chip *chip,
121 struct ssb_bus *bus = ssb_gpio_get_bus(chip);
123 ssb_extif_gpio_outen(&bus->extif, 1 << gpio, 0);
127 static int ssb_gpio_extif_direction_output(struct gpio_chip *chip,
128 unsigned gpio, int value)
130 struct ssb_bus *bus = ssb_gpio_get_bus(chip);
132 ssb_extif_gpio_outen(&bus->extif, 1 << gpio, 1 << gpio);
133 ssb_extif_gpio_out(&bus->extif, 1 << gpio, value ? 1 << gpio : 0);
137 static int ssb_gpio_extif_init(struct ssb_bus *bus)
139 struct gpio_chip *chip = &bus->gpio;
141 chip->label = "ssb_extif_gpio";
142 chip->owner = THIS_MODULE;
143 chip->get = ssb_gpio_extif_get_value;
144 chip->set = ssb_gpio_extif_set_value;
145 chip->direction_input = ssb_gpio_extif_direction_input;
146 chip->direction_output = ssb_gpio_extif_direction_output;
148 /* There is just one SoC in one device and its GPIO addresses should be
149 * deterministic to address them more easily. The other buses could get
150 * a random base number. */
151 if (bus->bustype == SSB_BUSTYPE_SSB)
156 return gpiochip_add(chip);
160 static int ssb_gpio_extif_init(struct ssb_bus *bus)
166 int ssb_gpio_init(struct ssb_bus *bus)
168 if (ssb_chipco_available(&bus->chipco))
169 return ssb_gpio_chipco_init(bus);
170 else if (ssb_extif_available(&bus->extif))
171 return ssb_gpio_extif_init(bus);