USB: keyspan: fix null-deref at disconnect and release
[linux/fpc-iii.git] / drivers / ssb / driver_gpio.c
blobdc109de228c67b079f5eed0623fc3e1d94451b6e
1 /*
2 * Sonics Silicon Backplane
3 * GPIO driver
5 * Copyright 2011, Broadcom Corporation
6 * Copyright 2012, Hauke Mehrtens <hauke@hauke-m.de>
8 * Licensed under the GNU/GPL. See COPYING for details.
9 */
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,
30 int value)
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,
38 unsigned gpio)
40 struct ssb_bus *bus = ssb_gpio_get_bus(chip);
42 ssb_chipco_gpio_outen(&bus->chipco, 1 << gpio, 0);
43 return 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);
53 return 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);
61 /* clear pulldown */
62 ssb_chipco_gpio_pulldown(&bus->chipco, 1 << gpio, 0);
63 /* Set pullup */
64 ssb_chipco_gpio_pullup(&bus->chipco, 1 << gpio, 1 << gpio);
66 return 0;
69 static void ssb_gpio_chipco_free(struct gpio_chip *chip, unsigned gpio)
71 struct ssb_bus *bus = ssb_gpio_get_bus(chip);
73 /* clear pullup */
74 ssb_chipco_gpio_pullup(&bus->chipco, 1 << gpio, 0);
77 static int ssb_gpio_chipco_to_irq(struct gpio_chip *chip, unsigned gpio)
79 struct ssb_bus *bus = ssb_gpio_get_bus(chip);
81 if (bus->bustype == SSB_BUSTYPE_SSB)
82 return ssb_mips_irq(bus->chipco.dev) + 2;
83 else
84 return -EINVAL;
87 static int ssb_gpio_chipco_init(struct ssb_bus *bus)
89 struct gpio_chip *chip = &bus->gpio;
91 chip->label = "ssb_chipco_gpio";
92 chip->owner = THIS_MODULE;
93 chip->request = ssb_gpio_chipco_request;
94 chip->free = ssb_gpio_chipco_free;
95 chip->get = ssb_gpio_chipco_get_value;
96 chip->set = ssb_gpio_chipco_set_value;
97 chip->direction_input = ssb_gpio_chipco_direction_input;
98 chip->direction_output = ssb_gpio_chipco_direction_output;
99 chip->to_irq = ssb_gpio_chipco_to_irq;
100 chip->ngpio = 16;
101 /* There is just one SoC in one device and its GPIO addresses should be
102 * deterministic to address them more easily. The other buses could get
103 * a random base number. */
104 if (bus->bustype == SSB_BUSTYPE_SSB)
105 chip->base = 0;
106 else
107 chip->base = -1;
109 return gpiochip_add(chip);
112 #ifdef CONFIG_SSB_DRIVER_EXTIF
114 static int ssb_gpio_extif_get_value(struct gpio_chip *chip, unsigned gpio)
116 struct ssb_bus *bus = ssb_gpio_get_bus(chip);
118 return !!ssb_extif_gpio_in(&bus->extif, 1 << gpio);
121 static void ssb_gpio_extif_set_value(struct gpio_chip *chip, unsigned gpio,
122 int value)
124 struct ssb_bus *bus = ssb_gpio_get_bus(chip);
126 ssb_extif_gpio_out(&bus->extif, 1 << gpio, value ? 1 << gpio : 0);
129 static int ssb_gpio_extif_direction_input(struct gpio_chip *chip,
130 unsigned gpio)
132 struct ssb_bus *bus = ssb_gpio_get_bus(chip);
134 ssb_extif_gpio_outen(&bus->extif, 1 << gpio, 0);
135 return 0;
138 static int ssb_gpio_extif_direction_output(struct gpio_chip *chip,
139 unsigned gpio, int value)
141 struct ssb_bus *bus = ssb_gpio_get_bus(chip);
143 ssb_extif_gpio_outen(&bus->extif, 1 << gpio, 1 << gpio);
144 ssb_extif_gpio_out(&bus->extif, 1 << gpio, value ? 1 << gpio : 0);
145 return 0;
148 static int ssb_gpio_extif_to_irq(struct gpio_chip *chip, unsigned gpio)
150 struct ssb_bus *bus = ssb_gpio_get_bus(chip);
152 if (bus->bustype == SSB_BUSTYPE_SSB)
153 return ssb_mips_irq(bus->extif.dev) + 2;
154 else
155 return -EINVAL;
158 static int ssb_gpio_extif_init(struct ssb_bus *bus)
160 struct gpio_chip *chip = &bus->gpio;
162 chip->label = "ssb_extif_gpio";
163 chip->owner = THIS_MODULE;
164 chip->get = ssb_gpio_extif_get_value;
165 chip->set = ssb_gpio_extif_set_value;
166 chip->direction_input = ssb_gpio_extif_direction_input;
167 chip->direction_output = ssb_gpio_extif_direction_output;
168 chip->to_irq = ssb_gpio_extif_to_irq;
169 chip->ngpio = 5;
170 /* There is just one SoC in one device and its GPIO addresses should be
171 * deterministic to address them more easily. The other buses could get
172 * a random base number. */
173 if (bus->bustype == SSB_BUSTYPE_SSB)
174 chip->base = 0;
175 else
176 chip->base = -1;
178 return gpiochip_add(chip);
181 #else
182 static int ssb_gpio_extif_init(struct ssb_bus *bus)
184 return -ENOTSUPP;
186 #endif
188 int ssb_gpio_init(struct ssb_bus *bus)
190 if (ssb_chipco_available(&bus->chipco))
191 return ssb_gpio_chipco_init(bus);
192 else if (ssb_extif_available(&bus->extif))
193 return ssb_gpio_extif_init(bus);
194 else
195 SSB_WARN_ON(1);
197 return -1;
200 int ssb_gpio_unregister(struct ssb_bus *bus)
202 if (ssb_chipco_available(&bus->chipco) ||
203 ssb_extif_available(&bus->extif)) {
204 return gpiochip_remove(&bus->gpio);
205 } else {
206 SSB_WARN_ON(1);
209 return -1;