merge of '0b604857bbf871639fdb43ee8380222e8ef64bb7'
[vuplus_openembedded] / packages / linux / linux-rp-2.6.24 / tosa / 0022-Provide-new-implementation-infrastructure-that-platf.patch
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:
9
10   * Providing two features which were "want to have (but OK to defer)" when
11     GPIO interfaces were first discussed in November 2006:
12
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).
17
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.)
22
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.
27
28 The driver programming interfaces introduced in 2.6.21 do not change at all;
29 this infrastructure is entirely below those covers.
30
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>
40 ---
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
46
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
51 @@ -1,3 +1,5 @@
52  # gpio support: dedicated expander chips, etc
53  
54  ccflags-$(CONFIG_DEBUG_GPIO)   += -DDEBUG
55 +
56 +obj-$(CONFIG_HAVE_GPIO_LIB)    += gpiolib.o
57 diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
58 new file mode 100644
59 index 0000000..d8db2f8
60 --- /dev/null
61 +++ b/drivers/gpio/gpiolib.c
62 @@ -0,0 +1,567 @@
63 +#include <linux/kernel.h>
64 +#include <linux/module.h>
65 +#include <linux/irq.h>
66 +#include <linux/spinlock.h>
67 +
68 +#include <asm/gpio.h>
69 +
70 +
71 +/* Optional implementation infrastructure for GPIO interfaces.
72 + *
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.
75 + *
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.
81 + */
82 +
83 +
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.
87 + *
88 + * Otherwise, minimize overhead in what may be bitbanging codepaths.
89 + */
90 +#ifdef DEBUG
91 +#define        extra_checks    1
92 +#else
93 +#define        extra_checks    0
94 +#endif
95 +
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.
99 + */
100 +static DEFINE_SPINLOCK(gpio_lock);
101 +
102 +struct gpio_desc {
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
108 +
109 +#ifdef CONFIG_DEBUG_FS
110 +       const char              *label;
111 +#endif
112 +};
113 +static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
114 +
115 +static inline void desc_set_label(struct gpio_desc *d, const char *label)
116 +{
117 +#ifdef CONFIG_DEBUG_FS
118 +       d->label = label;
119 +#endif
120 +}
121 +
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...
127 + */
128 +static void gpio_ensure_requested(struct gpio_desc *desc)
129 +{
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]");
133 +       }
134 +}
135 +
136 +/* caller holds gpio_lock *OR* gpio is marked as requested */
137 +static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
138 +{
139 +       return gpio_desc[gpio].chip;
140 +}
141 +
142 +/**
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
146 + *
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.
150 + */
151 +int gpiochip_add(struct gpio_chip *chip)
152 +{
153 +       unsigned long   flags;
154 +       int             status = 0;
155 +       unsigned        id;
156 +
157 +       /* NOTE chip->base negative is reserved to mean a request for
158 +        * dynamic allocation.  We don't currently support that.
159 +        */
160 +
161 +       if (chip->base < 0 || (chip->base  + chip->ngpio) >= ARCH_NR_GPIOS) {
162 +               status = -EINVAL;
163 +               goto fail;
164 +       }
165 +
166 +       spin_lock_irqsave(&gpio_lock, flags);
167 +
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) {
171 +                       status = -EBUSY;
172 +                       break;
173 +               }
174 +       }
175 +       if (status == 0) {
176 +               for (id = chip->base; id < chip->base + chip->ngpio; id++) {
177 +                       gpio_desc[id].chip = chip;
178 +                       gpio_desc[id].flags = 0;
179 +               }
180 +       }
181 +
182 +       spin_unlock_irqrestore(&gpio_lock, flags);
183 +fail:
184 +       /* failures here can mean systems won't boot... */
185 +       if (status)
186 +               pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n",
187 +                       chip->base, chip->base + chip->ngpio,
188 +                       chip->label ? : "generic");
189 +       return status;
190 +}
191 +EXPORT_SYMBOL_GPL(gpiochip_add);
192 +
193 +/**
194 + * gpiochip_remove() - unregister a gpio_chip
195 + * @chip: the chip to unregister
196 + *
197 + * A gpio_chip with any GPIOs still requested may not be removed.
198 + */
199 +int gpiochip_remove(struct gpio_chip *chip)
200 +{
201 +       unsigned long   flags;
202 +       int             status = 0;
203 +       unsigned        id;
204 +
205 +       spin_lock_irqsave(&gpio_lock, flags);
206 +
207 +       for (id = chip->base; id < chip->base + chip->ngpio; id++) {
208 +               if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
209 +                       status = -EBUSY;
210 +                       break;
211 +               }
212 +       }
213 +       if (status == 0) {
214 +               for (id = chip->base; id < chip->base + chip->ngpio; id++)
215 +                       gpio_desc[id].chip = NULL;
216 +       }
217 +
218 +       spin_unlock_irqrestore(&gpio_lock, flags);
219 +       return status;
220 +}
221 +EXPORT_SYMBOL_GPL(gpiochip_remove);
222 +
223 +
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.
227 + */
228 +int gpio_request(unsigned gpio, const char *label)
229 +{
230 +       struct gpio_desc        *desc;
231 +       int                     status = -EINVAL;
232 +       unsigned long           flags;
233 +
234 +       spin_lock_irqsave(&gpio_lock, flags);
235 +
236 +       if (gpio >= ARCH_NR_GPIOS)
237 +               goto done;
238 +       desc = &gpio_desc[gpio];
239 +       if (desc->chip == NULL)
240 +               goto done;
241 +
242 +       /* NOTE:  gpio_request() can be called in early boot,
243 +        * before IRQs are enabled.
244 +        */
245 +
246 +       if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
247 +               desc_set_label(desc, label ? : "?");
248 +               status = 0;
249 +       } else
250 +               status = -EBUSY;
251 +
252 +done:
253 +       if (status)
254 +               pr_debug("gpio_request: gpio-%d (%s) status %d\n",
255 +                       gpio, label ? : "?", status);
256 +       spin_unlock_irqrestore(&gpio_lock, flags);
257 +       return status;
258 +}
259 +EXPORT_SYMBOL_GPL(gpio_request);
260 +
261 +void gpio_free(unsigned gpio)
262 +{
263 +       unsigned long           flags;
264 +       struct gpio_desc        *desc;
265 +
266 +       if (gpio >= ARCH_NR_GPIOS) {
267 +               WARN_ON(extra_checks);
268 +               return;
269 +       }
270 +
271 +       spin_lock_irqsave(&gpio_lock, flags);
272 +
273 +       desc = &gpio_desc[gpio];
274 +       if (desc->chip && test_and_clear_bit(FLAG_REQUESTED, &desc->flags))
275 +               desc_set_label(desc, NULL);
276 +       else
277 +               WARN_ON(extra_checks);
278 +
279 +       spin_unlock_irqrestore(&gpio_lock, flags);
280 +}
281 +EXPORT_SYMBOL_GPL(gpio_free);
282 +
283 +
284 +/**
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
288 + *
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.
292 + *
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.
296 + */
297 +const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
298 +{
299 +       unsigned gpio = chip->base + offset;
300 +
301 +       if (gpio >= ARCH_NR_GPIOS || gpio_desc[gpio].chip != chip)
302 +               return NULL;
303 +       if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
304 +               return NULL;
305 +#ifdef CONFIG_DEBUG_FS
306 +       return gpio_desc[gpio].label;
307 +#else
308 +       return "?";
309 +#endif
310 +}
311 +EXPORT_SYMBOL_GPL(gpiochip_is_requested);
312 +
313 +
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.
316 + *
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.
321 + */
322 +
323 +int gpio_direction_input(unsigned gpio)
324 +{
325 +       unsigned long           flags;
326 +       struct gpio_chip        *chip;
327 +       struct gpio_desc        *desc = &gpio_desc[gpio];
328 +       int                     status = -EINVAL;
329 +
330 +       spin_lock_irqsave(&gpio_lock, flags);
331 +
332 +       if (gpio >= ARCH_NR_GPIOS)
333 +               goto fail;
334 +       chip = desc->chip;
335 +       if (!chip || !chip->get || !chip->direction_input)
336 +               goto fail;
337 +       gpio -= chip->base;
338 +       if (gpio >= chip->ngpio)
339 +               goto fail;
340 +       gpio_ensure_requested(desc);
341 +
342 +       /* now we know the gpio is valid and chip won't vanish */
343 +
344 +       spin_unlock_irqrestore(&gpio_lock, flags);
345 +
346 +       might_sleep_if(extra_checks && chip->can_sleep);
347 +
348 +       status = chip->direction_input(chip, gpio);
349 +       if (status == 0)
350 +               clear_bit(FLAG_IS_OUT, &desc->flags);
351 +       return status;
352 +fail:
353 +       spin_unlock_irqrestore(&gpio_lock, flags);
354 +       if (status)
355 +               pr_debug("%s: gpio-%d status %d\n",
356 +                       __FUNCTION__, gpio, status);
357 +       return status;
358 +}
359 +EXPORT_SYMBOL_GPL(gpio_direction_input);
360 +
361 +int gpio_direction_output(unsigned gpio, int value)
362 +{
363 +       unsigned long           flags;
364 +       struct gpio_chip        *chip;
365 +       struct gpio_desc        *desc = &gpio_desc[gpio];
366 +       int                     status = -EINVAL;
367 +
368 +       spin_lock_irqsave(&gpio_lock, flags);
369 +
370 +       if (gpio >= ARCH_NR_GPIOS)
371 +               goto fail;
372 +       chip = desc->chip;
373 +       if (!chip || !chip->set || !chip->direction_output)
374 +               goto fail;
375 +       gpio -= chip->base;
376 +       if (gpio >= chip->ngpio)
377 +               goto fail;
378 +       gpio_ensure_requested(desc);
379 +
380 +       /* now we know the gpio is valid and chip won't vanish */
381 +
382 +       spin_unlock_irqrestore(&gpio_lock, flags);
383 +
384 +       might_sleep_if(extra_checks && chip->can_sleep);
385 +
386 +       status = chip->direction_output(chip, gpio, value);
387 +       if (status == 0)
388 +               set_bit(FLAG_IS_OUT, &desc->flags);
389 +       return status;
390 +fail:
391 +       spin_unlock_irqrestore(&gpio_lock, flags);
392 +       if (status)
393 +               pr_debug("%s: gpio-%d status %d\n",
394 +                       __FUNCTION__, gpio, status);
395 +       return status;
396 +}
397 +EXPORT_SYMBOL_GPL(gpio_direction_output);
398 +
399 +
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.
402 + *
403 + * "Get" operations are often inlinable as reading a pin value register,
404 + * and masking the relevant bit in that register.
405 + *
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.
409 + *
410 + *------------------------------------------------------------------------
411 + *
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.
417 + *
418 + * REVISIT when debugging, consider adding some instrumentation to ensure
419 + * that the GPIO was actually requested.
420 + */
421 +
422 +/**
423 + * __gpio_get_value() - return a gpio's value
424 + * @gpio: gpio whose value will be returned
425 + * Context: any
426 + *
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.
430 + */
431 +int __gpio_get_value(unsigned gpio)
432 +{
433 +       struct gpio_chip        *chip;
434 +
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;
438 +}
439 +EXPORT_SYMBOL_GPL(__gpio_get_value);
440 +
441 +/**
442 + * __gpio_set_value() - assign a gpio's value
443 + * @gpio: gpio whose value will be assigned
444 + * @value: value to assign
445 + * Context: any
446 + *
447 + * This is used directly or indirectly to implement gpio_set_value().
448 + * It invokes the associated gpio_chip.set() method.
449 + */
450 +void __gpio_set_value(unsigned gpio, int value)
451 +{
452 +       struct gpio_chip        *chip;
453 +
454 +       chip = gpio_to_chip(gpio);
455 +       WARN_ON(extra_checks && chip->can_sleep);
456 +       chip->set(chip, gpio - chip->base, value);
457 +}
458 +EXPORT_SYMBOL_GPL(__gpio_set_value);
459 +
460 +/**
461 + * __gpio_cansleep() - report whether gpio value access will sleep
462 + * @gpio: gpio in question
463 + * Context: any
464 + *
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.
467 + */
468 +int __gpio_cansleep(unsigned gpio)
469 +{
470 +       struct gpio_chip        *chip;
471 +
472 +       /* only call this on GPIOs that are valid! */
473 +       chip = gpio_to_chip(gpio);
474 +
475 +       return chip->can_sleep;
476 +}
477 +EXPORT_SYMBOL_GPL(__gpio_cansleep);
478 +
479 +
480 +
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.
483 + */
484 +
485 +int gpio_get_value_cansleep(unsigned gpio)
486 +{
487 +       struct gpio_chip        *chip;
488 +
489 +       might_sleep_if(extra_checks);
490 +       chip = gpio_to_chip(gpio);
491 +       return chip->get(chip, gpio - chip->base);
492 +}
493 +EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
494 +
495 +void gpio_set_value_cansleep(unsigned gpio, int value)
496 +{
497 +       struct gpio_chip        *chip;
498 +
499 +       might_sleep_if(extra_checks);
500 +       chip = gpio_to_chip(gpio);
501 +       chip->set(chip, gpio - chip->base, value);
502 +}
503 +EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
504 +
505 +
506 +#ifdef CONFIG_DEBUG_FS
507 +
508 +#include <linux/debugfs.h>
509 +#include <linux/seq_file.h>
510 +
511 +
512 +static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
513 +{
514 +       unsigned                i;
515 +       unsigned                gpio = chip->base;
516 +       struct gpio_desc        *gdesc = &gpio_desc[gpio];
517 +       int                     is_out;
518 +
519 +       for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
520 +               if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
521 +                       continue;
522 +
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 ",
527 +                       chip->get
528 +                               ? (chip->get(chip, i) ? "hi" : "lo")
529 +                               : "?  ");
530 +
531 +               if (!is_out) {
532 +                       int             irq = gpio_to_irq(gpio);
533 +                       struct irq_desc *desc = irq_desc + irq;
534 +
535 +                       /* This races with request_irq(), set_irq_type(),
536 +                        * and set_irq_wake() ... but those are "rare".
537 +                        *
538 +                        * More significantly, trigger type flags aren't
539 +                        * currently maintained by genirq.
540 +                        */
541 +                       if (irq >= 0 && desc->action) {
542 +                               char *trigger;
543 +
544 +                               switch (desc->status & IRQ_TYPE_SENSE_MASK) {
545 +                               case IRQ_TYPE_NONE:
546 +                                       trigger = "(default)";
547 +                                       break;
548 +                               case IRQ_TYPE_EDGE_FALLING:
549 +                                       trigger = "edge-falling";
550 +                                       break;
551 +                               case IRQ_TYPE_EDGE_RISING:
552 +                                       trigger = "edge-rising";
553 +                                       break;
554 +                               case IRQ_TYPE_EDGE_BOTH:
555 +                                       trigger = "edge-both";
556 +                                       break;
557 +                               case IRQ_TYPE_LEVEL_HIGH:
558 +                                       trigger = "level-high";
559 +                                       break;
560 +                               case IRQ_TYPE_LEVEL_LOW:
561 +                                       trigger = "level-low";
562 +                                       break;
563 +                               default:
564 +                                       trigger = "?trigger?";
565 +                                       break;
566 +                               }
567 +
568 +                               seq_printf(s, " irq-%d %s%s",
569 +                                       irq, trigger,
570 +                                       (desc->status & IRQ_WAKEUP)
571 +                                               ? " wakeup" : "");
572 +                       }
573 +               }
574 +
575 +               seq_printf(s, "\n");
576 +       }
577 +}
578 +
579 +static int gpiolib_show(struct seq_file *s, void *unused)
580 +{
581 +       struct gpio_chip        *chip = NULL;
582 +       unsigned                gpio;
583 +       int                     started = 0;
584 +
585 +       /* REVISIT this isn't locked against gpio_chip removal ... */
586 +
587 +       for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
588 +               if (chip == gpio_desc[gpio].chip)
589 +                       continue;
590 +               chip = gpio_desc[gpio].chip;
591 +               if (!chip)
592 +                       continue;
593 +
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" : "");
599 +               started = 1;
600 +               if (chip->dbg_show)
601 +                       chip->dbg_show(s, chip);
602 +               else
603 +                       gpiolib_dbg_show(s, chip);
604 +       }
605 +       return 0;
606 +}
607 +
608 +static int gpiolib_open(struct inode *inode, struct file *file)
609 +{
610 +       return single_open(file, gpiolib_show, NULL);
611 +}
612 +
613 +static struct file_operations gpiolib_operations = {
614 +       .open           = gpiolib_open,
615 +       .read           = seq_read,
616 +       .llseek         = seq_lseek,
617 +       .release        = single_release,
618 +};
619 +
620 +static int __init gpiolib_debugfs_init(void)
621 +{
622 +       /* /sys/kernel/debug/gpio */
623 +       (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
624 +                               NULL, NULL, &gpiolib_operations);
625 +       return 0;
626 +}
627 +subsys_initcall(gpiolib_debugfs_init);
628 +
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
634 @@ -1,6 +1,102 @@
635  #ifndef _ASM_GENERIC_GPIO_H
636  #define _ASM_GENERIC_GPIO_H
637  
638 +#ifdef CONFIG_HAVE_GPIO_LIB
639 +
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).
643 + *
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.
647 + */
648 +
649 +#ifndef ARCH_NR_GPIOS
650 +#define ARCH_NR_GPIOS          256
651 +#endif
652 +
653 +struct seq_file;
654 +
655 +/**
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
672 + *
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.
677 + *
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.
682 + */
683 +struct gpio_chip {
684 +       char                    *label;
685 +
686 +       int                     (*direction_input)(struct gpio_chip *chip,
687 +                                               unsigned offset);
688 +       int                     (*get)(struct gpio_chip *chip,
689 +                                               unsigned offset);
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);
696 +       int                     base;
697 +       u16                     ngpio;
698 +       unsigned                can_sleep:1;
699 +};
700 +
701 +extern const char *gpiochip_is_requested(struct gpio_chip *chip,
702 +                       unsigned offset);
703 +
704 +/* add/remove chips */
705 +extern int gpiochip_add(struct gpio_chip *chip);
706 +extern int __must_check gpiochip_remove(struct gpio_chip *chip);
707 +
708 +
709 +/* Always use the library code for GPIO management calls,
710 + * or when sleeping may be involved.
711 + */
712 +extern int gpio_request(unsigned gpio, const char *label);
713 +extern void gpio_free(unsigned gpio);
714 +
715 +extern int gpio_direction_input(unsigned gpio);
716 +extern int gpio_direction_output(unsigned gpio, int value);
717 +
718 +extern int gpio_get_value_cansleep(unsigned gpio);
719 +extern void gpio_set_value_cansleep(unsigned gpio, int value);
720 +
721 +
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.
725 + */
726 +extern int __gpio_get_value(unsigned gpio);
727 +extern void __gpio_set_value(unsigned gpio, int value);
728 +
729 +extern int __gpio_cansleep(unsigned gpio);
730 +
731 +
732 +#else
733 +
734  /* platforms that don't directly support access to GPIOs through I2C, SPI,
735   * or other blocking infrastructure can use these wrappers.
736   */
737 @@ -22,4 +118,6 @@ static inline void gpio_set_value_cansleep(unsigned gpio, int value)
738         gpio_set_value(gpio, value);
739  }
740  
741 +#endif
742 +
743  #endif /* _ASM_GENERIC_GPIO_H */
744 -- 
745 1.5.3.8
746