1 From 3a0251c01446f3a6763e4406ca5495102db63aa4 Mon Sep 17 00:00:00 2001
2 From: David Brownell <dbrownell@users.sourceforge.net>
3 Date: Fri, 18 Jan 2008 00:35:20 +0300
4 Subject: [PATCH 22/64] Provide new implementation infrastructure that platforms may choose to use
5 when implementing the GPIO programming interface. Platforms can update their
6 GPIO support to use this. In many cases the incremental cost to access a
7 non-inlined GPIO should be less than a dozen instructions, with the memory
8 cost being about a page (total) of extra data and code. The upside is:
10 * Providing two features which were "want to have (but OK to defer)" when
11 GPIO interfaces were first discussed in November 2006:
13 - A "struct gpio_chip" to plug in GPIOs that aren't directly supported
14 by SOC platforms, but come from FPGAs or other multifunction devices
15 using conventional device registers (like UCB-1x00 or SM501 GPIOs,
16 and southbridges in PCs with more open specs than usual).
18 - Full support for message-based GPIO expanders, where registers are
19 accessed through sleeping I/O calls. Previous support for these
20 "cansleep" calls was just stubs. (One example: the widely used
21 pcf8574 I2C chips, with 8 GPIOs each.)
23 * Including a non-stub implementation of the gpio_{request,free}() calls,
24 making those calls much more useful. The diagnostic labels are also
25 recorded given DEBUG_FS, so /sys/kernel/debug/gpio can show a snapshot
26 of all GPIOs known to this infrastructure.
28 The driver programming interfaces introduced in 2.6.21 do not change at all;
29 this infrastructure is entirely below those covers.
31 Signed-off-by: David Brownell <dbrownell@users.sourceforge.net>
32 Cc: Sam Ravnborg <sam@ravnborg.org>
33 Cc: Jean Delvare <khali@linux-fr.org>
34 Cc: Eric Miao <eric.miao@marvell.com>
35 Cc: Haavard Skinnemoen <hskinnemoen@atmel.com>
36 Cc: Philipp Zabel <philipp.zabel@gmail.com>
37 Cc: Russell King <rmk@arm.linux.org.uk>
38 Cc: Ben Gardner <bgardner@wabtec.com>
39 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
41 drivers/gpio/Makefile | 2 +
42 drivers/gpio/gpiolib.c | 567 ++++++++++++++++++++++++++++++++++++++++++++
43 include/asm-generic/gpio.h | 98 ++++++++
44 3 files changed, 667 insertions(+), 0 deletions(-)
45 create mode 100644 drivers/gpio/gpiolib.c
47 diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
48 index cdbba6b..2db28ce 100644
49 --- a/drivers/gpio/Makefile
50 +++ b/drivers/gpio/Makefile
52 # gpio support: dedicated expander chips, etc
54 ccflags-$(CONFIG_DEBUG_GPIO) += -DDEBUG
56 +obj-$(CONFIG_HAVE_GPIO_LIB) += gpiolib.o
57 diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
59 index 0000000..d8db2f8
61 +++ b/drivers/gpio/gpiolib.c
63 +#include <linux/kernel.h>
64 +#include <linux/module.h>
65 +#include <linux/irq.h>
66 +#include <linux/spinlock.h>
68 +#include <asm/gpio.h>
71 +/* Optional implementation infrastructure for GPIO interfaces.
73 + * Platforms may want to use this if they tend to use very many GPIOs
74 + * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
76 + * When kernel footprint or instruction count is an issue, simpler
77 + * implementations may be preferred. The GPIO programming interface
78 + * allows for inlining speed-critical get/set operations for common
79 + * cases, so that access to SOC-integrated GPIOs can sometimes cost
80 + * only an instruction or two per bit.
84 +/* When debugging, extend minimal trust to callers and platform code.
85 + * Also emit diagnostic messages that may help initial bringup, when
86 + * board setup or driver bugs are most common.
88 + * Otherwise, minimize overhead in what may be bitbanging codepaths.
91 +#define extra_checks 1
93 +#define extra_checks 0
96 +/* gpio_lock prevents conflicts during gpio_desc[] table updates.
97 + * While any GPIO is requested, its gpio_chip is not removable;
98 + * each GPIO's "requested" flag serves as a lock and refcount.
100 +static DEFINE_SPINLOCK(gpio_lock);
103 + struct gpio_chip *chip;
104 + unsigned long flags;
105 +/* flag symbols are bit numbers */
106 +#define FLAG_REQUESTED 0
107 +#define FLAG_IS_OUT 1
109 +#ifdef CONFIG_DEBUG_FS
113 +static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
115 +static inline void desc_set_label(struct gpio_desc *d, const char *label)
117 +#ifdef CONFIG_DEBUG_FS
122 +/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
123 + * when setting direction, and otherwise illegal. Until board setup code
124 + * and drivers use explicit requests everywhere (which won't happen when
125 + * those calls have no teeth) we can't avoid autorequesting. This nag
126 + * message should motivate switching to explicit requests...
128 +static void gpio_ensure_requested(struct gpio_desc *desc)
130 + if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
131 + pr_warning("GPIO-%d autorequested\n", (int)(desc - gpio_desc));
132 + desc_set_label(desc, "[auto]");
136 +/* caller holds gpio_lock *OR* gpio is marked as requested */
137 +static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
139 + return gpio_desc[gpio].chip;
143 + * gpiochip_add() - register a gpio_chip
144 + * @chip: the chip to register, with chip->base initialized
145 + * Context: potentially before irqs or kmalloc will work
147 + * Returns a negative errno if the chip can't be registered, such as
148 + * because the chip->base is invalid or already associated with a
149 + * different chip. Otherwise it returns zero as a success code.
151 +int gpiochip_add(struct gpio_chip *chip)
153 + unsigned long flags;
157 + /* NOTE chip->base negative is reserved to mean a request for
158 + * dynamic allocation. We don't currently support that.
161 + if (chip->base < 0 || (chip->base + chip->ngpio) >= ARCH_NR_GPIOS) {
166 + spin_lock_irqsave(&gpio_lock, flags);
168 + /* these GPIO numbers must not be managed by another gpio_chip */
169 + for (id = chip->base; id < chip->base + chip->ngpio; id++) {
170 + if (gpio_desc[id].chip != NULL) {
176 + for (id = chip->base; id < chip->base + chip->ngpio; id++) {
177 + gpio_desc[id].chip = chip;
178 + gpio_desc[id].flags = 0;
182 + spin_unlock_irqrestore(&gpio_lock, flags);
184 + /* failures here can mean systems won't boot... */
186 + pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n",
187 + chip->base, chip->base + chip->ngpio,
188 + chip->label ? : "generic");
191 +EXPORT_SYMBOL_GPL(gpiochip_add);
194 + * gpiochip_remove() - unregister a gpio_chip
195 + * @chip: the chip to unregister
197 + * A gpio_chip with any GPIOs still requested may not be removed.
199 +int gpiochip_remove(struct gpio_chip *chip)
201 + unsigned long flags;
205 + spin_lock_irqsave(&gpio_lock, flags);
207 + for (id = chip->base; id < chip->base + chip->ngpio; id++) {
208 + if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
214 + for (id = chip->base; id < chip->base + chip->ngpio; id++)
215 + gpio_desc[id].chip = NULL;
218 + spin_unlock_irqrestore(&gpio_lock, flags);
221 +EXPORT_SYMBOL_GPL(gpiochip_remove);
224 +/* These "optional" allocation calls help prevent drivers from stomping
225 + * on each other, and help provide better diagnostics in debugfs.
226 + * They're called even less than the "set direction" calls.
228 +int gpio_request(unsigned gpio, const char *label)
230 + struct gpio_desc *desc;
231 + int status = -EINVAL;
232 + unsigned long flags;
234 + spin_lock_irqsave(&gpio_lock, flags);
236 + if (gpio >= ARCH_NR_GPIOS)
238 + desc = &gpio_desc[gpio];
239 + if (desc->chip == NULL)
242 + /* NOTE: gpio_request() can be called in early boot,
243 + * before IRQs are enabled.
246 + if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
247 + desc_set_label(desc, label ? : "?");
254 + pr_debug("gpio_request: gpio-%d (%s) status %d\n",
255 + gpio, label ? : "?", status);
256 + spin_unlock_irqrestore(&gpio_lock, flags);
259 +EXPORT_SYMBOL_GPL(gpio_request);
261 +void gpio_free(unsigned gpio)
263 + unsigned long flags;
264 + struct gpio_desc *desc;
266 + if (gpio >= ARCH_NR_GPIOS) {
267 + WARN_ON(extra_checks);
271 + spin_lock_irqsave(&gpio_lock, flags);
273 + desc = &gpio_desc[gpio];
274 + if (desc->chip && test_and_clear_bit(FLAG_REQUESTED, &desc->flags))
275 + desc_set_label(desc, NULL);
277 + WARN_ON(extra_checks);
279 + spin_unlock_irqrestore(&gpio_lock, flags);
281 +EXPORT_SYMBOL_GPL(gpio_free);
285 + * gpiochip_is_requested - return string iff signal was requested
286 + * @chip: controller managing the signal
287 + * @offset: of signal within controller's 0..(ngpio - 1) range
289 + * Returns NULL if the GPIO is not currently requested, else a string.
290 + * If debugfs support is enabled, the string returned is the label passed
291 + * to gpio_request(); otherwise it is a meaningless constant.
293 + * This function is for use by GPIO controller drivers. The label can
294 + * help with diagnostics, and knowing that the signal is used as a GPIO
295 + * can help avoid accidentally multiplexing it to another controller.
297 +const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
299 + unsigned gpio = chip->base + offset;
301 + if (gpio >= ARCH_NR_GPIOS || gpio_desc[gpio].chip != chip)
303 + if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
305 +#ifdef CONFIG_DEBUG_FS
306 + return gpio_desc[gpio].label;
311 +EXPORT_SYMBOL_GPL(gpiochip_is_requested);
314 +/* Drivers MUST set GPIO direction before making get/set calls. In
315 + * some cases this is done in early boot, before IRQs are enabled.
317 + * As a rule these aren't called more than once (except for drivers
318 + * using the open-drain emulation idiom) so these are natural places
319 + * to accumulate extra debugging checks. Note that we can't (yet)
320 + * rely on gpio_request() having been called beforehand.
323 +int gpio_direction_input(unsigned gpio)
325 + unsigned long flags;
326 + struct gpio_chip *chip;
327 + struct gpio_desc *desc = &gpio_desc[gpio];
328 + int status = -EINVAL;
330 + spin_lock_irqsave(&gpio_lock, flags);
332 + if (gpio >= ARCH_NR_GPIOS)
335 + if (!chip || !chip->get || !chip->direction_input)
337 + gpio -= chip->base;
338 + if (gpio >= chip->ngpio)
340 + gpio_ensure_requested(desc);
342 + /* now we know the gpio is valid and chip won't vanish */
344 + spin_unlock_irqrestore(&gpio_lock, flags);
346 + might_sleep_if(extra_checks && chip->can_sleep);
348 + status = chip->direction_input(chip, gpio);
350 + clear_bit(FLAG_IS_OUT, &desc->flags);
353 + spin_unlock_irqrestore(&gpio_lock, flags);
355 + pr_debug("%s: gpio-%d status %d\n",
356 + __FUNCTION__, gpio, status);
359 +EXPORT_SYMBOL_GPL(gpio_direction_input);
361 +int gpio_direction_output(unsigned gpio, int value)
363 + unsigned long flags;
364 + struct gpio_chip *chip;
365 + struct gpio_desc *desc = &gpio_desc[gpio];
366 + int status = -EINVAL;
368 + spin_lock_irqsave(&gpio_lock, flags);
370 + if (gpio >= ARCH_NR_GPIOS)
373 + if (!chip || !chip->set || !chip->direction_output)
375 + gpio -= chip->base;
376 + if (gpio >= chip->ngpio)
378 + gpio_ensure_requested(desc);
380 + /* now we know the gpio is valid and chip won't vanish */
382 + spin_unlock_irqrestore(&gpio_lock, flags);
384 + might_sleep_if(extra_checks && chip->can_sleep);
386 + status = chip->direction_output(chip, gpio, value);
388 + set_bit(FLAG_IS_OUT, &desc->flags);
391 + spin_unlock_irqrestore(&gpio_lock, flags);
393 + pr_debug("%s: gpio-%d status %d\n",
394 + __FUNCTION__, gpio, status);
397 +EXPORT_SYMBOL_GPL(gpio_direction_output);
400 +/* I/O calls are only valid after configuration completed; the relevant
401 + * "is this a valid GPIO" error checks should already have been done.
403 + * "Get" operations are often inlinable as reading a pin value register,
404 + * and masking the relevant bit in that register.
406 + * When "set" operations are inlinable, they involve writing that mask to
407 + * one register to set a low value, or a different register to set it high.
408 + * Otherwise locking is needed, so there may be little value to inlining.
410 + *------------------------------------------------------------------------
412 + * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
413 + * have requested the GPIO. That can include implicit requesting by
414 + * a direction setting call. Marking a gpio as requested locks its chip
415 + * in memory, guaranteeing that these table lookups need no more locking
416 + * and that gpiochip_remove() will fail.
418 + * REVISIT when debugging, consider adding some instrumentation to ensure
419 + * that the GPIO was actually requested.
423 + * __gpio_get_value() - return a gpio's value
424 + * @gpio: gpio whose value will be returned
427 + * This is used directly or indirectly to implement gpio_get_value().
428 + * It returns the zero or nonzero value provided by the associated
429 + * gpio_chip.get() method; or zero if no such method is provided.
431 +int __gpio_get_value(unsigned gpio)
433 + struct gpio_chip *chip;
435 + chip = gpio_to_chip(gpio);
436 + WARN_ON(extra_checks && chip->can_sleep);
437 + return chip->get ? chip->get(chip, gpio - chip->base) : 0;
439 +EXPORT_SYMBOL_GPL(__gpio_get_value);
442 + * __gpio_set_value() - assign a gpio's value
443 + * @gpio: gpio whose value will be assigned
444 + * @value: value to assign
447 + * This is used directly or indirectly to implement gpio_set_value().
448 + * It invokes the associated gpio_chip.set() method.
450 +void __gpio_set_value(unsigned gpio, int value)
452 + struct gpio_chip *chip;
454 + chip = gpio_to_chip(gpio);
455 + WARN_ON(extra_checks && chip->can_sleep);
456 + chip->set(chip, gpio - chip->base, value);
458 +EXPORT_SYMBOL_GPL(__gpio_set_value);
461 + * __gpio_cansleep() - report whether gpio value access will sleep
462 + * @gpio: gpio in question
465 + * This is used directly or indirectly to implement gpio_cansleep(). It
466 + * returns nonzero if access reading or writing the GPIO value can sleep.
468 +int __gpio_cansleep(unsigned gpio)
470 + struct gpio_chip *chip;
472 + /* only call this on GPIOs that are valid! */
473 + chip = gpio_to_chip(gpio);
475 + return chip->can_sleep;
477 +EXPORT_SYMBOL_GPL(__gpio_cansleep);
481 +/* There's no value in making it easy to inline GPIO calls that may sleep.
482 + * Common examples include ones connected to I2C or SPI chips.
485 +int gpio_get_value_cansleep(unsigned gpio)
487 + struct gpio_chip *chip;
489 + might_sleep_if(extra_checks);
490 + chip = gpio_to_chip(gpio);
491 + return chip->get(chip, gpio - chip->base);
493 +EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
495 +void gpio_set_value_cansleep(unsigned gpio, int value)
497 + struct gpio_chip *chip;
499 + might_sleep_if(extra_checks);
500 + chip = gpio_to_chip(gpio);
501 + chip->set(chip, gpio - chip->base, value);
503 +EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
506 +#ifdef CONFIG_DEBUG_FS
508 +#include <linux/debugfs.h>
509 +#include <linux/seq_file.h>
512 +static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
515 + unsigned gpio = chip->base;
516 + struct gpio_desc *gdesc = &gpio_desc[gpio];
519 + for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
520 + if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
523 + is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
524 + seq_printf(s, " gpio-%-3d (%-12s) %s %s",
525 + gpio, gdesc->label,
526 + is_out ? "out" : "in ",
528 + ? (chip->get(chip, i) ? "hi" : "lo")
532 + int irq = gpio_to_irq(gpio);
533 + struct irq_desc *desc = irq_desc + irq;
535 + /* This races with request_irq(), set_irq_type(),
536 + * and set_irq_wake() ... but those are "rare".
538 + * More significantly, trigger type flags aren't
539 + * currently maintained by genirq.
541 + if (irq >= 0 && desc->action) {
544 + switch (desc->status & IRQ_TYPE_SENSE_MASK) {
545 + case IRQ_TYPE_NONE:
546 + trigger = "(default)";
548 + case IRQ_TYPE_EDGE_FALLING:
549 + trigger = "edge-falling";
551 + case IRQ_TYPE_EDGE_RISING:
552 + trigger = "edge-rising";
554 + case IRQ_TYPE_EDGE_BOTH:
555 + trigger = "edge-both";
557 + case IRQ_TYPE_LEVEL_HIGH:
558 + trigger = "level-high";
560 + case IRQ_TYPE_LEVEL_LOW:
561 + trigger = "level-low";
564 + trigger = "?trigger?";
568 + seq_printf(s, " irq-%d %s%s",
570 + (desc->status & IRQ_WAKEUP)
575 + seq_printf(s, "\n");
579 +static int gpiolib_show(struct seq_file *s, void *unused)
581 + struct gpio_chip *chip = NULL;
585 + /* REVISIT this isn't locked against gpio_chip removal ... */
587 + for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
588 + if (chip == gpio_desc[gpio].chip)
590 + chip = gpio_desc[gpio].chip;
594 + seq_printf(s, "%sGPIOs %d-%d, %s%s:\n",
595 + started ? "\n" : "",
596 + chip->base, chip->base + chip->ngpio - 1,
597 + chip->label ? : "generic",
598 + chip->can_sleep ? ", can sleep" : "");
600 + if (chip->dbg_show)
601 + chip->dbg_show(s, chip);
603 + gpiolib_dbg_show(s, chip);
608 +static int gpiolib_open(struct inode *inode, struct file *file)
610 + return single_open(file, gpiolib_show, NULL);
613 +static struct file_operations gpiolib_operations = {
614 + .open = gpiolib_open,
616 + .llseek = seq_lseek,
617 + .release = single_release,
620 +static int __init gpiolib_debugfs_init(void)
622 + /* /sys/kernel/debug/gpio */
623 + (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
624 + NULL, NULL, &gpiolib_operations);
627 +subsys_initcall(gpiolib_debugfs_init);
629 +#endif /* DEBUG_FS */
630 diff --git a/include/asm-generic/gpio.h b/include/asm-generic/gpio.h
631 index 2d0aab1..f29a502 100644
632 --- a/include/asm-generic/gpio.h
633 +++ b/include/asm-generic/gpio.h
635 #ifndef _ASM_GENERIC_GPIO_H
636 #define _ASM_GENERIC_GPIO_H
638 +#ifdef CONFIG_HAVE_GPIO_LIB
640 +/* Platforms may implement their GPIO interface with library code,
641 + * at a small performance cost for non-inlined operations and some
642 + * extra memory (for code and for per-GPIO table entries).
644 + * While the GPIO programming interface defines valid GPIO numbers
645 + * to be in the range 0..MAX_INT, this library restricts them to the
646 + * smaller range 0..ARCH_NR_GPIOS.
649 +#ifndef ARCH_NR_GPIOS
650 +#define ARCH_NR_GPIOS 256
656 + * struct gpio_chip - abstract a GPIO controller
657 + * @label: for diagnostics
658 + * @direction_input: configures signal "offset" as input, or returns error
659 + * @get: returns value for signal "offset"; for output signals this
660 + * returns either the value actually sensed, or zero
661 + * @direction_output: configures signal "offset" as output, or returns error
662 + * @set: assigns output value for signal "offset"
663 + * @dbg_show: optional routine to show contents in debugfs; default code
664 + * will be used when this is omitted, but custom code can show extra
665 + * state (such as pullup/pulldown configuration).
666 + * @base: identifies the first GPIO number handled by this chip; or, if
667 + * negative during registration, requests dynamic ID allocation.
668 + * @ngpio: the number of GPIOs handled by this controller; the last GPIO
669 + * handled is (base + ngpio - 1).
670 + * @can_sleep: flag must be set iff get()/set() methods sleep, as they
671 + * must while accessing GPIO expander chips over I2C or SPI
673 + * A gpio_chip can help platforms abstract various sources of GPIOs so
674 + * they can all be accessed through a common programing interface.
675 + * Example sources would be SOC controllers, FPGAs, multifunction
676 + * chips, dedicated GPIO expanders, and so on.
678 + * Each chip controls a number of signals, identified in method calls
679 + * by "offset" values in the range 0..(@ngpio - 1). When those signals
680 + * are referenced through calls like gpio_get_value(gpio), the offset
681 + * is calculated by subtracting @base from the gpio number.
686 + int (*direction_input)(struct gpio_chip *chip,
688 + int (*get)(struct gpio_chip *chip,
690 + int (*direction_output)(struct gpio_chip *chip,
691 + unsigned offset, int value);
692 + void (*set)(struct gpio_chip *chip,
693 + unsigned offset, int value);
694 + void (*dbg_show)(struct seq_file *s,
695 + struct gpio_chip *chip);
698 + unsigned can_sleep:1;
701 +extern const char *gpiochip_is_requested(struct gpio_chip *chip,
704 +/* add/remove chips */
705 +extern int gpiochip_add(struct gpio_chip *chip);
706 +extern int __must_check gpiochip_remove(struct gpio_chip *chip);
709 +/* Always use the library code for GPIO management calls,
710 + * or when sleeping may be involved.
712 +extern int gpio_request(unsigned gpio, const char *label);
713 +extern void gpio_free(unsigned gpio);
715 +extern int gpio_direction_input(unsigned gpio);
716 +extern int gpio_direction_output(unsigned gpio, int value);
718 +extern int gpio_get_value_cansleep(unsigned gpio);
719 +extern void gpio_set_value_cansleep(unsigned gpio, int value);
722 +/* A platform's <asm/gpio.h> code may want to inline the I/O calls when
723 + * the GPIO is constant and refers to some always-present controller,
724 + * giving direct access to chip registers and tight bitbanging loops.
726 +extern int __gpio_get_value(unsigned gpio);
727 +extern void __gpio_set_value(unsigned gpio, int value);
729 +extern int __gpio_cansleep(unsigned gpio);
734 /* platforms that don't directly support access to GPIOs through I2C, SPI,
735 * or other blocking infrastructure can use these wrappers.
737 @@ -22,4 +118,6 @@ static inline void gpio_set_value_cansleep(unsigned gpio, int value)
738 gpio_set_value(gpio, value);
743 #endif /* _ASM_GENERIC_GPIO_H */