Commit | Line | Data |
---|---|---|
d2876d08 DB |
1 | #include <linux/kernel.h> |
2 | #include <linux/module.h> | |
ff77c352 | 3 | #include <linux/interrupt.h> |
d2876d08 DB |
4 | #include <linux/irq.h> |
5 | #include <linux/spinlock.h> | |
1a989d0f | 6 | #include <linux/list.h> |
d8f388d8 DB |
7 | #include <linux/device.h> |
8 | #include <linux/err.h> | |
9 | #include <linux/debugfs.h> | |
10 | #include <linux/seq_file.h> | |
11 | #include <linux/gpio.h> | |
391c970c | 12 | #include <linux/of_gpio.h> |
81f59e9d | 13 | #include <linux/acpi_gpio.h> |
ff77c352 | 14 | #include <linux/idr.h> |
5a0e3ad6 | 15 | #include <linux/slab.h> |
7b199811 | 16 | #include <linux/acpi.h> |
53e7cac3 | 17 | #include <linux/gpio/driver.h> |
d2876d08 | 18 | |
3f397c21 UKK |
19 | #define CREATE_TRACE_POINTS |
20 | #include <trace/events/gpio.h> | |
d2876d08 | 21 | |
79a9becd | 22 | /* Implementation infrastructure for GPIO interfaces. |
d2876d08 | 23 | * |
79a9becd AC |
24 | * The GPIO programming interface allows for inlining speed-critical |
25 | * get/set operations for common cases, so that access to SOC-integrated | |
26 | * GPIOs can sometimes cost only an instruction or two per bit. | |
d2876d08 DB |
27 | */ |
28 | ||
29 | ||
30 | /* When debugging, extend minimal trust to callers and platform code. | |
31 | * Also emit diagnostic messages that may help initial bringup, when | |
32 | * board setup or driver bugs are most common. | |
33 | * | |
34 | * Otherwise, minimize overhead in what may be bitbanging codepaths. | |
35 | */ | |
36 | #ifdef DEBUG | |
37 | #define extra_checks 1 | |
38 | #else | |
39 | #define extra_checks 0 | |
40 | #endif | |
41 | ||
42 | /* gpio_lock prevents conflicts during gpio_desc[] table updates. | |
43 | * While any GPIO is requested, its gpio_chip is not removable; | |
44 | * each GPIO's "requested" flag serves as a lock and refcount. | |
45 | */ | |
46 | static DEFINE_SPINLOCK(gpio_lock); | |
47 | ||
48 | struct gpio_desc { | |
49 | struct gpio_chip *chip; | |
50 | unsigned long flags; | |
51 | /* flag symbols are bit numbers */ | |
52 | #define FLAG_REQUESTED 0 | |
53 | #define FLAG_IS_OUT 1 | |
710b40ea AC |
54 | #define FLAG_EXPORT 2 /* protected by sysfs_lock */ |
55 | #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */ | |
56 | #define FLAG_TRIG_FALL 4 /* trigger on falling edge */ | |
57 | #define FLAG_TRIG_RISE 5 /* trigger on rising edge */ | |
79a9becd | 58 | #define FLAG_ACTIVE_LOW 6 /* value has active low */ |
710b40ea AC |
59 | #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ |
60 | #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ | |
d468bf9e | 61 | #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ |
ff77c352 | 62 | |
5ba1821d | 63 | #define ID_SHIFT 16 /* add new flags before this one */ |
ff77c352 | 64 | |
5ba1821d | 65 | #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1) |
ff77c352 | 66 | #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE)) |
d2876d08 DB |
67 | |
68 | #ifdef CONFIG_DEBUG_FS | |
69 | const char *label; | |
70 | #endif | |
71 | }; | |
72 | static struct gpio_desc gpio_desc[ARCH_NR_GPIOS]; | |
73 | ||
6c0b4e6c AC |
74 | #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio) |
75 | ||
bae48da2 AC |
76 | static DEFINE_MUTEX(gpio_lookup_lock); |
77 | static LIST_HEAD(gpio_lookup_list); | |
1a989d0f AC |
78 | static LIST_HEAD(gpio_chips); |
79 | ||
ff77c352 | 80 | #ifdef CONFIG_GPIO_SYSFS |
5ba1821d | 81 | static DEFINE_IDR(dirent_idr); |
ff77c352 DG |
82 | #endif |
83 | ||
372e722e AC |
84 | static int gpiod_request(struct gpio_desc *desc, const char *label); |
85 | static void gpiod_free(struct gpio_desc *desc); | |
372e722e | 86 | |
1a2a99c6 AS |
87 | /* With descriptor prefix */ |
88 | ||
7b17b59f | 89 | #ifdef CONFIG_DEBUG_FS |
7589e59f AS |
90 | #define gpiod_emerg(desc, fmt, ...) \ |
91 | pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ | |
7b17b59f | 92 | ##__VA_ARGS__) |
7589e59f AS |
93 | #define gpiod_crit(desc, fmt, ...) \ |
94 | pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ | |
7b17b59f | 95 | ##__VA_ARGS__) |
7589e59f AS |
96 | #define gpiod_err(desc, fmt, ...) \ |
97 | pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ | |
7b17b59f | 98 | ##__VA_ARGS__) |
7589e59f AS |
99 | #define gpiod_warn(desc, fmt, ...) \ |
100 | pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ | |
7b17b59f | 101 | ##__VA_ARGS__) |
7589e59f AS |
102 | #define gpiod_info(desc, fmt, ...) \ |
103 | pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ | |
7b17b59f | 104 | ##__VA_ARGS__) |
7589e59f AS |
105 | #define gpiod_dbg(desc, fmt, ...) \ |
106 | pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ | |
7b17b59f MB |
107 | ##__VA_ARGS__) |
108 | #else | |
7589e59f | 109 | #define gpiod_emerg(desc, fmt, ...) \ |
6424de5a | 110 | pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) |
7589e59f | 111 | #define gpiod_crit(desc, fmt, ...) \ |
6424de5a | 112 | pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) |
7589e59f | 113 | #define gpiod_err(desc, fmt, ...) \ |
6424de5a | 114 | pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) |
7589e59f | 115 | #define gpiod_warn(desc, fmt, ...) \ |
6424de5a | 116 | pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) |
7589e59f | 117 | #define gpiod_info(desc, fmt, ...) \ |
6424de5a | 118 | pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) |
7589e59f | 119 | #define gpiod_dbg(desc, fmt, ...) \ |
6424de5a | 120 | pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) |
7b17b59f | 121 | #endif |
372e722e | 122 | |
1a2a99c6 AS |
123 | /* With chip prefix */ |
124 | ||
125 | #define chip_emerg(chip, fmt, ...) \ | |
126 | pr_emerg("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) | |
127 | #define chip_crit(chip, fmt, ...) \ | |
128 | pr_crit("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) | |
129 | #define chip_err(chip, fmt, ...) \ | |
130 | pr_err("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) | |
131 | #define chip_warn(chip, fmt, ...) \ | |
132 | pr_warn("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) | |
133 | #define chip_info(chip, fmt, ...) \ | |
134 | pr_info("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) | |
135 | #define chip_dbg(chip, fmt, ...) \ | |
136 | pr_debug("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) | |
137 | ||
d2876d08 DB |
138 | static inline void desc_set_label(struct gpio_desc *d, const char *label) |
139 | { | |
140 | #ifdef CONFIG_DEBUG_FS | |
141 | d->label = label; | |
142 | #endif | |
143 | } | |
144 | ||
372e722e AC |
145 | /* |
146 | * Return the GPIO number of the passed descriptor relative to its chip | |
147 | */ | |
148 | static int gpio_chip_hwgpio(const struct gpio_desc *desc) | |
149 | { | |
6c0b4e6c | 150 | return desc - &desc->chip->desc[0]; |
372e722e AC |
151 | } |
152 | ||
153 | /** | |
154 | * Convert a GPIO number to its descriptor | |
155 | */ | |
79a9becd | 156 | struct gpio_desc *gpio_to_desc(unsigned gpio) |
372e722e AC |
157 | { |
158 | if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio)) | |
159 | return NULL; | |
160 | else | |
161 | return &gpio_desc[gpio]; | |
162 | } | |
79a9becd | 163 | EXPORT_SYMBOL_GPL(gpio_to_desc); |
372e722e | 164 | |
d468bf9e LW |
165 | /** |
166 | * Convert an offset on a certain chip to a corresponding descriptor | |
167 | */ | |
168 | static struct gpio_desc *gpiochip_offset_to_desc(struct gpio_chip *chip, | |
169 | unsigned int offset) | |
170 | { | |
b7d0a28a AC |
171 | if (offset >= chip->ngpio) |
172 | return ERR_PTR(-EINVAL); | |
d468bf9e | 173 | |
b7d0a28a | 174 | return &chip->desc[offset]; |
d468bf9e | 175 | } |
372e722e AC |
176 | |
177 | /** | |
178 | * Convert a GPIO descriptor to the integer namespace. | |
179 | * This should disappear in the future but is needed since we still | |
180 | * use GPIO numbers for error messages and sysfs nodes | |
181 | */ | |
79a9becd | 182 | int desc_to_gpio(const struct gpio_desc *desc) |
372e722e | 183 | { |
8c0fca81 | 184 | return desc - &gpio_desc[0]; |
372e722e | 185 | } |
79a9becd | 186 | EXPORT_SYMBOL_GPL(desc_to_gpio); |
372e722e AC |
187 | |
188 | ||
d2876d08 DB |
189 | /* Warn when drivers omit gpio_request() calls -- legal but ill-advised |
190 | * when setting direction, and otherwise illegal. Until board setup code | |
191 | * and drivers use explicit requests everywhere (which won't happen when | |
192 | * those calls have no teeth) we can't avoid autorequesting. This nag | |
35e8bb51 DB |
193 | * message should motivate switching to explicit requests... so should |
194 | * the weaker cleanup after faults, compared to gpio_request(). | |
8a0cecff DB |
195 | * |
196 | * NOTE: the autorequest mechanism is going away; at this point it's | |
197 | * only "legal" in the sense that (old) code using it won't break yet, | |
198 | * but instead only triggers a WARN() stack dump. | |
d2876d08 | 199 | */ |
372e722e | 200 | static int gpio_ensure_requested(struct gpio_desc *desc) |
d2876d08 | 201 | { |
8a0cecff | 202 | const struct gpio_chip *chip = desc->chip; |
372e722e | 203 | const int gpio = desc_to_gpio(desc); |
35e8bb51 | 204 | |
8a0cecff DB |
205 | if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0, |
206 | "autorequest GPIO-%d\n", gpio)) { | |
35e8bb51 | 207 | if (!try_module_get(chip->owner)) { |
7589e59f AS |
208 | gpiod_err(desc, "%s: module can't be gotten\n", |
209 | __func__); | |
35e8bb51 DB |
210 | clear_bit(FLAG_REQUESTED, &desc->flags); |
211 | /* lose */ | |
212 | return -EIO; | |
213 | } | |
d2876d08 | 214 | desc_set_label(desc, "[auto]"); |
35e8bb51 DB |
215 | /* caller must chip->request() w/o spinlock */ |
216 | if (chip->request) | |
217 | return 1; | |
d2876d08 | 218 | } |
35e8bb51 | 219 | return 0; |
d2876d08 DB |
220 | } |
221 | ||
79a9becd AC |
222 | /** |
223 | * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs | |
224 | * @desc: descriptor to return the chip of | |
225 | */ | |
226 | struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) | |
372e722e | 227 | { |
bcabdef1 | 228 | return desc ? desc->chip : NULL; |
372e722e | 229 | } |
79a9becd | 230 | EXPORT_SYMBOL_GPL(gpiod_to_chip); |
d2876d08 | 231 | |
8d0aab2f AV |
232 | /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ |
233 | static int gpiochip_find_base(int ngpio) | |
234 | { | |
83cabe33 AC |
235 | struct gpio_chip *chip; |
236 | int base = ARCH_NR_GPIOS - ngpio; | |
8d0aab2f | 237 | |
83cabe33 AC |
238 | list_for_each_entry_reverse(chip, &gpio_chips, list) { |
239 | /* found a free space? */ | |
240 | if (chip->base + chip->ngpio <= base) | |
241 | break; | |
242 | else | |
243 | /* nope, check the space right before the chip */ | |
244 | base = chip->base - ngpio; | |
8d0aab2f AV |
245 | } |
246 | ||
83cabe33 | 247 | if (gpio_is_valid(base)) { |
8d0aab2f | 248 | pr_debug("%s: found new base at %d\n", __func__, base); |
83cabe33 AC |
249 | return base; |
250 | } else { | |
251 | pr_err("%s: cannot find free range\n", __func__); | |
252 | return -ENOSPC; | |
169b6a7a | 253 | } |
169b6a7a AV |
254 | } |
255 | ||
79a9becd AC |
256 | /** |
257 | * gpiod_get_direction - return the current direction of a GPIO | |
258 | * @desc: GPIO to get the direction of | |
259 | * | |
260 | * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error. | |
261 | * | |
262 | * This function may sleep if gpiod_cansleep() is true. | |
263 | */ | |
264 | int gpiod_get_direction(const struct gpio_desc *desc) | |
80b0a602 MN |
265 | { |
266 | struct gpio_chip *chip; | |
372e722e | 267 | unsigned offset; |
80b0a602 MN |
268 | int status = -EINVAL; |
269 | ||
372e722e AC |
270 | chip = gpiod_to_chip(desc); |
271 | offset = gpio_chip_hwgpio(desc); | |
80b0a602 MN |
272 | |
273 | if (!chip->get_direction) | |
274 | return status; | |
275 | ||
372e722e | 276 | status = chip->get_direction(chip, offset); |
80b0a602 MN |
277 | if (status > 0) { |
278 | /* GPIOF_DIR_IN, or other positive */ | |
279 | status = 1; | |
def63433 AC |
280 | /* FLAG_IS_OUT is just a cache of the result of get_direction(), |
281 | * so it does not affect constness per se */ | |
282 | clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags); | |
80b0a602 MN |
283 | } |
284 | if (status == 0) { | |
285 | /* GPIOF_DIR_OUT */ | |
def63433 | 286 | set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags); |
80b0a602 MN |
287 | } |
288 | return status; | |
289 | } | |
79a9becd | 290 | EXPORT_SYMBOL_GPL(gpiod_get_direction); |
80b0a602 | 291 | |
d8f388d8 DB |
292 | #ifdef CONFIG_GPIO_SYSFS |
293 | ||
294 | /* lock protects against unexport_gpio() being called while | |
295 | * sysfs files are active. | |
296 | */ | |
297 | static DEFINE_MUTEX(sysfs_lock); | |
298 | ||
299 | /* | |
300 | * /sys/class/gpio/gpioN... only for GPIOs that are exported | |
301 | * /direction | |
302 | * * MAY BE OMITTED if kernel won't allow direction changes | |
303 | * * is read/write as "in" or "out" | |
304 | * * may also be written as "high" or "low", initializing | |
305 | * output value as specified ("out" implies "low") | |
306 | * /value | |
307 | * * always readable, subject to hardware behavior | |
308 | * * may be writable, as zero/nonzero | |
ff77c352 DG |
309 | * /edge |
310 | * * configures behavior of poll(2) on /value | |
311 | * * available only if pin can generate IRQs on input | |
312 | * * is read/write as "none", "falling", "rising", or "both" | |
07697461 JN |
313 | * /active_low |
314 | * * configures polarity of /value | |
315 | * * is read/write as zero/nonzero | |
316 | * * also affects existing and subsequent "falling" and "rising" | |
317 | * /edge configuration | |
d8f388d8 DB |
318 | */ |
319 | ||
320 | static ssize_t gpio_direction_show(struct device *dev, | |
321 | struct device_attribute *attr, char *buf) | |
322 | { | |
def63433 | 323 | const struct gpio_desc *desc = dev_get_drvdata(dev); |
d8f388d8 DB |
324 | ssize_t status; |
325 | ||
326 | mutex_lock(&sysfs_lock); | |
327 | ||
476171ce | 328 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { |
d8f388d8 | 329 | status = -EIO; |
476171ce | 330 | } else { |
372e722e | 331 | gpiod_get_direction(desc); |
d8f388d8 DB |
332 | status = sprintf(buf, "%s\n", |
333 | test_bit(FLAG_IS_OUT, &desc->flags) | |
334 | ? "out" : "in"); | |
476171ce | 335 | } |
d8f388d8 DB |
336 | |
337 | mutex_unlock(&sysfs_lock); | |
338 | return status; | |
339 | } | |
340 | ||
341 | static ssize_t gpio_direction_store(struct device *dev, | |
342 | struct device_attribute *attr, const char *buf, size_t size) | |
343 | { | |
372e722e | 344 | struct gpio_desc *desc = dev_get_drvdata(dev); |
d8f388d8 DB |
345 | ssize_t status; |
346 | ||
347 | mutex_lock(&sysfs_lock); | |
348 | ||
349 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
350 | status = -EIO; | |
351 | else if (sysfs_streq(buf, "high")) | |
372e722e | 352 | status = gpiod_direction_output(desc, 1); |
d8f388d8 | 353 | else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low")) |
372e722e | 354 | status = gpiod_direction_output(desc, 0); |
d8f388d8 | 355 | else if (sysfs_streq(buf, "in")) |
372e722e | 356 | status = gpiod_direction_input(desc); |
d8f388d8 DB |
357 | else |
358 | status = -EINVAL; | |
359 | ||
360 | mutex_unlock(&sysfs_lock); | |
361 | return status ? : size; | |
362 | } | |
363 | ||
07697461 | 364 | static /* const */ DEVICE_ATTR(direction, 0644, |
d8f388d8 DB |
365 | gpio_direction_show, gpio_direction_store); |
366 | ||
367 | static ssize_t gpio_value_show(struct device *dev, | |
368 | struct device_attribute *attr, char *buf) | |
369 | { | |
372e722e | 370 | struct gpio_desc *desc = dev_get_drvdata(dev); |
d8f388d8 DB |
371 | ssize_t status; |
372 | ||
373 | mutex_lock(&sysfs_lock); | |
374 | ||
79a9becd | 375 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
d8f388d8 | 376 | status = -EIO; |
79a9becd AC |
377 | else |
378 | status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc)); | |
d8f388d8 DB |
379 | |
380 | mutex_unlock(&sysfs_lock); | |
381 | return status; | |
382 | } | |
383 | ||
384 | static ssize_t gpio_value_store(struct device *dev, | |
385 | struct device_attribute *attr, const char *buf, size_t size) | |
386 | { | |
372e722e | 387 | struct gpio_desc *desc = dev_get_drvdata(dev); |
d8f388d8 DB |
388 | ssize_t status; |
389 | ||
390 | mutex_lock(&sysfs_lock); | |
391 | ||
392 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
393 | status = -EIO; | |
394 | else if (!test_bit(FLAG_IS_OUT, &desc->flags)) | |
395 | status = -EPERM; | |
396 | else { | |
397 | long value; | |
398 | ||
a3d88c92 | 399 | status = kstrtol(buf, 0, &value); |
d8f388d8 | 400 | if (status == 0) { |
79a9becd | 401 | gpiod_set_value_cansleep(desc, value); |
d8f388d8 DB |
402 | status = size; |
403 | } | |
404 | } | |
405 | ||
406 | mutex_unlock(&sysfs_lock); | |
407 | return status; | |
408 | } | |
409 | ||
07697461 | 410 | static const DEVICE_ATTR(value, 0644, |
d8f388d8 DB |
411 | gpio_value_show, gpio_value_store); |
412 | ||
ff77c352 DG |
413 | static irqreturn_t gpio_sysfs_irq(int irq, void *priv) |
414 | { | |
5ba1821d | 415 | struct sysfs_dirent *value_sd = priv; |
ff77c352 | 416 | |
5ba1821d | 417 | sysfs_notify_dirent(value_sd); |
ff77c352 DG |
418 | return IRQ_HANDLED; |
419 | } | |
420 | ||
ff77c352 DG |
421 | static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev, |
422 | unsigned long gpio_flags) | |
423 | { | |
5ba1821d | 424 | struct sysfs_dirent *value_sd; |
ff77c352 DG |
425 | unsigned long irq_flags; |
426 | int ret, irq, id; | |
427 | ||
428 | if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags) | |
429 | return 0; | |
430 | ||
372e722e | 431 | irq = gpiod_to_irq(desc); |
ff77c352 DG |
432 | if (irq < 0) |
433 | return -EIO; | |
434 | ||
5ba1821d DG |
435 | id = desc->flags >> ID_SHIFT; |
436 | value_sd = idr_find(&dirent_idr, id); | |
437 | if (value_sd) | |
438 | free_irq(irq, value_sd); | |
ff77c352 DG |
439 | |
440 | desc->flags &= ~GPIO_TRIGGER_MASK; | |
441 | ||
442 | if (!gpio_flags) { | |
d468bf9e | 443 | gpiod_unlock_as_irq(desc); |
ff77c352 | 444 | ret = 0; |
5ba1821d | 445 | goto free_id; |
ff77c352 DG |
446 | } |
447 | ||
448 | irq_flags = IRQF_SHARED; | |
449 | if (test_bit(FLAG_TRIG_FALL, &gpio_flags)) | |
07697461 JN |
450 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
451 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; | |
ff77c352 | 452 | if (test_bit(FLAG_TRIG_RISE, &gpio_flags)) |
07697461 JN |
453 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
454 | IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; | |
ff77c352 | 455 | |
5ba1821d | 456 | if (!value_sd) { |
388975cc | 457 | value_sd = sysfs_get_dirent(dev->kobj.sd, "value"); |
5ba1821d DG |
458 | if (!value_sd) { |
459 | ret = -ENODEV; | |
ff77c352 DG |
460 | goto err_out; |
461 | } | |
462 | ||
62f516b8 TH |
463 | ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL); |
464 | if (ret < 0) | |
5ba1821d | 465 | goto free_sd; |
62f516b8 | 466 | id = ret; |
ff77c352 DG |
467 | |
468 | desc->flags &= GPIO_FLAGS_MASK; | |
5ba1821d | 469 | desc->flags |= (unsigned long)id << ID_SHIFT; |
ff77c352 | 470 | |
5ba1821d | 471 | if (desc->flags >> ID_SHIFT != id) { |
ff77c352 DG |
472 | ret = -ERANGE; |
473 | goto free_id; | |
474 | } | |
ff77c352 DG |
475 | } |
476 | ||
364fadb3 | 477 | ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags, |
5ba1821d | 478 | "gpiolib", value_sd); |
364fadb3 | 479 | if (ret < 0) |
5ba1821d | 480 | goto free_id; |
ff77c352 | 481 | |
d468bf9e LW |
482 | ret = gpiod_lock_as_irq(desc); |
483 | if (ret < 0) { | |
484 | gpiod_warn(desc, "failed to flag the GPIO for IRQ\n"); | |
485 | goto free_id; | |
486 | } | |
487 | ||
ff77c352 DG |
488 | desc->flags |= gpio_flags; |
489 | return 0; | |
490 | ||
ff77c352 | 491 | free_id: |
5ba1821d | 492 | idr_remove(&dirent_idr, id); |
ff77c352 | 493 | desc->flags &= GPIO_FLAGS_MASK; |
5ba1821d DG |
494 | free_sd: |
495 | if (value_sd) | |
496 | sysfs_put(value_sd); | |
ff77c352 DG |
497 | err_out: |
498 | return ret; | |
499 | } | |
500 | ||
501 | static const struct { | |
502 | const char *name; | |
503 | unsigned long flags; | |
504 | } trigger_types[] = { | |
505 | { "none", 0 }, | |
506 | { "falling", BIT(FLAG_TRIG_FALL) }, | |
507 | { "rising", BIT(FLAG_TRIG_RISE) }, | |
508 | { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) }, | |
509 | }; | |
510 | ||
511 | static ssize_t gpio_edge_show(struct device *dev, | |
512 | struct device_attribute *attr, char *buf) | |
513 | { | |
514 | const struct gpio_desc *desc = dev_get_drvdata(dev); | |
515 | ssize_t status; | |
516 | ||
517 | mutex_lock(&sysfs_lock); | |
518 | ||
519 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
520 | status = -EIO; | |
521 | else { | |
522 | int i; | |
523 | ||
524 | status = 0; | |
525 | for (i = 0; i < ARRAY_SIZE(trigger_types); i++) | |
526 | if ((desc->flags & GPIO_TRIGGER_MASK) | |
527 | == trigger_types[i].flags) { | |
528 | status = sprintf(buf, "%s\n", | |
529 | trigger_types[i].name); | |
530 | break; | |
531 | } | |
532 | } | |
533 | ||
534 | mutex_unlock(&sysfs_lock); | |
535 | return status; | |
536 | } | |
537 | ||
538 | static ssize_t gpio_edge_store(struct device *dev, | |
539 | struct device_attribute *attr, const char *buf, size_t size) | |
540 | { | |
541 | struct gpio_desc *desc = dev_get_drvdata(dev); | |
542 | ssize_t status; | |
543 | int i; | |
544 | ||
545 | for (i = 0; i < ARRAY_SIZE(trigger_types); i++) | |
546 | if (sysfs_streq(trigger_types[i].name, buf)) | |
547 | goto found; | |
548 | return -EINVAL; | |
549 | ||
550 | found: | |
551 | mutex_lock(&sysfs_lock); | |
552 | ||
553 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
554 | status = -EIO; | |
555 | else { | |
556 | status = gpio_setup_irq(desc, dev, trigger_types[i].flags); | |
557 | if (!status) | |
558 | status = size; | |
559 | } | |
560 | ||
561 | mutex_unlock(&sysfs_lock); | |
562 | ||
563 | return status; | |
564 | } | |
565 | ||
566 | static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store); | |
567 | ||
07697461 JN |
568 | static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev, |
569 | int value) | |
570 | { | |
571 | int status = 0; | |
572 | ||
573 | if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value) | |
574 | return 0; | |
575 | ||
576 | if (value) | |
577 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); | |
578 | else | |
579 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); | |
580 | ||
581 | /* reconfigure poll(2) support if enabled on one edge only */ | |
582 | if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^ | |
583 | !!test_bit(FLAG_TRIG_FALL, &desc->flags))) { | |
584 | unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK; | |
585 | ||
586 | gpio_setup_irq(desc, dev, 0); | |
587 | status = gpio_setup_irq(desc, dev, trigger_flags); | |
588 | } | |
589 | ||
590 | return status; | |
591 | } | |
592 | ||
593 | static ssize_t gpio_active_low_show(struct device *dev, | |
594 | struct device_attribute *attr, char *buf) | |
595 | { | |
596 | const struct gpio_desc *desc = dev_get_drvdata(dev); | |
597 | ssize_t status; | |
598 | ||
599 | mutex_lock(&sysfs_lock); | |
600 | ||
601 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
602 | status = -EIO; | |
603 | else | |
604 | status = sprintf(buf, "%d\n", | |
605 | !!test_bit(FLAG_ACTIVE_LOW, &desc->flags)); | |
606 | ||
607 | mutex_unlock(&sysfs_lock); | |
608 | ||
609 | return status; | |
610 | } | |
611 | ||
612 | static ssize_t gpio_active_low_store(struct device *dev, | |
613 | struct device_attribute *attr, const char *buf, size_t size) | |
614 | { | |
615 | struct gpio_desc *desc = dev_get_drvdata(dev); | |
616 | ssize_t status; | |
617 | ||
618 | mutex_lock(&sysfs_lock); | |
619 | ||
620 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { | |
621 | status = -EIO; | |
622 | } else { | |
623 | long value; | |
624 | ||
a3d88c92 | 625 | status = kstrtol(buf, 0, &value); |
07697461 JN |
626 | if (status == 0) |
627 | status = sysfs_set_active_low(desc, dev, value != 0); | |
628 | } | |
629 | ||
630 | mutex_unlock(&sysfs_lock); | |
631 | ||
632 | return status ? : size; | |
633 | } | |
634 | ||
635 | static const DEVICE_ATTR(active_low, 0644, | |
636 | gpio_active_low_show, gpio_active_low_store); | |
637 | ||
d8f388d8 | 638 | static const struct attribute *gpio_attrs[] = { |
d8f388d8 | 639 | &dev_attr_value.attr, |
07697461 | 640 | &dev_attr_active_low.attr, |
d8f388d8 DB |
641 | NULL, |
642 | }; | |
643 | ||
644 | static const struct attribute_group gpio_attr_group = { | |
645 | .attrs = (struct attribute **) gpio_attrs, | |
646 | }; | |
647 | ||
648 | /* | |
649 | * /sys/class/gpio/gpiochipN/ | |
650 | * /base ... matching gpio_chip.base (N) | |
651 | * /label ... matching gpio_chip.label | |
652 | * /ngpio ... matching gpio_chip.ngpio | |
653 | */ | |
654 | ||
655 | static ssize_t chip_base_show(struct device *dev, | |
656 | struct device_attribute *attr, char *buf) | |
657 | { | |
658 | const struct gpio_chip *chip = dev_get_drvdata(dev); | |
659 | ||
660 | return sprintf(buf, "%d\n", chip->base); | |
661 | } | |
662 | static DEVICE_ATTR(base, 0444, chip_base_show, NULL); | |
663 | ||
664 | static ssize_t chip_label_show(struct device *dev, | |
665 | struct device_attribute *attr, char *buf) | |
666 | { | |
667 | const struct gpio_chip *chip = dev_get_drvdata(dev); | |
668 | ||
669 | return sprintf(buf, "%s\n", chip->label ? : ""); | |
670 | } | |
671 | static DEVICE_ATTR(label, 0444, chip_label_show, NULL); | |
672 | ||
673 | static ssize_t chip_ngpio_show(struct device *dev, | |
674 | struct device_attribute *attr, char *buf) | |
675 | { | |
676 | const struct gpio_chip *chip = dev_get_drvdata(dev); | |
677 | ||
678 | return sprintf(buf, "%u\n", chip->ngpio); | |
679 | } | |
680 | static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL); | |
681 | ||
682 | static const struct attribute *gpiochip_attrs[] = { | |
683 | &dev_attr_base.attr, | |
684 | &dev_attr_label.attr, | |
685 | &dev_attr_ngpio.attr, | |
686 | NULL, | |
687 | }; | |
688 | ||
689 | static const struct attribute_group gpiochip_attr_group = { | |
690 | .attrs = (struct attribute **) gpiochip_attrs, | |
691 | }; | |
692 | ||
693 | /* | |
694 | * /sys/class/gpio/export ... write-only | |
695 | * integer N ... number of GPIO to export (full access) | |
696 | * /sys/class/gpio/unexport ... write-only | |
697 | * integer N ... number of GPIO to unexport | |
698 | */ | |
28812fe1 AK |
699 | static ssize_t export_store(struct class *class, |
700 | struct class_attribute *attr, | |
701 | const char *buf, size_t len) | |
d8f388d8 | 702 | { |
372e722e AC |
703 | long gpio; |
704 | struct gpio_desc *desc; | |
705 | int status; | |
d8f388d8 | 706 | |
a3d88c92 | 707 | status = kstrtol(buf, 0, &gpio); |
d8f388d8 DB |
708 | if (status < 0) |
709 | goto done; | |
710 | ||
372e722e | 711 | desc = gpio_to_desc(gpio); |
bcabdef1 AC |
712 | /* reject invalid GPIOs */ |
713 | if (!desc) { | |
714 | pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); | |
715 | return -EINVAL; | |
716 | } | |
372e722e | 717 | |
d8f388d8 DB |
718 | /* No extra locking here; FLAG_SYSFS just signifies that the |
719 | * request and export were done by on behalf of userspace, so | |
720 | * they may be undone on its behalf too. | |
721 | */ | |
722 | ||
372e722e | 723 | status = gpiod_request(desc, "sysfs"); |
ad2fab36 MN |
724 | if (status < 0) { |
725 | if (status == -EPROBE_DEFER) | |
726 | status = -ENODEV; | |
d8f388d8 | 727 | goto done; |
ad2fab36 | 728 | } |
372e722e | 729 | status = gpiod_export(desc, true); |
d8f388d8 | 730 | if (status < 0) |
372e722e | 731 | gpiod_free(desc); |
d8f388d8 | 732 | else |
372e722e | 733 | set_bit(FLAG_SYSFS, &desc->flags); |
d8f388d8 DB |
734 | |
735 | done: | |
736 | if (status) | |
737 | pr_debug("%s: status %d\n", __func__, status); | |
738 | return status ? : len; | |
739 | } | |
740 | ||
28812fe1 AK |
741 | static ssize_t unexport_store(struct class *class, |
742 | struct class_attribute *attr, | |
743 | const char *buf, size_t len) | |
d8f388d8 | 744 | { |
372e722e AC |
745 | long gpio; |
746 | struct gpio_desc *desc; | |
747 | int status; | |
d8f388d8 | 748 | |
a3d88c92 | 749 | status = kstrtol(buf, 0, &gpio); |
d8f388d8 DB |
750 | if (status < 0) |
751 | goto done; | |
752 | ||
372e722e | 753 | desc = gpio_to_desc(gpio); |
d8f388d8 | 754 | /* reject bogus commands (gpio_unexport ignores them) */ |
bcabdef1 AC |
755 | if (!desc) { |
756 | pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); | |
757 | return -EINVAL; | |
758 | } | |
759 | ||
760 | status = -EINVAL; | |
d8f388d8 DB |
761 | |
762 | /* No extra locking here; FLAG_SYSFS just signifies that the | |
763 | * request and export were done by on behalf of userspace, so | |
764 | * they may be undone on its behalf too. | |
765 | */ | |
372e722e | 766 | if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) { |
d8f388d8 | 767 | status = 0; |
372e722e | 768 | gpiod_free(desc); |
d8f388d8 DB |
769 | } |
770 | done: | |
771 | if (status) | |
772 | pr_debug("%s: status %d\n", __func__, status); | |
773 | return status ? : len; | |
774 | } | |
775 | ||
776 | static struct class_attribute gpio_class_attrs[] = { | |
777 | __ATTR(export, 0200, NULL, export_store), | |
778 | __ATTR(unexport, 0200, NULL, unexport_store), | |
779 | __ATTR_NULL, | |
780 | }; | |
781 | ||
782 | static struct class gpio_class = { | |
783 | .name = "gpio", | |
784 | .owner = THIS_MODULE, | |
785 | ||
786 | .class_attrs = gpio_class_attrs, | |
787 | }; | |
788 | ||
789 | ||
790 | /** | |
79a9becd | 791 | * gpiod_export - export a GPIO through sysfs |
d8f388d8 DB |
792 | * @gpio: gpio to make available, already requested |
793 | * @direction_may_change: true if userspace may change gpio direction | |
794 | * Context: arch_initcall or later | |
795 | * | |
796 | * When drivers want to make a GPIO accessible to userspace after they | |
797 | * have requested it -- perhaps while debugging, or as part of their | |
798 | * public interface -- they may use this routine. If the GPIO can | |
799 | * change direction (some can't) and the caller allows it, userspace | |
800 | * will see "direction" sysfs attribute which may be used to change | |
801 | * the gpio's direction. A "value" attribute will always be provided. | |
802 | * | |
803 | * Returns zero on success, else an error. | |
804 | */ | |
79a9becd | 805 | int gpiod_export(struct gpio_desc *desc, bool direction_may_change) |
d8f388d8 DB |
806 | { |
807 | unsigned long flags; | |
fc4e2514 | 808 | int status; |
62154991 | 809 | const char *ioname = NULL; |
fc4e2514 | 810 | struct device *dev; |
372e722e | 811 | int offset; |
d8f388d8 DB |
812 | |
813 | /* can't export until sysfs is available ... */ | |
814 | if (!gpio_class.p) { | |
815 | pr_debug("%s: called too early!\n", __func__); | |
816 | return -ENOENT; | |
817 | } | |
818 | ||
372e722e AC |
819 | if (!desc) { |
820 | pr_debug("%s: invalid gpio descriptor\n", __func__); | |
fc4e2514 RM |
821 | return -EINVAL; |
822 | } | |
d8f388d8 DB |
823 | |
824 | mutex_lock(&sysfs_lock); | |
825 | ||
826 | spin_lock_irqsave(&gpio_lock, flags); | |
fc4e2514 RM |
827 | if (!test_bit(FLAG_REQUESTED, &desc->flags) || |
828 | test_bit(FLAG_EXPORT, &desc->flags)) { | |
829 | spin_unlock_irqrestore(&gpio_lock, flags); | |
7589e59f AS |
830 | gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n", |
831 | __func__, | |
fc4e2514 RM |
832 | test_bit(FLAG_REQUESTED, &desc->flags), |
833 | test_bit(FLAG_EXPORT, &desc->flags)); | |
529f2ad5 DC |
834 | status = -EPERM; |
835 | goto fail_unlock; | |
d8f388d8 | 836 | } |
fc4e2514 RM |
837 | |
838 | if (!desc->chip->direction_input || !desc->chip->direction_output) | |
839 | direction_may_change = false; | |
d8f388d8 DB |
840 | spin_unlock_irqrestore(&gpio_lock, flags); |
841 | ||
372e722e AC |
842 | offset = gpio_chip_hwgpio(desc); |
843 | if (desc->chip->names && desc->chip->names[offset]) | |
844 | ioname = desc->chip->names[offset]; | |
926b663c | 845 | |
fc4e2514 | 846 | dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0), |
372e722e AC |
847 | desc, ioname ? ioname : "gpio%u", |
848 | desc_to_gpio(desc)); | |
fc4e2514 RM |
849 | if (IS_ERR(dev)) { |
850 | status = PTR_ERR(dev); | |
851 | goto fail_unlock; | |
d8f388d8 DB |
852 | } |
853 | ||
fc4e2514 | 854 | status = sysfs_create_group(&dev->kobj, &gpio_attr_group); |
d8f388d8 | 855 | if (status) |
fc4e2514 | 856 | goto fail_unregister_device; |
d8f388d8 | 857 | |
fc4e2514 RM |
858 | if (direction_may_change) { |
859 | status = device_create_file(dev, &dev_attr_direction); | |
860 | if (status) | |
861 | goto fail_unregister_device; | |
862 | } | |
863 | ||
372e722e | 864 | if (gpiod_to_irq(desc) >= 0 && (direction_may_change || |
fc4e2514 RM |
865 | !test_bit(FLAG_IS_OUT, &desc->flags))) { |
866 | status = device_create_file(dev, &dev_attr_edge); | |
867 | if (status) | |
868 | goto fail_unregister_device; | |
869 | } | |
d8f388d8 | 870 | |
fc4e2514 RM |
871 | set_bit(FLAG_EXPORT, &desc->flags); |
872 | mutex_unlock(&sysfs_lock); | |
873 | return 0; | |
874 | ||
875 | fail_unregister_device: | |
876 | device_unregister(dev); | |
877 | fail_unlock: | |
878 | mutex_unlock(&sysfs_lock); | |
7589e59f | 879 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
d8f388d8 DB |
880 | return status; |
881 | } | |
79a9becd | 882 | EXPORT_SYMBOL_GPL(gpiod_export); |
d8f388d8 | 883 | |
9f3b795a | 884 | static int match_export(struct device *dev, const void *data) |
d8f388d8 DB |
885 | { |
886 | return dev_get_drvdata(dev) == data; | |
887 | } | |
888 | ||
a4177ee7 | 889 | /** |
79a9becd | 890 | * gpiod_export_link - create a sysfs link to an exported GPIO node |
a4177ee7 JN |
891 | * @dev: device under which to create symlink |
892 | * @name: name of the symlink | |
893 | * @gpio: gpio to create symlink to, already exported | |
894 | * | |
895 | * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN | |
896 | * node. Caller is responsible for unlinking. | |
897 | * | |
898 | * Returns zero on success, else an error. | |
899 | */ | |
79a9becd AC |
900 | int gpiod_export_link(struct device *dev, const char *name, |
901 | struct gpio_desc *desc) | |
a4177ee7 | 902 | { |
a4177ee7 JN |
903 | int status = -EINVAL; |
904 | ||
bcabdef1 AC |
905 | if (!desc) { |
906 | pr_warn("%s: invalid GPIO\n", __func__); | |
907 | return -EINVAL; | |
908 | } | |
a4177ee7 JN |
909 | |
910 | mutex_lock(&sysfs_lock); | |
911 | ||
a4177ee7 JN |
912 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
913 | struct device *tdev; | |
914 | ||
915 | tdev = class_find_device(&gpio_class, NULL, desc, match_export); | |
916 | if (tdev != NULL) { | |
917 | status = sysfs_create_link(&dev->kobj, &tdev->kobj, | |
918 | name); | |
919 | } else { | |
920 | status = -ENODEV; | |
921 | } | |
922 | } | |
923 | ||
924 | mutex_unlock(&sysfs_lock); | |
925 | ||
a4177ee7 | 926 | if (status) |
7589e59f | 927 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
a4177ee7 JN |
928 | |
929 | return status; | |
930 | } | |
79a9becd | 931 | EXPORT_SYMBOL_GPL(gpiod_export_link); |
07697461 JN |
932 | |
933 | /** | |
79a9becd | 934 | * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value |
07697461 JN |
935 | * @gpio: gpio to change |
936 | * @value: non-zero to use active low, i.e. inverted values | |
937 | * | |
938 | * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute. | |
939 | * The GPIO does not have to be exported yet. If poll(2) support has | |
940 | * been enabled for either rising or falling edge, it will be | |
941 | * reconfigured to follow the new polarity. | |
942 | * | |
943 | * Returns zero on success, else an error. | |
944 | */ | |
79a9becd | 945 | int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) |
07697461 | 946 | { |
07697461 JN |
947 | struct device *dev = NULL; |
948 | int status = -EINVAL; | |
949 | ||
bcabdef1 AC |
950 | if (!desc) { |
951 | pr_warn("%s: invalid GPIO\n", __func__); | |
952 | return -EINVAL; | |
953 | } | |
07697461 JN |
954 | |
955 | mutex_lock(&sysfs_lock); | |
956 | ||
07697461 | 957 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
07697461 JN |
958 | dev = class_find_device(&gpio_class, NULL, desc, match_export); |
959 | if (dev == NULL) { | |
960 | status = -ENODEV; | |
961 | goto unlock; | |
962 | } | |
963 | } | |
964 | ||
965 | status = sysfs_set_active_low(desc, dev, value); | |
966 | ||
967 | unlock: | |
968 | mutex_unlock(&sysfs_lock); | |
969 | ||
07697461 | 970 | if (status) |
7589e59f | 971 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
07697461 JN |
972 | |
973 | return status; | |
974 | } | |
79a9becd | 975 | EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low); |
07697461 | 976 | |
d8f388d8 | 977 | /** |
79a9becd | 978 | * gpiod_unexport - reverse effect of gpio_export() |
d8f388d8 DB |
979 | * @gpio: gpio to make unavailable |
980 | * | |
981 | * This is implicit on gpio_free(). | |
982 | */ | |
79a9becd | 983 | void gpiod_unexport(struct gpio_desc *desc) |
d8f388d8 | 984 | { |
6a99ad4a | 985 | int status = 0; |
864533ce | 986 | struct device *dev = NULL; |
d8f388d8 | 987 | |
372e722e | 988 | if (!desc) { |
bcabdef1 AC |
989 | pr_warn("%s: invalid GPIO\n", __func__); |
990 | return; | |
6a99ad4a | 991 | } |
d8f388d8 DB |
992 | |
993 | mutex_lock(&sysfs_lock); | |
994 | ||
d8f388d8 | 995 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
d8f388d8 DB |
996 | |
997 | dev = class_find_device(&gpio_class, NULL, desc, match_export); | |
998 | if (dev) { | |
ff77c352 | 999 | gpio_setup_irq(desc, dev, 0); |
d8f388d8 | 1000 | clear_bit(FLAG_EXPORT, &desc->flags); |
d8f388d8 DB |
1001 | } else |
1002 | status = -ENODEV; | |
1003 | } | |
1004 | ||
1005 | mutex_unlock(&sysfs_lock); | |
372e722e | 1006 | |
864533ce ML |
1007 | if (dev) { |
1008 | device_unregister(dev); | |
1009 | put_device(dev); | |
1010 | } | |
bcabdef1 | 1011 | |
d8f388d8 | 1012 | if (status) |
7589e59f | 1013 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
372e722e | 1014 | } |
79a9becd | 1015 | EXPORT_SYMBOL_GPL(gpiod_unexport); |
d8f388d8 DB |
1016 | |
1017 | static int gpiochip_export(struct gpio_chip *chip) | |
1018 | { | |
1019 | int status; | |
1020 | struct device *dev; | |
1021 | ||
1022 | /* Many systems register gpio chips for SOC support very early, | |
1023 | * before driver model support is available. In those cases we | |
1024 | * export this later, in gpiolib_sysfs_init() ... here we just | |
1025 | * verify that _some_ field of gpio_class got initialized. | |
1026 | */ | |
1027 | if (!gpio_class.p) | |
1028 | return 0; | |
1029 | ||
1030 | /* use chip->base for the ID; it's already known to be unique */ | |
1031 | mutex_lock(&sysfs_lock); | |
1032 | dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip, | |
1033 | "gpiochip%d", chip->base); | |
d62668e1 | 1034 | if (!IS_ERR(dev)) { |
d8f388d8 DB |
1035 | status = sysfs_create_group(&dev->kobj, |
1036 | &gpiochip_attr_group); | |
1037 | } else | |
d62668e1 | 1038 | status = PTR_ERR(dev); |
d8f388d8 DB |
1039 | chip->exported = (status == 0); |
1040 | mutex_unlock(&sysfs_lock); | |
1041 | ||
1042 | if (status) { | |
1043 | unsigned long flags; | |
1044 | unsigned gpio; | |
1045 | ||
1046 | spin_lock_irqsave(&gpio_lock, flags); | |
6c0b4e6c AC |
1047 | gpio = 0; |
1048 | while (gpio < chip->ngpio) | |
1049 | chip->desc[gpio++].chip = NULL; | |
d8f388d8 DB |
1050 | spin_unlock_irqrestore(&gpio_lock, flags); |
1051 | ||
1a2a99c6 | 1052 | chip_dbg(chip, "%s: status %d\n", __func__, status); |
d8f388d8 DB |
1053 | } |
1054 | ||
1055 | return status; | |
1056 | } | |
1057 | ||
1058 | static void gpiochip_unexport(struct gpio_chip *chip) | |
1059 | { | |
1060 | int status; | |
1061 | struct device *dev; | |
1062 | ||
1063 | mutex_lock(&sysfs_lock); | |
1064 | dev = class_find_device(&gpio_class, NULL, chip, match_export); | |
1065 | if (dev) { | |
1066 | put_device(dev); | |
1067 | device_unregister(dev); | |
9fb1f39e | 1068 | chip->exported = false; |
d8f388d8 DB |
1069 | status = 0; |
1070 | } else | |
1071 | status = -ENODEV; | |
1072 | mutex_unlock(&sysfs_lock); | |
1073 | ||
1074 | if (status) | |
1a2a99c6 | 1075 | chip_dbg(chip, "%s: status %d\n", __func__, status); |
d8f388d8 DB |
1076 | } |
1077 | ||
1078 | static int __init gpiolib_sysfs_init(void) | |
1079 | { | |
1080 | int status; | |
1081 | unsigned long flags; | |
65493e3a | 1082 | struct gpio_chip *chip; |
d8f388d8 DB |
1083 | |
1084 | status = class_register(&gpio_class); | |
1085 | if (status < 0) | |
1086 | return status; | |
1087 | ||
1088 | /* Scan and register the gpio_chips which registered very | |
1089 | * early (e.g. before the class_register above was called). | |
1090 | * | |
1091 | * We run before arch_initcall() so chip->dev nodes can have | |
1092 | * registered, and so arch_initcall() can always gpio_export(). | |
1093 | */ | |
1094 | spin_lock_irqsave(&gpio_lock, flags); | |
65493e3a | 1095 | list_for_each_entry(chip, &gpio_chips, list) { |
d8f388d8 DB |
1096 | if (!chip || chip->exported) |
1097 | continue; | |
1098 | ||
1099 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1100 | status = gpiochip_export(chip); | |
1101 | spin_lock_irqsave(&gpio_lock, flags); | |
1102 | } | |
1103 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1104 | ||
1105 | ||
1106 | return status; | |
1107 | } | |
1108 | postcore_initcall(gpiolib_sysfs_init); | |
1109 | ||
1110 | #else | |
1111 | static inline int gpiochip_export(struct gpio_chip *chip) | |
1112 | { | |
1113 | return 0; | |
1114 | } | |
1115 | ||
1116 | static inline void gpiochip_unexport(struct gpio_chip *chip) | |
1117 | { | |
1118 | } | |
1119 | ||
1120 | #endif /* CONFIG_GPIO_SYSFS */ | |
1121 | ||
1a989d0f AC |
1122 | /* |
1123 | * Add a new chip to the global chips list, keeping the list of chips sorted | |
1124 | * by base order. | |
1125 | * | |
1126 | * Return -EBUSY if the new chip overlaps with some other chip's integer | |
1127 | * space. | |
1128 | */ | |
1129 | static int gpiochip_add_to_list(struct gpio_chip *chip) | |
1130 | { | |
1131 | struct list_head *pos = &gpio_chips; | |
1132 | struct gpio_chip *_chip; | |
1133 | int err = 0; | |
1134 | ||
1135 | /* find where to insert our chip */ | |
1136 | list_for_each(pos, &gpio_chips) { | |
1137 | _chip = list_entry(pos, struct gpio_chip, list); | |
1138 | /* shall we insert before _chip? */ | |
1139 | if (_chip->base >= chip->base + chip->ngpio) | |
1140 | break; | |
1141 | } | |
1142 | ||
1143 | /* are we stepping on the chip right before? */ | |
1144 | if (pos != &gpio_chips && pos->prev != &gpio_chips) { | |
1145 | _chip = list_entry(pos->prev, struct gpio_chip, list); | |
1146 | if (_chip->base + _chip->ngpio > chip->base) { | |
1147 | dev_err(chip->dev, | |
1148 | "GPIO integer space overlap, cannot add chip\n"); | |
1149 | err = -EBUSY; | |
1150 | } | |
1151 | } | |
1152 | ||
1153 | if (!err) | |
1154 | list_add_tail(&chip->list, pos); | |
1155 | ||
1156 | return err; | |
1157 | } | |
1158 | ||
d2876d08 DB |
1159 | /** |
1160 | * gpiochip_add() - register a gpio_chip | |
1161 | * @chip: the chip to register, with chip->base initialized | |
1162 | * Context: potentially before irqs or kmalloc will work | |
1163 | * | |
1164 | * Returns a negative errno if the chip can't be registered, such as | |
1165 | * because the chip->base is invalid or already associated with a | |
1166 | * different chip. Otherwise it returns zero as a success code. | |
8d0aab2f | 1167 | * |
d8f388d8 DB |
1168 | * When gpiochip_add() is called very early during boot, so that GPIOs |
1169 | * can be freely used, the chip->dev device must be registered before | |
1170 | * the gpio framework's arch_initcall(). Otherwise sysfs initialization | |
1171 | * for GPIOs will fail rudely. | |
1172 | * | |
8d0aab2f AV |
1173 | * If chip->base is negative, this requests dynamic assignment of |
1174 | * a range of valid GPIOs. | |
d2876d08 DB |
1175 | */ |
1176 | int gpiochip_add(struct gpio_chip *chip) | |
1177 | { | |
1178 | unsigned long flags; | |
1179 | int status = 0; | |
1180 | unsigned id; | |
8d0aab2f | 1181 | int base = chip->base; |
d2876d08 | 1182 | |
bff5fda9 | 1183 | if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1)) |
8d0aab2f | 1184 | && base >= 0) { |
d2876d08 DB |
1185 | status = -EINVAL; |
1186 | goto fail; | |
1187 | } | |
1188 | ||
1189 | spin_lock_irqsave(&gpio_lock, flags); | |
1190 | ||
8d0aab2f AV |
1191 | if (base < 0) { |
1192 | base = gpiochip_find_base(chip->ngpio); | |
1193 | if (base < 0) { | |
1194 | status = base; | |
d8f388d8 | 1195 | goto unlock; |
8d0aab2f AV |
1196 | } |
1197 | chip->base = base; | |
1198 | } | |
1199 | ||
1a989d0f AC |
1200 | status = gpiochip_add_to_list(chip); |
1201 | ||
d2876d08 | 1202 | if (status == 0) { |
6c0b4e6c AC |
1203 | chip->desc = &gpio_desc[chip->base]; |
1204 | ||
1205 | for (id = 0; id < chip->ngpio; id++) { | |
1206 | struct gpio_desc *desc = &chip->desc[id]; | |
1207 | desc->chip = chip; | |
d8f388d8 DB |
1208 | |
1209 | /* REVISIT: most hardware initializes GPIOs as | |
1210 | * inputs (often with pullups enabled) so power | |
1211 | * usage is minimized. Linux code should set the | |
1212 | * gpio direction first thing; but until it does, | |
80b0a602 | 1213 | * and in case chip->get_direction is not set, |
d8f388d8 DB |
1214 | * we may expose the wrong direction in sysfs. |
1215 | */ | |
6c0b4e6c | 1216 | desc->flags = !chip->direction_input |
d8f388d8 DB |
1217 | ? (1 << FLAG_IS_OUT) |
1218 | : 0; | |
d2876d08 DB |
1219 | } |
1220 | } | |
1221 | ||
3bae4811 ZG |
1222 | spin_unlock_irqrestore(&gpio_lock, flags); |
1223 | ||
f23f1516 SH |
1224 | #ifdef CONFIG_PINCTRL |
1225 | INIT_LIST_HEAD(&chip->pin_ranges); | |
1226 | #endif | |
1227 | ||
391c970c AV |
1228 | of_gpiochip_add(chip); |
1229 | ||
cedb1881 AV |
1230 | if (status) |
1231 | goto fail; | |
1232 | ||
1233 | status = gpiochip_export(chip); | |
1234 | if (status) | |
1235 | goto fail; | |
1236 | ||
7589e59f | 1237 | pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__, |
64842aad GL |
1238 | chip->base, chip->base + chip->ngpio - 1, |
1239 | chip->label ? : "generic"); | |
1240 | ||
cedb1881 | 1241 | return 0; |
3bae4811 ZG |
1242 | |
1243 | unlock: | |
1244 | spin_unlock_irqrestore(&gpio_lock, flags); | |
d2876d08 DB |
1245 | fail: |
1246 | /* failures here can mean systems won't boot... */ | |
7589e59f | 1247 | pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__, |
cedb1881 AV |
1248 | chip->base, chip->base + chip->ngpio - 1, |
1249 | chip->label ? : "generic"); | |
d2876d08 DB |
1250 | return status; |
1251 | } | |
1252 | EXPORT_SYMBOL_GPL(gpiochip_add); | |
1253 | ||
1254 | /** | |
1255 | * gpiochip_remove() - unregister a gpio_chip | |
1256 | * @chip: the chip to unregister | |
1257 | * | |
1258 | * A gpio_chip with any GPIOs still requested may not be removed. | |
1259 | */ | |
1260 | int gpiochip_remove(struct gpio_chip *chip) | |
1261 | { | |
1262 | unsigned long flags; | |
1263 | int status = 0; | |
1264 | unsigned id; | |
1265 | ||
1266 | spin_lock_irqsave(&gpio_lock, flags); | |
1267 | ||
9ef0d6f7 | 1268 | gpiochip_remove_pin_ranges(chip); |
391c970c AV |
1269 | of_gpiochip_remove(chip); |
1270 | ||
6c0b4e6c AC |
1271 | for (id = 0; id < chip->ngpio; id++) { |
1272 | if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) { | |
d2876d08 DB |
1273 | status = -EBUSY; |
1274 | break; | |
1275 | } | |
1276 | } | |
1277 | if (status == 0) { | |
6c0b4e6c AC |
1278 | for (id = 0; id < chip->ngpio; id++) |
1279 | chip->desc[id].chip = NULL; | |
1a989d0f AC |
1280 | |
1281 | list_del(&chip->list); | |
d2876d08 DB |
1282 | } |
1283 | ||
1284 | spin_unlock_irqrestore(&gpio_lock, flags); | |
d8f388d8 DB |
1285 | |
1286 | if (status == 0) | |
1287 | gpiochip_unexport(chip); | |
1288 | ||
d2876d08 DB |
1289 | return status; |
1290 | } | |
1291 | EXPORT_SYMBOL_GPL(gpiochip_remove); | |
1292 | ||
594fa265 GL |
1293 | /** |
1294 | * gpiochip_find() - iterator for locating a specific gpio_chip | |
1295 | * @data: data to pass to match function | |
1296 | * @callback: Callback function to check gpio_chip | |
1297 | * | |
1298 | * Similar to bus_find_device. It returns a reference to a gpio_chip as | |
1299 | * determined by a user supplied @match callback. The callback should return | |
1300 | * 0 if the device doesn't match and non-zero if it does. If the callback is | |
1301 | * non-zero, this function will return to the caller and not iterate over any | |
1302 | * more gpio_chips. | |
1303 | */ | |
07ce8ec7 | 1304 | struct gpio_chip *gpiochip_find(void *data, |
6e2cf651 | 1305 | int (*match)(struct gpio_chip *chip, |
3d0f7cf0 | 1306 | void *data)) |
594fa265 | 1307 | { |
125eef96 | 1308 | struct gpio_chip *chip; |
594fa265 | 1309 | unsigned long flags; |
594fa265 GL |
1310 | |
1311 | spin_lock_irqsave(&gpio_lock, flags); | |
125eef96 AC |
1312 | list_for_each_entry(chip, &gpio_chips, list) |
1313 | if (match(chip, data)) | |
594fa265 | 1314 | break; |
125eef96 AC |
1315 | |
1316 | /* No match? */ | |
1317 | if (&chip->list == &gpio_chips) | |
1318 | chip = NULL; | |
594fa265 GL |
1319 | spin_unlock_irqrestore(&gpio_lock, flags); |
1320 | ||
1321 | return chip; | |
1322 | } | |
8fa0c9bf | 1323 | EXPORT_SYMBOL_GPL(gpiochip_find); |
d2876d08 | 1324 | |
79697ef9 AC |
1325 | static int gpiochip_match_name(struct gpio_chip *chip, void *data) |
1326 | { | |
1327 | const char *name = data; | |
1328 | ||
1329 | return !strcmp(chip->label, name); | |
1330 | } | |
1331 | ||
1332 | static struct gpio_chip *find_chip_by_name(const char *name) | |
1333 | { | |
1334 | return gpiochip_find((void *)name, gpiochip_match_name); | |
1335 | } | |
1336 | ||
f23f1516 | 1337 | #ifdef CONFIG_PINCTRL |
165adc9c | 1338 | |
586a87e6 CR |
1339 | /** |
1340 | * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping | |
1341 | * @chip: the gpiochip to add the range for | |
1342 | * @pinctrl: the dev_name() of the pin controller to map to | |
1343 | * @gpio_offset: the start offset in the current gpio_chip number space | |
1344 | * @pin_group: name of the pin group inside the pin controller | |
1345 | */ | |
1346 | int gpiochip_add_pingroup_range(struct gpio_chip *chip, | |
1347 | struct pinctrl_dev *pctldev, | |
1348 | unsigned int gpio_offset, const char *pin_group) | |
1349 | { | |
1350 | struct gpio_pin_range *pin_range; | |
1351 | int ret; | |
1352 | ||
1353 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); | |
1354 | if (!pin_range) { | |
1a2a99c6 | 1355 | chip_err(chip, "failed to allocate pin ranges\n"); |
586a87e6 CR |
1356 | return -ENOMEM; |
1357 | } | |
1358 | ||
1359 | /* Use local offset as range ID */ | |
1360 | pin_range->range.id = gpio_offset; | |
1361 | pin_range->range.gc = chip; | |
1362 | pin_range->range.name = chip->label; | |
1363 | pin_range->range.base = chip->base + gpio_offset; | |
1364 | pin_range->pctldev = pctldev; | |
1365 | ||
1366 | ret = pinctrl_get_group_pins(pctldev, pin_group, | |
1367 | &pin_range->range.pins, | |
1368 | &pin_range->range.npins); | |
61c6375d MN |
1369 | if (ret < 0) { |
1370 | kfree(pin_range); | |
586a87e6 | 1371 | return ret; |
61c6375d | 1372 | } |
586a87e6 CR |
1373 | |
1374 | pinctrl_add_gpio_range(pctldev, &pin_range->range); | |
1375 | ||
1a2a99c6 AS |
1376 | chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n", |
1377 | gpio_offset, gpio_offset + pin_range->range.npins - 1, | |
586a87e6 CR |
1378 | pinctrl_dev_get_devname(pctldev), pin_group); |
1379 | ||
1380 | list_add_tail(&pin_range->node, &chip->pin_ranges); | |
1381 | ||
1382 | return 0; | |
1383 | } | |
1384 | EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range); | |
1385 | ||
3f0f8670 LW |
1386 | /** |
1387 | * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping | |
1388 | * @chip: the gpiochip to add the range for | |
1389 | * @pinctrl_name: the dev_name() of the pin controller to map to | |
316511c0 LW |
1390 | * @gpio_offset: the start offset in the current gpio_chip number space |
1391 | * @pin_offset: the start offset in the pin controller number space | |
3f0f8670 LW |
1392 | * @npins: the number of pins from the offset of each pin space (GPIO and |
1393 | * pin controller) to accumulate in this range | |
1394 | */ | |
1e63d7b9 | 1395 | int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name, |
316511c0 | 1396 | unsigned int gpio_offset, unsigned int pin_offset, |
3f0f8670 | 1397 | unsigned int npins) |
f23f1516 SH |
1398 | { |
1399 | struct gpio_pin_range *pin_range; | |
b4d4b1f0 | 1400 | int ret; |
f23f1516 | 1401 | |
3f0f8670 | 1402 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); |
f23f1516 | 1403 | if (!pin_range) { |
1a2a99c6 | 1404 | chip_err(chip, "failed to allocate pin ranges\n"); |
1e63d7b9 | 1405 | return -ENOMEM; |
f23f1516 SH |
1406 | } |
1407 | ||
3f0f8670 | 1408 | /* Use local offset as range ID */ |
316511c0 | 1409 | pin_range->range.id = gpio_offset; |
3f0f8670 | 1410 | pin_range->range.gc = chip; |
f23f1516 | 1411 | pin_range->range.name = chip->label; |
316511c0 LW |
1412 | pin_range->range.base = chip->base + gpio_offset; |
1413 | pin_range->range.pin_base = pin_offset; | |
f23f1516 | 1414 | pin_range->range.npins = npins; |
192c369c | 1415 | pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name, |
f23f1516 | 1416 | &pin_range->range); |
8f23ca1a | 1417 | if (IS_ERR(pin_range->pctldev)) { |
b4d4b1f0 | 1418 | ret = PTR_ERR(pin_range->pctldev); |
1a2a99c6 | 1419 | chip_err(chip, "could not create pin range\n"); |
3f0f8670 | 1420 | kfree(pin_range); |
b4d4b1f0 | 1421 | return ret; |
3f0f8670 | 1422 | } |
1a2a99c6 AS |
1423 | chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n", |
1424 | gpio_offset, gpio_offset + npins - 1, | |
316511c0 LW |
1425 | pinctl_name, |
1426 | pin_offset, pin_offset + npins - 1); | |
f23f1516 SH |
1427 | |
1428 | list_add_tail(&pin_range->node, &chip->pin_ranges); | |
1e63d7b9 LW |
1429 | |
1430 | return 0; | |
f23f1516 | 1431 | } |
165adc9c | 1432 | EXPORT_SYMBOL_GPL(gpiochip_add_pin_range); |
f23f1516 | 1433 | |
3f0f8670 LW |
1434 | /** |
1435 | * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings | |
1436 | * @chip: the chip to remove all the mappings for | |
1437 | */ | |
f23f1516 SH |
1438 | void gpiochip_remove_pin_ranges(struct gpio_chip *chip) |
1439 | { | |
1440 | struct gpio_pin_range *pin_range, *tmp; | |
1441 | ||
1442 | list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) { | |
1443 | list_del(&pin_range->node); | |
1444 | pinctrl_remove_gpio_range(pin_range->pctldev, | |
1445 | &pin_range->range); | |
3f0f8670 | 1446 | kfree(pin_range); |
f23f1516 SH |
1447 | } |
1448 | } | |
165adc9c LW |
1449 | EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges); |
1450 | ||
1451 | #endif /* CONFIG_PINCTRL */ | |
f23f1516 | 1452 | |
d2876d08 DB |
1453 | /* These "optional" allocation calls help prevent drivers from stomping |
1454 | * on each other, and help provide better diagnostics in debugfs. | |
1455 | * They're called even less than the "set direction" calls. | |
1456 | */ | |
372e722e | 1457 | static int gpiod_request(struct gpio_desc *desc, const char *label) |
d2876d08 | 1458 | { |
35e8bb51 | 1459 | struct gpio_chip *chip; |
e9354576 | 1460 | int status = -EPROBE_DEFER; |
d2876d08 DB |
1461 | unsigned long flags; |
1462 | ||
0204df47 | 1463 | if (!desc) { |
bcabdef1 AC |
1464 | pr_warn("%s: invalid GPIO\n", __func__); |
1465 | return -EINVAL; | |
ad2fab36 | 1466 | } |
bcabdef1 AC |
1467 | |
1468 | spin_lock_irqsave(&gpio_lock, flags); | |
1469 | ||
35e8bb51 | 1470 | chip = desc->chip; |
0204df47 AC |
1471 | if (chip == NULL) |
1472 | goto done; | |
d2876d08 | 1473 | |
35e8bb51 | 1474 | if (!try_module_get(chip->owner)) |
438d8908 GL |
1475 | goto done; |
1476 | ||
d2876d08 | 1477 | /* NOTE: gpio_request() can be called in early boot, |
35e8bb51 | 1478 | * before IRQs are enabled, for non-sleeping (SOC) GPIOs. |
d2876d08 DB |
1479 | */ |
1480 | ||
1481 | if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { | |
1482 | desc_set_label(desc, label ? : "?"); | |
1483 | status = 0; | |
438d8908 | 1484 | } else { |
d2876d08 | 1485 | status = -EBUSY; |
35e8bb51 | 1486 | module_put(chip->owner); |
7460db56 | 1487 | goto done; |
35e8bb51 DB |
1488 | } |
1489 | ||
1490 | if (chip->request) { | |
1491 | /* chip->request may sleep */ | |
1492 | spin_unlock_irqrestore(&gpio_lock, flags); | |
372e722e | 1493 | status = chip->request(chip, gpio_chip_hwgpio(desc)); |
35e8bb51 DB |
1494 | spin_lock_irqsave(&gpio_lock, flags); |
1495 | ||
1496 | if (status < 0) { | |
1497 | desc_set_label(desc, NULL); | |
1498 | module_put(chip->owner); | |
1499 | clear_bit(FLAG_REQUESTED, &desc->flags); | |
80b0a602 | 1500 | goto done; |
35e8bb51 | 1501 | } |
438d8908 | 1502 | } |
80b0a602 MN |
1503 | if (chip->get_direction) { |
1504 | /* chip->get_direction may sleep */ | |
1505 | spin_unlock_irqrestore(&gpio_lock, flags); | |
372e722e | 1506 | gpiod_get_direction(desc); |
80b0a602 MN |
1507 | spin_lock_irqsave(&gpio_lock, flags); |
1508 | } | |
d2876d08 DB |
1509 | done: |
1510 | if (status) | |
7589e59f | 1511 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
d2876d08 DB |
1512 | spin_unlock_irqrestore(&gpio_lock, flags); |
1513 | return status; | |
1514 | } | |
372e722e AC |
1515 | |
1516 | int gpio_request(unsigned gpio, const char *label) | |
1517 | { | |
1518 | return gpiod_request(gpio_to_desc(gpio), label); | |
1519 | } | |
d2876d08 DB |
1520 | EXPORT_SYMBOL_GPL(gpio_request); |
1521 | ||
372e722e | 1522 | static void gpiod_free(struct gpio_desc *desc) |
d2876d08 DB |
1523 | { |
1524 | unsigned long flags; | |
35e8bb51 | 1525 | struct gpio_chip *chip; |
d2876d08 | 1526 | |
3d599d1c UKK |
1527 | might_sleep(); |
1528 | ||
372e722e | 1529 | if (!desc) { |
d2876d08 DB |
1530 | WARN_ON(extra_checks); |
1531 | return; | |
1532 | } | |
1533 | ||
372e722e | 1534 | gpiod_unexport(desc); |
d8f388d8 | 1535 | |
d2876d08 DB |
1536 | spin_lock_irqsave(&gpio_lock, flags); |
1537 | ||
35e8bb51 DB |
1538 | chip = desc->chip; |
1539 | if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) { | |
1540 | if (chip->free) { | |
1541 | spin_unlock_irqrestore(&gpio_lock, flags); | |
9c4ba946 | 1542 | might_sleep_if(chip->can_sleep); |
372e722e | 1543 | chip->free(chip, gpio_chip_hwgpio(desc)); |
35e8bb51 DB |
1544 | spin_lock_irqsave(&gpio_lock, flags); |
1545 | } | |
d2876d08 | 1546 | desc_set_label(desc, NULL); |
438d8908 | 1547 | module_put(desc->chip->owner); |
07697461 | 1548 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); |
35e8bb51 | 1549 | clear_bit(FLAG_REQUESTED, &desc->flags); |
aca5ce14 | 1550 | clear_bit(FLAG_OPEN_DRAIN, &desc->flags); |
25553ff0 | 1551 | clear_bit(FLAG_OPEN_SOURCE, &desc->flags); |
438d8908 | 1552 | } else |
d2876d08 DB |
1553 | WARN_ON(extra_checks); |
1554 | ||
1555 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1556 | } | |
372e722e AC |
1557 | |
1558 | void gpio_free(unsigned gpio) | |
1559 | { | |
1560 | gpiod_free(gpio_to_desc(gpio)); | |
1561 | } | |
d2876d08 DB |
1562 | EXPORT_SYMBOL_GPL(gpio_free); |
1563 | ||
3e45f1d1 EM |
1564 | /** |
1565 | * gpio_request_one - request a single GPIO with initial configuration | |
1566 | * @gpio: the GPIO number | |
1567 | * @flags: GPIO configuration as specified by GPIOF_* | |
1568 | * @label: a literal description string of this GPIO | |
1569 | */ | |
1570 | int gpio_request_one(unsigned gpio, unsigned long flags, const char *label) | |
1571 | { | |
372e722e | 1572 | struct gpio_desc *desc; |
3e45f1d1 EM |
1573 | int err; |
1574 | ||
372e722e AC |
1575 | desc = gpio_to_desc(gpio); |
1576 | ||
1577 | err = gpiod_request(desc, label); | |
3e45f1d1 EM |
1578 | if (err) |
1579 | return err; | |
1580 | ||
aca5ce14 | 1581 | if (flags & GPIOF_OPEN_DRAIN) |
372e722e | 1582 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); |
aca5ce14 | 1583 | |
25553ff0 | 1584 | if (flags & GPIOF_OPEN_SOURCE) |
372e722e | 1585 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); |
25553ff0 | 1586 | |
3e45f1d1 | 1587 | if (flags & GPIOF_DIR_IN) |
372e722e | 1588 | err = gpiod_direction_input(desc); |
3e45f1d1 | 1589 | else |
372e722e | 1590 | err = gpiod_direction_output(desc, |
3e45f1d1 EM |
1591 | (flags & GPIOF_INIT_HIGH) ? 1 : 0); |
1592 | ||
e254811c | 1593 | if (err) |
fc3a1f04 WS |
1594 | goto free_gpio; |
1595 | ||
1596 | if (flags & GPIOF_EXPORT) { | |
372e722e | 1597 | err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE); |
fc3a1f04 WS |
1598 | if (err) |
1599 | goto free_gpio; | |
1600 | } | |
1601 | ||
1602 | return 0; | |
e254811c | 1603 | |
fc3a1f04 | 1604 | free_gpio: |
372e722e | 1605 | gpiod_free(desc); |
3e45f1d1 EM |
1606 | return err; |
1607 | } | |
1608 | EXPORT_SYMBOL_GPL(gpio_request_one); | |
1609 | ||
1610 | /** | |
1611 | * gpio_request_array - request multiple GPIOs in a single call | |
1612 | * @array: array of the 'struct gpio' | |
1613 | * @num: how many GPIOs in the array | |
1614 | */ | |
7c295975 | 1615 | int gpio_request_array(const struct gpio *array, size_t num) |
3e45f1d1 EM |
1616 | { |
1617 | int i, err; | |
1618 | ||
1619 | for (i = 0; i < num; i++, array++) { | |
1620 | err = gpio_request_one(array->gpio, array->flags, array->label); | |
1621 | if (err) | |
1622 | goto err_free; | |
1623 | } | |
1624 | return 0; | |
1625 | ||
1626 | err_free: | |
1627 | while (i--) | |
1628 | gpio_free((--array)->gpio); | |
1629 | return err; | |
1630 | } | |
1631 | EXPORT_SYMBOL_GPL(gpio_request_array); | |
1632 | ||
1633 | /** | |
1634 | * gpio_free_array - release multiple GPIOs in a single call | |
1635 | * @array: array of the 'struct gpio' | |
1636 | * @num: how many GPIOs in the array | |
1637 | */ | |
7c295975 | 1638 | void gpio_free_array(const struct gpio *array, size_t num) |
3e45f1d1 EM |
1639 | { |
1640 | while (num--) | |
1641 | gpio_free((array++)->gpio); | |
1642 | } | |
1643 | EXPORT_SYMBOL_GPL(gpio_free_array); | |
d2876d08 DB |
1644 | |
1645 | /** | |
1646 | * gpiochip_is_requested - return string iff signal was requested | |
1647 | * @chip: controller managing the signal | |
1648 | * @offset: of signal within controller's 0..(ngpio - 1) range | |
1649 | * | |
1650 | * Returns NULL if the GPIO is not currently requested, else a string. | |
1651 | * If debugfs support is enabled, the string returned is the label passed | |
1652 | * to gpio_request(); otherwise it is a meaningless constant. | |
1653 | * | |
1654 | * This function is for use by GPIO controller drivers. The label can | |
1655 | * help with diagnostics, and knowing that the signal is used as a GPIO | |
1656 | * can help avoid accidentally multiplexing it to another controller. | |
1657 | */ | |
1658 | const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset) | |
1659 | { | |
6c0b4e6c | 1660 | struct gpio_desc *desc; |
d2876d08 | 1661 | |
6c0b4e6c | 1662 | if (!GPIO_OFFSET_VALID(chip, offset)) |
d2876d08 | 1663 | return NULL; |
6c0b4e6c AC |
1664 | |
1665 | desc = &chip->desc[offset]; | |
1666 | ||
372e722e | 1667 | if (test_bit(FLAG_REQUESTED, &desc->flags) == 0) |
d2876d08 DB |
1668 | return NULL; |
1669 | #ifdef CONFIG_DEBUG_FS | |
372e722e | 1670 | return desc->label; |
d2876d08 DB |
1671 | #else |
1672 | return "?"; | |
1673 | #endif | |
1674 | } | |
1675 | EXPORT_SYMBOL_GPL(gpiochip_is_requested); | |
1676 | ||
1677 | ||
1678 | /* Drivers MUST set GPIO direction before making get/set calls. In | |
1679 | * some cases this is done in early boot, before IRQs are enabled. | |
1680 | * | |
1681 | * As a rule these aren't called more than once (except for drivers | |
1682 | * using the open-drain emulation idiom) so these are natural places | |
1683 | * to accumulate extra debugging checks. Note that we can't (yet) | |
1684 | * rely on gpio_request() having been called beforehand. | |
1685 | */ | |
1686 | ||
79a9becd AC |
1687 | /** |
1688 | * gpiod_direction_input - set the GPIO direction to input | |
1689 | * @desc: GPIO to set to input | |
1690 | * | |
1691 | * Set the direction of the passed GPIO to input, such as gpiod_get_value() can | |
1692 | * be called safely on it. | |
1693 | * | |
1694 | * Return 0 in case of success, else an error code. | |
1695 | */ | |
1696 | int gpiod_direction_input(struct gpio_desc *desc) | |
d2876d08 DB |
1697 | { |
1698 | unsigned long flags; | |
1699 | struct gpio_chip *chip; | |
d2876d08 | 1700 | int status = -EINVAL; |
372e722e | 1701 | int offset; |
d2876d08 | 1702 | |
be1a4b13 | 1703 | if (!desc || !desc->chip) { |
bcabdef1 AC |
1704 | pr_warn("%s: invalid GPIO\n", __func__); |
1705 | return -EINVAL; | |
1706 | } | |
1707 | ||
be1a4b13 LW |
1708 | chip = desc->chip; |
1709 | if (!chip->get || !chip->direction_input) { | |
6424de5a MB |
1710 | gpiod_warn(desc, |
1711 | "%s: missing get() or direction_input() operations\n", | |
7589e59f | 1712 | __func__); |
be1a4b13 LW |
1713 | return -EIO; |
1714 | } | |
1715 | ||
d2876d08 DB |
1716 | spin_lock_irqsave(&gpio_lock, flags); |
1717 | ||
372e722e | 1718 | status = gpio_ensure_requested(desc); |
35e8bb51 DB |
1719 | if (status < 0) |
1720 | goto fail; | |
d2876d08 DB |
1721 | |
1722 | /* now we know the gpio is valid and chip won't vanish */ | |
1723 | ||
1724 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1725 | ||
9c4ba946 | 1726 | might_sleep_if(chip->can_sleep); |
d2876d08 | 1727 | |
372e722e | 1728 | offset = gpio_chip_hwgpio(desc); |
35e8bb51 | 1729 | if (status) { |
372e722e | 1730 | status = chip->request(chip, offset); |
35e8bb51 | 1731 | if (status < 0) { |
7589e59f AS |
1732 | gpiod_dbg(desc, "%s: chip request fail, %d\n", |
1733 | __func__, status); | |
35e8bb51 DB |
1734 | /* and it's not available to anyone else ... |
1735 | * gpio_request() is the fully clean solution. | |
1736 | */ | |
1737 | goto lose; | |
1738 | } | |
1739 | } | |
1740 | ||
372e722e | 1741 | status = chip->direction_input(chip, offset); |
d2876d08 DB |
1742 | if (status == 0) |
1743 | clear_bit(FLAG_IS_OUT, &desc->flags); | |
3f397c21 | 1744 | |
372e722e | 1745 | trace_gpio_direction(desc_to_gpio(desc), 1, status); |
35e8bb51 | 1746 | lose: |
d2876d08 DB |
1747 | return status; |
1748 | fail: | |
1749 | spin_unlock_irqrestore(&gpio_lock, flags); | |
bcabdef1 | 1750 | if (status) |
7589e59f | 1751 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
d2876d08 DB |
1752 | return status; |
1753 | } | |
79a9becd | 1754 | EXPORT_SYMBOL_GPL(gpiod_direction_input); |
372e722e | 1755 | |
79a9becd AC |
1756 | /** |
1757 | * gpiod_direction_output - set the GPIO direction to input | |
1758 | * @desc: GPIO to set to output | |
1759 | * @value: initial output value of the GPIO | |
1760 | * | |
1761 | * Set the direction of the passed GPIO to output, such as gpiod_set_value() can | |
1762 | * be called safely on it. The initial value of the output must be specified. | |
1763 | * | |
1764 | * Return 0 in case of success, else an error code. | |
1765 | */ | |
1766 | int gpiod_direction_output(struct gpio_desc *desc, int value) | |
d2876d08 DB |
1767 | { |
1768 | unsigned long flags; | |
1769 | struct gpio_chip *chip; | |
d2876d08 | 1770 | int status = -EINVAL; |
372e722e | 1771 | int offset; |
d2876d08 | 1772 | |
be1a4b13 | 1773 | if (!desc || !desc->chip) { |
bcabdef1 AC |
1774 | pr_warn("%s: invalid GPIO\n", __func__); |
1775 | return -EINVAL; | |
1776 | } | |
1777 | ||
d468bf9e LW |
1778 | /* GPIOs used for IRQs shall not be set as output */ |
1779 | if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) { | |
1780 | gpiod_err(desc, | |
1781 | "%s: tried to set a GPIO tied to an IRQ as output\n", | |
1782 | __func__); | |
1783 | return -EIO; | |
1784 | } | |
1785 | ||
aca5ce14 LD |
1786 | /* Open drain pin should not be driven to 1 */ |
1787 | if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags)) | |
372e722e | 1788 | return gpiod_direction_input(desc); |
aca5ce14 | 1789 | |
25553ff0 LD |
1790 | /* Open source pin should not be driven to 0 */ |
1791 | if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags)) | |
372e722e | 1792 | return gpiod_direction_input(desc); |
25553ff0 | 1793 | |
be1a4b13 LW |
1794 | chip = desc->chip; |
1795 | if (!chip->set || !chip->direction_output) { | |
6424de5a MB |
1796 | gpiod_warn(desc, |
1797 | "%s: missing set() or direction_output() operations\n", | |
1798 | __func__); | |
be1a4b13 LW |
1799 | return -EIO; |
1800 | } | |
1801 | ||
d2876d08 DB |
1802 | spin_lock_irqsave(&gpio_lock, flags); |
1803 | ||
372e722e | 1804 | status = gpio_ensure_requested(desc); |
35e8bb51 DB |
1805 | if (status < 0) |
1806 | goto fail; | |
d2876d08 DB |
1807 | |
1808 | /* now we know the gpio is valid and chip won't vanish */ | |
1809 | ||
1810 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1811 | ||
9c4ba946 | 1812 | might_sleep_if(chip->can_sleep); |
d2876d08 | 1813 | |
372e722e | 1814 | offset = gpio_chip_hwgpio(desc); |
35e8bb51 | 1815 | if (status) { |
372e722e | 1816 | status = chip->request(chip, offset); |
35e8bb51 | 1817 | if (status < 0) { |
7589e59f AS |
1818 | gpiod_dbg(desc, "%s: chip request fail, %d\n", |
1819 | __func__, status); | |
35e8bb51 DB |
1820 | /* and it's not available to anyone else ... |
1821 | * gpio_request() is the fully clean solution. | |
1822 | */ | |
1823 | goto lose; | |
1824 | } | |
1825 | } | |
1826 | ||
372e722e | 1827 | status = chip->direction_output(chip, offset, value); |
d2876d08 DB |
1828 | if (status == 0) |
1829 | set_bit(FLAG_IS_OUT, &desc->flags); | |
372e722e AC |
1830 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
1831 | trace_gpio_direction(desc_to_gpio(desc), 0, status); | |
35e8bb51 | 1832 | lose: |
d2876d08 DB |
1833 | return status; |
1834 | fail: | |
1835 | spin_unlock_irqrestore(&gpio_lock, flags); | |
bcabdef1 | 1836 | if (status) |
6424de5a | 1837 | gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status); |
d2876d08 DB |
1838 | return status; |
1839 | } | |
79a9becd | 1840 | EXPORT_SYMBOL_GPL(gpiod_direction_output); |
d2876d08 | 1841 | |
c4b5be98 | 1842 | /** |
79a9becd | 1843 | * gpiod_set_debounce - sets @debounce time for a @gpio |
c4b5be98 FB |
1844 | * @gpio: the gpio to set debounce time |
1845 | * @debounce: debounce time is microseconds | |
65d87656 LW |
1846 | * |
1847 | * returns -ENOTSUPP if the controller does not support setting | |
1848 | * debounce. | |
c4b5be98 | 1849 | */ |
79a9becd | 1850 | int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) |
c4b5be98 FB |
1851 | { |
1852 | unsigned long flags; | |
1853 | struct gpio_chip *chip; | |
c4b5be98 | 1854 | int status = -EINVAL; |
372e722e | 1855 | int offset; |
c4b5be98 | 1856 | |
be1a4b13 | 1857 | if (!desc || !desc->chip) { |
bcabdef1 AC |
1858 | pr_warn("%s: invalid GPIO\n", __func__); |
1859 | return -EINVAL; | |
1860 | } | |
1861 | ||
c4b5be98 | 1862 | chip = desc->chip; |
be1a4b13 | 1863 | if (!chip->set || !chip->set_debounce) { |
6424de5a MB |
1864 | gpiod_dbg(desc, |
1865 | "%s: missing set() or set_debounce() operations\n", | |
1866 | __func__); | |
65d87656 | 1867 | return -ENOTSUPP; |
be1a4b13 LW |
1868 | } |
1869 | ||
1870 | spin_lock_irqsave(&gpio_lock, flags); | |
372e722e AC |
1871 | |
1872 | status = gpio_ensure_requested(desc); | |
c4b5be98 FB |
1873 | if (status < 0) |
1874 | goto fail; | |
1875 | ||
1876 | /* now we know the gpio is valid and chip won't vanish */ | |
1877 | ||
1878 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1879 | ||
9c4ba946 | 1880 | might_sleep_if(chip->can_sleep); |
c4b5be98 | 1881 | |
372e722e AC |
1882 | offset = gpio_chip_hwgpio(desc); |
1883 | return chip->set_debounce(chip, offset, debounce); | |
c4b5be98 FB |
1884 | |
1885 | fail: | |
1886 | spin_unlock_irqrestore(&gpio_lock, flags); | |
bcabdef1 | 1887 | if (status) |
6424de5a | 1888 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
c4b5be98 FB |
1889 | |
1890 | return status; | |
1891 | } | |
79a9becd | 1892 | EXPORT_SYMBOL_GPL(gpiod_set_debounce); |
372e722e | 1893 | |
79a9becd AC |
1894 | /** |
1895 | * gpiod_is_active_low - test whether a GPIO is active-low or not | |
1896 | * @desc: the gpio descriptor to test | |
1897 | * | |
1898 | * Returns 1 if the GPIO is active-low, 0 otherwise. | |
1899 | */ | |
1900 | int gpiod_is_active_low(const struct gpio_desc *desc) | |
372e722e | 1901 | { |
79a9becd | 1902 | return test_bit(FLAG_ACTIVE_LOW, &desc->flags); |
372e722e | 1903 | } |
79a9becd | 1904 | EXPORT_SYMBOL_GPL(gpiod_is_active_low); |
d2876d08 DB |
1905 | |
1906 | /* I/O calls are only valid after configuration completed; the relevant | |
1907 | * "is this a valid GPIO" error checks should already have been done. | |
1908 | * | |
1909 | * "Get" operations are often inlinable as reading a pin value register, | |
1910 | * and masking the relevant bit in that register. | |
1911 | * | |
1912 | * When "set" operations are inlinable, they involve writing that mask to | |
1913 | * one register to set a low value, or a different register to set it high. | |
1914 | * Otherwise locking is needed, so there may be little value to inlining. | |
1915 | * | |
1916 | *------------------------------------------------------------------------ | |
1917 | * | |
1918 | * IMPORTANT!!! The hot paths -- get/set value -- assume that callers | |
1919 | * have requested the GPIO. That can include implicit requesting by | |
1920 | * a direction setting call. Marking a gpio as requested locks its chip | |
1921 | * in memory, guaranteeing that these table lookups need no more locking | |
1922 | * and that gpiochip_remove() will fail. | |
1923 | * | |
1924 | * REVISIT when debugging, consider adding some instrumentation to ensure | |
1925 | * that the GPIO was actually requested. | |
1926 | */ | |
1927 | ||
79a9becd | 1928 | static int _gpiod_get_raw_value(const struct gpio_desc *desc) |
d2876d08 DB |
1929 | { |
1930 | struct gpio_chip *chip; | |
3f397c21 | 1931 | int value; |
372e722e | 1932 | int offset; |
d2876d08 | 1933 | |
372e722e AC |
1934 | chip = desc->chip; |
1935 | offset = gpio_chip_hwgpio(desc); | |
372e722e AC |
1936 | value = chip->get ? chip->get(chip, offset) : 0; |
1937 | trace_gpio_value(desc_to_gpio(desc), 1, value); | |
3f397c21 | 1938 | return value; |
d2876d08 | 1939 | } |
372e722e | 1940 | |
d2876d08 | 1941 | /** |
79a9becd AC |
1942 | * gpiod_get_raw_value() - return a gpio's raw value |
1943 | * @desc: gpio whose value will be returned | |
d2876d08 | 1944 | * |
79a9becd AC |
1945 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding |
1946 | * its ACTIVE_LOW status. | |
1947 | * | |
1948 | * This function should be called from contexts where we cannot sleep, and will | |
1949 | * complain if the GPIO chip functions potentially sleep. | |
d2876d08 | 1950 | */ |
79a9becd | 1951 | int gpiod_get_raw_value(const struct gpio_desc *desc) |
d2876d08 | 1952 | { |
bcabdef1 AC |
1953 | if (!desc) |
1954 | return 0; | |
e4e449e8 | 1955 | /* Should be using gpio_get_value_cansleep() */ |
d8e0ac08 | 1956 | WARN_ON(desc->chip->can_sleep); |
79a9becd | 1957 | return _gpiod_get_raw_value(desc); |
d2876d08 | 1958 | } |
79a9becd | 1959 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value); |
372e722e | 1960 | |
79a9becd AC |
1961 | /** |
1962 | * gpiod_get_value() - return a gpio's value | |
1963 | * @desc: gpio whose value will be returned | |
1964 | * | |
1965 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into | |
1966 | * account. | |
1967 | * | |
1968 | * This function should be called from contexts where we cannot sleep, and will | |
1969 | * complain if the GPIO chip functions potentially sleep. | |
1970 | */ | |
1971 | int gpiod_get_value(const struct gpio_desc *desc) | |
372e722e | 1972 | { |
79a9becd AC |
1973 | int value; |
1974 | if (!desc) | |
1975 | return 0; | |
1976 | /* Should be using gpio_get_value_cansleep() */ | |
1977 | WARN_ON(desc->chip->can_sleep); | |
1978 | ||
1979 | value = _gpiod_get_raw_value(desc); | |
1980 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | |
1981 | value = !value; | |
1982 | ||
1983 | return value; | |
372e722e | 1984 | } |
79a9becd | 1985 | EXPORT_SYMBOL_GPL(gpiod_get_value); |
d2876d08 | 1986 | |
aca5ce14 LD |
1987 | /* |
1988 | * _gpio_set_open_drain_value() - Set the open drain gpio's value. | |
79a9becd | 1989 | * @desc: gpio descriptor whose state need to be set. |
aca5ce14 LD |
1990 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. |
1991 | */ | |
372e722e | 1992 | static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value) |
aca5ce14 LD |
1993 | { |
1994 | int err = 0; | |
372e722e AC |
1995 | struct gpio_chip *chip = desc->chip; |
1996 | int offset = gpio_chip_hwgpio(desc); | |
1997 | ||
aca5ce14 | 1998 | if (value) { |
372e722e | 1999 | err = chip->direction_input(chip, offset); |
aca5ce14 | 2000 | if (!err) |
372e722e | 2001 | clear_bit(FLAG_IS_OUT, &desc->flags); |
aca5ce14 | 2002 | } else { |
372e722e | 2003 | err = chip->direction_output(chip, offset, 0); |
aca5ce14 | 2004 | if (!err) |
372e722e | 2005 | set_bit(FLAG_IS_OUT, &desc->flags); |
aca5ce14 | 2006 | } |
372e722e | 2007 | trace_gpio_direction(desc_to_gpio(desc), value, err); |
aca5ce14 | 2008 | if (err < 0) |
6424de5a MB |
2009 | gpiod_err(desc, |
2010 | "%s: Error in set_value for open drain err %d\n", | |
2011 | __func__, err); | |
aca5ce14 LD |
2012 | } |
2013 | ||
25553ff0 | 2014 | /* |
79a9becd AC |
2015 | * _gpio_set_open_source_value() - Set the open source gpio's value. |
2016 | * @desc: gpio descriptor whose state need to be set. | |
25553ff0 LD |
2017 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. |
2018 | */ | |
372e722e | 2019 | static void _gpio_set_open_source_value(struct gpio_desc *desc, int value) |
25553ff0 LD |
2020 | { |
2021 | int err = 0; | |
372e722e AC |
2022 | struct gpio_chip *chip = desc->chip; |
2023 | int offset = gpio_chip_hwgpio(desc); | |
2024 | ||
25553ff0 | 2025 | if (value) { |
372e722e | 2026 | err = chip->direction_output(chip, offset, 1); |
25553ff0 | 2027 | if (!err) |
372e722e | 2028 | set_bit(FLAG_IS_OUT, &desc->flags); |
25553ff0 | 2029 | } else { |
372e722e | 2030 | err = chip->direction_input(chip, offset); |
25553ff0 | 2031 | if (!err) |
372e722e | 2032 | clear_bit(FLAG_IS_OUT, &desc->flags); |
25553ff0 | 2033 | } |
372e722e | 2034 | trace_gpio_direction(desc_to_gpio(desc), !value, err); |
25553ff0 | 2035 | if (err < 0) |
6424de5a MB |
2036 | gpiod_err(desc, |
2037 | "%s: Error in set_value for open source err %d\n", | |
2038 | __func__, err); | |
25553ff0 LD |
2039 | } |
2040 | ||
79a9becd | 2041 | static void _gpiod_set_raw_value(struct gpio_desc *desc, int value) |
d2876d08 DB |
2042 | { |
2043 | struct gpio_chip *chip; | |
2044 | ||
372e722e | 2045 | chip = desc->chip; |
372e722e AC |
2046 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
2047 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) | |
2048 | _gpio_set_open_drain_value(desc, value); | |
2049 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) | |
2050 | _gpio_set_open_source_value(desc, value); | |
aca5ce14 | 2051 | else |
372e722e AC |
2052 | chip->set(chip, gpio_chip_hwgpio(desc), value); |
2053 | } | |
2054 | ||
d2876d08 | 2055 | /** |
79a9becd AC |
2056 | * gpiod_set_raw_value() - assign a gpio's raw value |
2057 | * @desc: gpio whose value will be assigned | |
d2876d08 | 2058 | * @value: value to assign |
d2876d08 | 2059 | * |
79a9becd AC |
2060 | * Set the raw value of the GPIO, i.e. the value of its physical line without |
2061 | * regard for its ACTIVE_LOW status. | |
2062 | * | |
2063 | * This function should be called from contexts where we cannot sleep, and will | |
2064 | * complain if the GPIO chip functions potentially sleep. | |
d2876d08 | 2065 | */ |
79a9becd | 2066 | void gpiod_set_raw_value(struct gpio_desc *desc, int value) |
372e722e | 2067 | { |
bcabdef1 AC |
2068 | if (!desc) |
2069 | return; | |
e4e449e8 | 2070 | /* Should be using gpio_set_value_cansleep() */ |
d8e0ac08 | 2071 | WARN_ON(desc->chip->can_sleep); |
79a9becd | 2072 | _gpiod_set_raw_value(desc, value); |
d2876d08 | 2073 | } |
79a9becd | 2074 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value); |
d2876d08 DB |
2075 | |
2076 | /** | |
79a9becd AC |
2077 | * gpiod_set_value() - assign a gpio's value |
2078 | * @desc: gpio whose value will be assigned | |
2079 | * @value: value to assign | |
2080 | * | |
2081 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into | |
2082 | * account | |
d2876d08 | 2083 | * |
79a9becd AC |
2084 | * This function should be called from contexts where we cannot sleep, and will |
2085 | * complain if the GPIO chip functions potentially sleep. | |
d2876d08 | 2086 | */ |
79a9becd | 2087 | void gpiod_set_value(struct gpio_desc *desc, int value) |
d2876d08 | 2088 | { |
bcabdef1 | 2089 | if (!desc) |
79a9becd AC |
2090 | return; |
2091 | /* Should be using gpio_set_value_cansleep() */ | |
2092 | WARN_ON(desc->chip->can_sleep); | |
2093 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | |
2094 | value = !value; | |
2095 | _gpiod_set_raw_value(desc, value); | |
372e722e | 2096 | } |
79a9becd | 2097 | EXPORT_SYMBOL_GPL(gpiod_set_value); |
d2876d08 | 2098 | |
d2876d08 | 2099 | /** |
79a9becd AC |
2100 | * gpiod_cansleep() - report whether gpio value access may sleep |
2101 | * @desc: gpio to check | |
d2876d08 | 2102 | * |
d2876d08 | 2103 | */ |
79a9becd | 2104 | int gpiod_cansleep(const struct gpio_desc *desc) |
372e722e | 2105 | { |
bcabdef1 AC |
2106 | if (!desc) |
2107 | return 0; | |
372e722e | 2108 | return desc->chip->can_sleep; |
d2876d08 | 2109 | } |
79a9becd | 2110 | EXPORT_SYMBOL_GPL(gpiod_cansleep); |
d2876d08 | 2111 | |
0f6d504e | 2112 | /** |
79a9becd AC |
2113 | * gpiod_to_irq() - return the IRQ corresponding to a GPIO |
2114 | * @desc: gpio whose IRQ will be returned (already requested) | |
0f6d504e | 2115 | * |
79a9becd AC |
2116 | * Return the IRQ corresponding to the passed GPIO, or an error code in case of |
2117 | * error. | |
0f6d504e | 2118 | */ |
79a9becd | 2119 | int gpiod_to_irq(const struct gpio_desc *desc) |
0f6d504e DB |
2120 | { |
2121 | struct gpio_chip *chip; | |
372e722e | 2122 | int offset; |
0f6d504e | 2123 | |
bcabdef1 AC |
2124 | if (!desc) |
2125 | return -EINVAL; | |
372e722e AC |
2126 | chip = desc->chip; |
2127 | offset = gpio_chip_hwgpio(desc); | |
2128 | return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO; | |
0f6d504e | 2129 | } |
79a9becd | 2130 | EXPORT_SYMBOL_GPL(gpiod_to_irq); |
0f6d504e | 2131 | |
d468bf9e LW |
2132 | /** |
2133 | * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ | |
2134 | * @gpio: the GPIO line to lock as used for IRQ | |
2135 | * | |
2136 | * This is used directly by GPIO drivers that want to lock down | |
2137 | * a certain GPIO line to be used as IRQs, for example in the | |
2138 | * .to_irq() callback of their gpio_chip, or in the .irq_enable() | |
2139 | * of its irq_chip implementation if the GPIO is known from that | |
2140 | * code. | |
2141 | */ | |
79a9becd | 2142 | int gpiod_lock_as_irq(struct gpio_desc *desc) |
372e722e | 2143 | { |
d468bf9e LW |
2144 | if (!desc) |
2145 | return -EINVAL; | |
2146 | ||
2147 | if (test_bit(FLAG_IS_OUT, &desc->flags)) { | |
2148 | gpiod_err(desc, | |
2149 | "%s: tried to flag a GPIO set as output for IRQ\n", | |
2150 | __func__); | |
2151 | return -EIO; | |
2152 | } | |
2153 | ||
2154 | set_bit(FLAG_USED_AS_IRQ, &desc->flags); | |
2155 | return 0; | |
372e722e | 2156 | } |
79a9becd | 2157 | EXPORT_SYMBOL_GPL(gpiod_lock_as_irq); |
d2876d08 | 2158 | |
d468bf9e LW |
2159 | int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset) |
2160 | { | |
2161 | return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip, offset)); | |
2162 | } | |
2163 | EXPORT_SYMBOL_GPL(gpio_lock_as_irq); | |
d2876d08 | 2164 | |
d468bf9e LW |
2165 | /** |
2166 | * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ | |
2167 | * @gpio: the GPIO line to unlock from IRQ usage | |
2168 | * | |
2169 | * This is used directly by GPIO drivers that want to indicate | |
2170 | * that a certain GPIO is no longer used exclusively for IRQ. | |
d2876d08 | 2171 | */ |
79a9becd | 2172 | void gpiod_unlock_as_irq(struct gpio_desc *desc) |
d468bf9e LW |
2173 | { |
2174 | if (!desc) | |
2175 | return; | |
d2876d08 | 2176 | |
d468bf9e LW |
2177 | clear_bit(FLAG_USED_AS_IRQ, &desc->flags); |
2178 | } | |
79a9becd | 2179 | EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq); |
d468bf9e LW |
2180 | |
2181 | void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset) | |
2182 | { | |
2183 | return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip, offset)); | |
2184 | } | |
2185 | EXPORT_SYMBOL_GPL(gpio_unlock_as_irq); | |
d2876d08 | 2186 | |
79a9becd AC |
2187 | /** |
2188 | * gpiod_get_raw_value_cansleep() - return a gpio's raw value | |
2189 | * @desc: gpio whose value will be returned | |
2190 | * | |
2191 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding | |
2192 | * its ACTIVE_LOW status. | |
2193 | * | |
2194 | * This function is to be called from contexts that can sleep. | |
d2876d08 | 2195 | */ |
79a9becd | 2196 | int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) |
d2876d08 | 2197 | { |
d2876d08 | 2198 | might_sleep_if(extra_checks); |
bcabdef1 AC |
2199 | if (!desc) |
2200 | return 0; | |
79a9becd | 2201 | return _gpiod_get_raw_value(desc); |
d2876d08 | 2202 | } |
79a9becd | 2203 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep); |
372e722e | 2204 | |
79a9becd AC |
2205 | /** |
2206 | * gpiod_get_value_cansleep() - return a gpio's value | |
2207 | * @desc: gpio whose value will be returned | |
2208 | * | |
2209 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into | |
2210 | * account. | |
2211 | * | |
2212 | * This function is to be called from contexts that can sleep. | |
2213 | */ | |
2214 | int gpiod_get_value_cansleep(const struct gpio_desc *desc) | |
d2876d08 | 2215 | { |
3f397c21 | 2216 | int value; |
d2876d08 DB |
2217 | |
2218 | might_sleep_if(extra_checks); | |
bcabdef1 AC |
2219 | if (!desc) |
2220 | return 0; | |
79a9becd AC |
2221 | |
2222 | value = _gpiod_get_raw_value(desc); | |
2223 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | |
2224 | value = !value; | |
2225 | ||
3f397c21 | 2226 | return value; |
d2876d08 | 2227 | } |
79a9becd | 2228 | EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep); |
372e722e | 2229 | |
79a9becd AC |
2230 | /** |
2231 | * gpiod_set_raw_value_cansleep() - assign a gpio's raw value | |
2232 | * @desc: gpio whose value will be assigned | |
2233 | * @value: value to assign | |
2234 | * | |
2235 | * Set the raw value of the GPIO, i.e. the value of its physical line without | |
2236 | * regard for its ACTIVE_LOW status. | |
2237 | * | |
2238 | * This function is to be called from contexts that can sleep. | |
2239 | */ | |
2240 | void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value) | |
372e722e | 2241 | { |
d2876d08 | 2242 | might_sleep_if(extra_checks); |
bcabdef1 AC |
2243 | if (!desc) |
2244 | return; | |
79a9becd | 2245 | _gpiod_set_raw_value(desc, value); |
372e722e | 2246 | } |
79a9becd | 2247 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep); |
d2876d08 | 2248 | |
79a9becd AC |
2249 | /** |
2250 | * gpiod_set_value_cansleep() - assign a gpio's value | |
2251 | * @desc: gpio whose value will be assigned | |
2252 | * @value: value to assign | |
2253 | * | |
2254 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into | |
2255 | * account | |
2256 | * | |
2257 | * This function is to be called from contexts that can sleep. | |
2258 | */ | |
2259 | void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) | |
d2876d08 | 2260 | { |
d2876d08 | 2261 | might_sleep_if(extra_checks); |
bcabdef1 AC |
2262 | if (!desc) |
2263 | return; | |
79a9becd AC |
2264 | |
2265 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | |
2266 | value = !value; | |
2267 | _gpiod_set_raw_value(desc, value); | |
372e722e | 2268 | } |
79a9becd | 2269 | EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep); |
d2876d08 | 2270 | |
bae48da2 | 2271 | /** |
ad824783 AC |
2272 | * gpiod_add_lookup_table() - register GPIO device consumers |
2273 | * @table: table of consumers to register | |
bae48da2 | 2274 | */ |
ad824783 | 2275 | void gpiod_add_lookup_table(struct gpiod_lookup_table *table) |
bae48da2 AC |
2276 | { |
2277 | mutex_lock(&gpio_lookup_lock); | |
2278 | ||
ad824783 | 2279 | list_add_tail(&table->list, &gpio_lookup_list); |
bae48da2 AC |
2280 | |
2281 | mutex_unlock(&gpio_lookup_lock); | |
2282 | } | |
2283 | ||
bae48da2 AC |
2284 | #ifdef CONFIG_OF |
2285 | static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, | |
53e7cac3 AC |
2286 | unsigned int idx, |
2287 | enum gpio_lookup_flags *flags) | |
bae48da2 AC |
2288 | { |
2289 | char prop_name[32]; /* 32 is max size of property name */ | |
2290 | enum of_gpio_flags of_flags; | |
2291 | struct gpio_desc *desc; | |
2292 | ||
2293 | if (con_id) | |
2294 | snprintf(prop_name, 32, "%s-gpios", con_id); | |
aca5ce14 | 2295 | else |
bae48da2 AC |
2296 | snprintf(prop_name, 32, "gpios"); |
2297 | ||
2298 | desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, | |
2299 | &of_flags); | |
2300 | ||
2301 | if (IS_ERR(desc)) | |
2302 | return desc; | |
2303 | ||
2304 | if (of_flags & OF_GPIO_ACTIVE_LOW) | |
53e7cac3 | 2305 | *flags |= GPIO_ACTIVE_LOW; |
bae48da2 AC |
2306 | |
2307 | return desc; | |
2308 | } | |
2309 | #else | |
2310 | static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, | |
209e64f9 AC |
2311 | unsigned int idx, |
2312 | enum gpio_lookup_flags *flags) | |
bae48da2 AC |
2313 | { |
2314 | return ERR_PTR(-ENODEV); | |
d2876d08 | 2315 | } |
bae48da2 | 2316 | #endif |
d2876d08 | 2317 | |
81f59e9d | 2318 | static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, |
53e7cac3 AC |
2319 | unsigned int idx, |
2320 | enum gpio_lookup_flags *flags) | |
372e722e | 2321 | { |
e01f440a MW |
2322 | struct acpi_gpio_info info; |
2323 | struct gpio_desc *desc; | |
2324 | ||
2325 | desc = acpi_get_gpiod_by_index(dev, idx, &info); | |
2326 | if (IS_ERR(desc)) | |
2327 | return desc; | |
2328 | ||
2329 | if (info.gpioint && info.active_low) | |
53e7cac3 | 2330 | *flags |= GPIO_ACTIVE_LOW; |
e01f440a MW |
2331 | |
2332 | return desc; | |
81f59e9d MW |
2333 | } |
2334 | ||
ad824783 | 2335 | static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev) |
bae48da2 AC |
2336 | { |
2337 | const char *dev_id = dev ? dev_name(dev) : NULL; | |
ad824783 | 2338 | struct gpiod_lookup_table *table; |
bae48da2 AC |
2339 | |
2340 | mutex_lock(&gpio_lookup_lock); | |
2341 | ||
ad824783 AC |
2342 | list_for_each_entry(table, &gpio_lookup_list, list) { |
2343 | if (table->dev_id && dev_id) { | |
2344 | /* | |
2345 | * Valid strings on both ends, must be identical to have | |
2346 | * a match | |
2347 | */ | |
2348 | if (!strcmp(table->dev_id, dev_id)) | |
2349 | goto found; | |
2350 | } else { | |
2351 | /* | |
2352 | * One of the pointers is NULL, so both must be to have | |
2353 | * a match | |
2354 | */ | |
2355 | if (dev_id == table->dev_id) | |
2356 | goto found; | |
2357 | } | |
2358 | } | |
2359 | table = NULL; | |
bae48da2 | 2360 | |
ad824783 AC |
2361 | found: |
2362 | mutex_unlock(&gpio_lookup_lock); | |
2363 | return table; | |
2364 | } | |
bae48da2 | 2365 | |
ad824783 AC |
2366 | static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id, |
2367 | unsigned int idx, | |
2368 | enum gpio_lookup_flags *flags) | |
2369 | { | |
2370 | struct gpio_desc *desc = ERR_PTR(-ENODEV); | |
2371 | struct gpiod_lookup_table *table; | |
2372 | struct gpiod_lookup *p; | |
bae48da2 | 2373 | |
ad824783 AC |
2374 | table = gpiod_find_lookup_table(dev); |
2375 | if (!table) | |
2376 | return desc; | |
bae48da2 | 2377 | |
ad824783 AC |
2378 | for (p = &table->table[0]; p->chip_label; p++) { |
2379 | struct gpio_chip *chip; | |
bae48da2 | 2380 | |
ad824783 | 2381 | /* idx must always match exactly */ |
bae48da2 AC |
2382 | if (p->idx != idx) |
2383 | continue; | |
2384 | ||
ad824783 AC |
2385 | /* If the lookup entry has a con_id, require exact match */ |
2386 | if (p->con_id && (!con_id || strcmp(p->con_id, con_id))) | |
2387 | continue; | |
bae48da2 | 2388 | |
ad824783 | 2389 | chip = find_chip_by_name(p->chip_label); |
bae48da2 | 2390 | |
ad824783 AC |
2391 | if (!chip) { |
2392 | dev_warn(dev, "cannot find GPIO chip %s\n", | |
2393 | p->chip_label); | |
2394 | continue; | |
2395 | } | |
bae48da2 | 2396 | |
ad824783 AC |
2397 | if (chip->ngpio <= p->chip_hwnum) { |
2398 | dev_warn(dev, "GPIO chip %s has %d GPIOs\n", | |
2399 | chip->label, chip->ngpio); | |
2400 | continue; | |
bae48da2 | 2401 | } |
bae48da2 | 2402 | |
ad824783 AC |
2403 | desc = gpiochip_offset_to_desc(chip, p->chip_hwnum); |
2404 | *flags = p->flags; | |
2405 | } | |
bae48da2 AC |
2406 | |
2407 | return desc; | |
2408 | } | |
2409 | ||
2410 | /** | |
2411 | * gpio_get - obtain a GPIO for a given GPIO function | |
ad824783 | 2412 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
bae48da2 AC |
2413 | * @con_id: function within the GPIO consumer |
2414 | * | |
2415 | * Return the GPIO descriptor corresponding to the function con_id of device | |
2416 | * dev, or an IS_ERR() condition if an error occured. | |
2417 | */ | |
2418 | struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id) | |
2419 | { | |
2420 | return gpiod_get_index(dev, con_id, 0); | |
2421 | } | |
2422 | EXPORT_SYMBOL_GPL(gpiod_get); | |
2423 | ||
2424 | /** | |
2425 | * gpiod_get_index - obtain a GPIO from a multi-index GPIO function | |
2426 | * @dev: GPIO consumer | |
2427 | * @con_id: function within the GPIO consumer | |
2428 | * @idx: index of the GPIO to obtain in the consumer | |
2429 | * | |
2430 | * This variant of gpiod_get() allows to access GPIOs other than the first | |
2431 | * defined one for functions that define several GPIOs. | |
2432 | * | |
2433 | * Return a valid GPIO descriptor, or an IS_ERR() condition in case of error. | |
2434 | */ | |
2435 | struct gpio_desc *__must_check gpiod_get_index(struct device *dev, | |
2436 | const char *con_id, | |
2437 | unsigned int idx) | |
2438 | { | |
35c5d7fd | 2439 | struct gpio_desc *desc = NULL; |
bae48da2 | 2440 | int status; |
53e7cac3 | 2441 | enum gpio_lookup_flags flags = 0; |
bae48da2 AC |
2442 | |
2443 | dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id); | |
2444 | ||
2445 | /* Using device tree? */ | |
2446 | if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) { | |
2447 | dev_dbg(dev, "using device tree for GPIO lookup\n"); | |
2448 | desc = of_find_gpio(dev, con_id, idx, &flags); | |
81f59e9d MW |
2449 | } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) { |
2450 | dev_dbg(dev, "using ACPI for GPIO lookup\n"); | |
2451 | desc = acpi_find_gpio(dev, con_id, idx, &flags); | |
35c5d7fd AC |
2452 | } |
2453 | ||
2454 | /* | |
2455 | * Either we are not using DT or ACPI, or their lookup did not return | |
2456 | * a result. In that case, use platform lookup as a fallback. | |
2457 | */ | |
2458 | if (!desc || IS_ERR(desc)) { | |
2459 | struct gpio_desc *pdesc; | |
7589e59f | 2460 | |
bae48da2 | 2461 | dev_dbg(dev, "using lookup tables for GPIO lookup"); |
35c5d7fd | 2462 | pdesc = gpiod_find(dev, con_id, idx, &flags); |
7589e59f | 2463 | |
35c5d7fd AC |
2464 | /* If used as fallback, do not replace the previous error */ |
2465 | if (!IS_ERR(pdesc) || !desc) | |
2466 | desc = pdesc; | |
bae48da2 AC |
2467 | } |
2468 | ||
2469 | if (IS_ERR(desc)) { | |
351cfe0f | 2470 | dev_dbg(dev, "lookup for GPIO %s failed\n", con_id); |
bae48da2 AC |
2471 | return desc; |
2472 | } | |
2473 | ||
2474 | status = gpiod_request(desc, con_id); | |
2475 | ||
2476 | if (status < 0) | |
2477 | return ERR_PTR(status); | |
2478 | ||
53e7cac3 | 2479 | if (flags & GPIO_ACTIVE_LOW) |
bae48da2 | 2480 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); |
53e7cac3 AC |
2481 | if (flags & GPIO_OPEN_DRAIN) |
2482 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); | |
2483 | if (flags & GPIO_OPEN_SOURCE) | |
2484 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); | |
bae48da2 AC |
2485 | |
2486 | return desc; | |
2487 | } | |
2488 | EXPORT_SYMBOL_GPL(gpiod_get_index); | |
2489 | ||
2490 | /** | |
2491 | * gpiod_put - dispose of a GPIO descriptor | |
2492 | * @desc: GPIO descriptor to dispose of | |
2493 | * | |
2494 | * No descriptor can be used after gpiod_put() has been called on it. | |
2495 | */ | |
2496 | void gpiod_put(struct gpio_desc *desc) | |
2497 | { | |
2498 | gpiod_free(desc); | |
372e722e | 2499 | } |
bae48da2 | 2500 | EXPORT_SYMBOL_GPL(gpiod_put); |
d2876d08 DB |
2501 | |
2502 | #ifdef CONFIG_DEBUG_FS | |
2503 | ||
d2876d08 DB |
2504 | static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
2505 | { | |
2506 | unsigned i; | |
2507 | unsigned gpio = chip->base; | |
6c0b4e6c | 2508 | struct gpio_desc *gdesc = &chip->desc[0]; |
d2876d08 | 2509 | int is_out; |
d468bf9e | 2510 | int is_irq; |
d2876d08 DB |
2511 | |
2512 | for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) { | |
2513 | if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) | |
2514 | continue; | |
2515 | ||
372e722e | 2516 | gpiod_get_direction(gdesc); |
d2876d08 | 2517 | is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); |
d468bf9e LW |
2518 | is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags); |
2519 | seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s", | |
d2876d08 DB |
2520 | gpio, gdesc->label, |
2521 | is_out ? "out" : "in ", | |
2522 | chip->get | |
2523 | ? (chip->get(chip, i) ? "hi" : "lo") | |
d468bf9e LW |
2524 | : "? ", |
2525 | is_irq ? "IRQ" : " "); | |
d2876d08 DB |
2526 | seq_printf(s, "\n"); |
2527 | } | |
2528 | } | |
2529 | ||
f9c4a31f | 2530 | static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) |
d2876d08 | 2531 | { |
362432ae | 2532 | unsigned long flags; |
f9c4a31f | 2533 | struct gpio_chip *chip = NULL; |
cb1650d4 | 2534 | loff_t index = *pos; |
d2876d08 | 2535 | |
f9c4a31f | 2536 | s->private = ""; |
d2876d08 | 2537 | |
362432ae | 2538 | spin_lock_irqsave(&gpio_lock, flags); |
cb1650d4 | 2539 | list_for_each_entry(chip, &gpio_chips, list) |
362432ae GL |
2540 | if (index-- == 0) { |
2541 | spin_unlock_irqrestore(&gpio_lock, flags); | |
cb1650d4 | 2542 | return chip; |
f9c4a31f | 2543 | } |
362432ae | 2544 | spin_unlock_irqrestore(&gpio_lock, flags); |
f9c4a31f | 2545 | |
cb1650d4 | 2546 | return NULL; |
f9c4a31f TR |
2547 | } |
2548 | ||
2549 | static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
2550 | { | |
362432ae | 2551 | unsigned long flags; |
f9c4a31f | 2552 | struct gpio_chip *chip = v; |
f9c4a31f TR |
2553 | void *ret = NULL; |
2554 | ||
362432ae | 2555 | spin_lock_irqsave(&gpio_lock, flags); |
cb1650d4 AC |
2556 | if (list_is_last(&chip->list, &gpio_chips)) |
2557 | ret = NULL; | |
2558 | else | |
2559 | ret = list_entry(chip->list.next, struct gpio_chip, list); | |
362432ae | 2560 | spin_unlock_irqrestore(&gpio_lock, flags); |
f9c4a31f TR |
2561 | |
2562 | s->private = "\n"; | |
2563 | ++*pos; | |
2564 | ||
2565 | return ret; | |
2566 | } | |
2567 | ||
2568 | static void gpiolib_seq_stop(struct seq_file *s, void *v) | |
2569 | { | |
2570 | } | |
2571 | ||
2572 | static int gpiolib_seq_show(struct seq_file *s, void *v) | |
2573 | { | |
2574 | struct gpio_chip *chip = v; | |
2575 | struct device *dev; | |
2576 | ||
2577 | seq_printf(s, "%sGPIOs %d-%d", (char *)s->private, | |
2578 | chip->base, chip->base + chip->ngpio - 1); | |
2579 | dev = chip->dev; | |
2580 | if (dev) | |
2581 | seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus", | |
2582 | dev_name(dev)); | |
2583 | if (chip->label) | |
2584 | seq_printf(s, ", %s", chip->label); | |
2585 | if (chip->can_sleep) | |
2586 | seq_printf(s, ", can sleep"); | |
2587 | seq_printf(s, ":\n"); | |
2588 | ||
2589 | if (chip->dbg_show) | |
2590 | chip->dbg_show(s, chip); | |
2591 | else | |
2592 | gpiolib_dbg_show(s, chip); | |
2593 | ||
d2876d08 DB |
2594 | return 0; |
2595 | } | |
2596 | ||
f9c4a31f TR |
2597 | static const struct seq_operations gpiolib_seq_ops = { |
2598 | .start = gpiolib_seq_start, | |
2599 | .next = gpiolib_seq_next, | |
2600 | .stop = gpiolib_seq_stop, | |
2601 | .show = gpiolib_seq_show, | |
2602 | }; | |
2603 | ||
d2876d08 DB |
2604 | static int gpiolib_open(struct inode *inode, struct file *file) |
2605 | { | |
f9c4a31f | 2606 | return seq_open(file, &gpiolib_seq_ops); |
d2876d08 DB |
2607 | } |
2608 | ||
828c0950 | 2609 | static const struct file_operations gpiolib_operations = { |
f9c4a31f | 2610 | .owner = THIS_MODULE, |
d2876d08 DB |
2611 | .open = gpiolib_open, |
2612 | .read = seq_read, | |
2613 | .llseek = seq_lseek, | |
f9c4a31f | 2614 | .release = seq_release, |
d2876d08 DB |
2615 | }; |
2616 | ||
2617 | static int __init gpiolib_debugfs_init(void) | |
2618 | { | |
2619 | /* /sys/kernel/debug/gpio */ | |
2620 | (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO, | |
2621 | NULL, NULL, &gpiolib_operations); | |
2622 | return 0; | |
2623 | } | |
2624 | subsys_initcall(gpiolib_debugfs_init); | |
2625 | ||
2626 | #endif /* DEBUG_FS */ |