]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/gpio/gpiolib.c
gpio: gpio_{request,free}() now required (feature removal)
[net-next-2.6.git] / drivers / gpio / gpiolib.c
CommitLineData
d2876d08
DB
1#include <linux/kernel.h>
2#include <linux/module.h>
3#include <linux/irq.h>
4#include <linux/spinlock.h>
d8f388d8
DB
5#include <linux/device.h>
6#include <linux/err.h>
7#include <linux/debugfs.h>
8#include <linux/seq_file.h>
9#include <linux/gpio.h>
d2876d08
DB
10
11
12/* Optional implementation infrastructure for GPIO interfaces.
13 *
14 * Platforms may want to use this if they tend to use very many GPIOs
15 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
16 *
17 * When kernel footprint or instruction count is an issue, simpler
18 * implementations may be preferred. The GPIO programming interface
19 * allows for inlining speed-critical get/set operations for common
20 * cases, so that access to SOC-integrated GPIOs can sometimes cost
21 * only an instruction or two per bit.
22 */
23
24
25/* When debugging, extend minimal trust to callers and platform code.
26 * Also emit diagnostic messages that may help initial bringup, when
27 * board setup or driver bugs are most common.
28 *
29 * Otherwise, minimize overhead in what may be bitbanging codepaths.
30 */
31#ifdef DEBUG
32#define extra_checks 1
33#else
34#define extra_checks 0
35#endif
36
37/* gpio_lock prevents conflicts during gpio_desc[] table updates.
38 * While any GPIO is requested, its gpio_chip is not removable;
39 * each GPIO's "requested" flag serves as a lock and refcount.
40 */
41static DEFINE_SPINLOCK(gpio_lock);
42
43struct gpio_desc {
44 struct gpio_chip *chip;
45 unsigned long flags;
46/* flag symbols are bit numbers */
47#define FLAG_REQUESTED 0
48#define FLAG_IS_OUT 1
169b6a7a 49#define FLAG_RESERVED 2
d8f388d8
DB
50#define FLAG_EXPORT 3 /* protected by sysfs_lock */
51#define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */
d2876d08
DB
52
53#ifdef CONFIG_DEBUG_FS
54 const char *label;
55#endif
56};
57static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
58
59static inline void desc_set_label(struct gpio_desc *d, const char *label)
60{
61#ifdef CONFIG_DEBUG_FS
62 d->label = label;
63#endif
64}
65
66/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
67 * when setting direction, and otherwise illegal. Until board setup code
68 * and drivers use explicit requests everywhere (which won't happen when
69 * those calls have no teeth) we can't avoid autorequesting. This nag
35e8bb51
DB
70 * message should motivate switching to explicit requests... so should
71 * the weaker cleanup after faults, compared to gpio_request().
8a0cecff
DB
72 *
73 * NOTE: the autorequest mechanism is going away; at this point it's
74 * only "legal" in the sense that (old) code using it won't break yet,
75 * but instead only triggers a WARN() stack dump.
d2876d08 76 */
35e8bb51 77static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
d2876d08 78{
8a0cecff
DB
79 const struct gpio_chip *chip = desc->chip;
80 const int gpio = chip->base + offset;
35e8bb51 81
8a0cecff
DB
82 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
83 "autorequest GPIO-%d\n", gpio)) {
35e8bb51
DB
84 if (!try_module_get(chip->owner)) {
85 pr_err("GPIO-%d: module can't be gotten \n", gpio);
86 clear_bit(FLAG_REQUESTED, &desc->flags);
87 /* lose */
88 return -EIO;
89 }
d2876d08 90 desc_set_label(desc, "[auto]");
35e8bb51
DB
91 /* caller must chip->request() w/o spinlock */
92 if (chip->request)
93 return 1;
d2876d08 94 }
35e8bb51 95 return 0;
d2876d08
DB
96}
97
98/* caller holds gpio_lock *OR* gpio is marked as requested */
99static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
100{
101 return gpio_desc[gpio].chip;
102}
103
8d0aab2f
AV
104/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
105static int gpiochip_find_base(int ngpio)
106{
107 int i;
108 int spare = 0;
109 int base = -ENOSPC;
110
111 for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
169b6a7a
AV
112 struct gpio_desc *desc = &gpio_desc[i];
113 struct gpio_chip *chip = desc->chip;
8d0aab2f 114
169b6a7a 115 if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) {
8d0aab2f
AV
116 spare++;
117 if (spare == ngpio) {
118 base = i;
119 break;
120 }
121 } else {
122 spare = 0;
169b6a7a
AV
123 if (chip)
124 i -= chip->ngpio - 1;
8d0aab2f
AV
125 }
126 }
127
128 if (gpio_is_valid(base))
129 pr_debug("%s: found new base at %d\n", __func__, base);
130 return base;
131}
132
169b6a7a
AV
133/**
134 * gpiochip_reserve() - reserve range of gpios to use with platform code only
135 * @start: starting gpio number
136 * @ngpio: number of gpios to reserve
137 * Context: platform init, potentially before irqs or kmalloc will work
138 *
139 * Returns a negative errno if any gpio within the range is already reserved
140 * or registered, else returns zero as a success code. Use this function
141 * to mark a range of gpios as unavailable for dynamic gpio number allocation,
142 * for example because its driver support is not yet loaded.
143 */
144int __init gpiochip_reserve(int start, int ngpio)
145{
146 int ret = 0;
147 unsigned long flags;
148 int i;
149
bff5fda9 150 if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1))
169b6a7a
AV
151 return -EINVAL;
152
153 spin_lock_irqsave(&gpio_lock, flags);
154
155 for (i = start; i < start + ngpio; i++) {
156 struct gpio_desc *desc = &gpio_desc[i];
157
158 if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) {
159 ret = -EBUSY;
160 goto err;
161 }
162
163 set_bit(FLAG_RESERVED, &desc->flags);
164 }
165
166 pr_debug("%s: reserved gpios from %d to %d\n",
167 __func__, start, start + ngpio - 1);
168err:
169 spin_unlock_irqrestore(&gpio_lock, flags);
170
171 return ret;
172}
173
d8f388d8
DB
174#ifdef CONFIG_GPIO_SYSFS
175
176/* lock protects against unexport_gpio() being called while
177 * sysfs files are active.
178 */
179static DEFINE_MUTEX(sysfs_lock);
180
181/*
182 * /sys/class/gpio/gpioN... only for GPIOs that are exported
183 * /direction
184 * * MAY BE OMITTED if kernel won't allow direction changes
185 * * is read/write as "in" or "out"
186 * * may also be written as "high" or "low", initializing
187 * output value as specified ("out" implies "low")
188 * /value
189 * * always readable, subject to hardware behavior
190 * * may be writable, as zero/nonzero
191 *
192 * REVISIT there will likely be an attribute for configuring async
193 * notifications, e.g. to specify polling interval or IRQ trigger type
194 * that would for example trigger a poll() on the "value".
195 */
196
197static ssize_t gpio_direction_show(struct device *dev,
198 struct device_attribute *attr, char *buf)
199{
200 const struct gpio_desc *desc = dev_get_drvdata(dev);
201 ssize_t status;
202
203 mutex_lock(&sysfs_lock);
204
205 if (!test_bit(FLAG_EXPORT, &desc->flags))
206 status = -EIO;
207 else
208 status = sprintf(buf, "%s\n",
209 test_bit(FLAG_IS_OUT, &desc->flags)
210 ? "out" : "in");
211
212 mutex_unlock(&sysfs_lock);
213 return status;
214}
215
216static ssize_t gpio_direction_store(struct device *dev,
217 struct device_attribute *attr, const char *buf, size_t size)
218{
219 const struct gpio_desc *desc = dev_get_drvdata(dev);
220 unsigned gpio = desc - gpio_desc;
221 ssize_t status;
222
223 mutex_lock(&sysfs_lock);
224
225 if (!test_bit(FLAG_EXPORT, &desc->flags))
226 status = -EIO;
227 else if (sysfs_streq(buf, "high"))
228 status = gpio_direction_output(gpio, 1);
229 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
230 status = gpio_direction_output(gpio, 0);
231 else if (sysfs_streq(buf, "in"))
232 status = gpio_direction_input(gpio);
233 else
234 status = -EINVAL;
235
236 mutex_unlock(&sysfs_lock);
237 return status ? : size;
238}
239
240static const DEVICE_ATTR(direction, 0644,
241 gpio_direction_show, gpio_direction_store);
242
243static ssize_t gpio_value_show(struct device *dev,
244 struct device_attribute *attr, char *buf)
245{
246 const struct gpio_desc *desc = dev_get_drvdata(dev);
247 unsigned gpio = desc - gpio_desc;
248 ssize_t status;
249
250 mutex_lock(&sysfs_lock);
251
252 if (!test_bit(FLAG_EXPORT, &desc->flags))
253 status = -EIO;
254 else
e3274e91 255 status = sprintf(buf, "%d\n", !!gpio_get_value_cansleep(gpio));
d8f388d8
DB
256
257 mutex_unlock(&sysfs_lock);
258 return status;
259}
260
261static ssize_t gpio_value_store(struct device *dev,
262 struct device_attribute *attr, const char *buf, size_t size)
263{
264 const struct gpio_desc *desc = dev_get_drvdata(dev);
265 unsigned gpio = desc - gpio_desc;
266 ssize_t status;
267
268 mutex_lock(&sysfs_lock);
269
270 if (!test_bit(FLAG_EXPORT, &desc->flags))
271 status = -EIO;
272 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
273 status = -EPERM;
274 else {
275 long value;
276
277 status = strict_strtol(buf, 0, &value);
278 if (status == 0) {
279 gpio_set_value_cansleep(gpio, value != 0);
280 status = size;
281 }
282 }
283
284 mutex_unlock(&sysfs_lock);
285 return status;
286}
287
288static /*const*/ DEVICE_ATTR(value, 0644,
289 gpio_value_show, gpio_value_store);
290
291static const struct attribute *gpio_attrs[] = {
292 &dev_attr_direction.attr,
293 &dev_attr_value.attr,
294 NULL,
295};
296
297static const struct attribute_group gpio_attr_group = {
298 .attrs = (struct attribute **) gpio_attrs,
299};
300
301/*
302 * /sys/class/gpio/gpiochipN/
303 * /base ... matching gpio_chip.base (N)
304 * /label ... matching gpio_chip.label
305 * /ngpio ... matching gpio_chip.ngpio
306 */
307
308static ssize_t chip_base_show(struct device *dev,
309 struct device_attribute *attr, char *buf)
310{
311 const struct gpio_chip *chip = dev_get_drvdata(dev);
312
313 return sprintf(buf, "%d\n", chip->base);
314}
315static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
316
317static ssize_t chip_label_show(struct device *dev,
318 struct device_attribute *attr, char *buf)
319{
320 const struct gpio_chip *chip = dev_get_drvdata(dev);
321
322 return sprintf(buf, "%s\n", chip->label ? : "");
323}
324static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
325
326static ssize_t chip_ngpio_show(struct device *dev,
327 struct device_attribute *attr, char *buf)
328{
329 const struct gpio_chip *chip = dev_get_drvdata(dev);
330
331 return sprintf(buf, "%u\n", chip->ngpio);
332}
333static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
334
335static const struct attribute *gpiochip_attrs[] = {
336 &dev_attr_base.attr,
337 &dev_attr_label.attr,
338 &dev_attr_ngpio.attr,
339 NULL,
340};
341
342static const struct attribute_group gpiochip_attr_group = {
343 .attrs = (struct attribute **) gpiochip_attrs,
344};
345
346/*
347 * /sys/class/gpio/export ... write-only
348 * integer N ... number of GPIO to export (full access)
349 * /sys/class/gpio/unexport ... write-only
350 * integer N ... number of GPIO to unexport
351 */
352static ssize_t export_store(struct class *class, const char *buf, size_t len)
353{
354 long gpio;
355 int status;
356
357 status = strict_strtol(buf, 0, &gpio);
358 if (status < 0)
359 goto done;
360
361 /* No extra locking here; FLAG_SYSFS just signifies that the
362 * request and export were done by on behalf of userspace, so
363 * they may be undone on its behalf too.
364 */
365
366 status = gpio_request(gpio, "sysfs");
367 if (status < 0)
368 goto done;
369
370 status = gpio_export(gpio, true);
371 if (status < 0)
372 gpio_free(gpio);
373 else
374 set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
375
376done:
377 if (status)
378 pr_debug("%s: status %d\n", __func__, status);
379 return status ? : len;
380}
381
382static ssize_t unexport_store(struct class *class, const char *buf, size_t len)
383{
384 long gpio;
385 int status;
386
387 status = strict_strtol(buf, 0, &gpio);
388 if (status < 0)
389 goto done;
390
391 status = -EINVAL;
392
393 /* reject bogus commands (gpio_unexport ignores them) */
394 if (!gpio_is_valid(gpio))
395 goto done;
396
397 /* No extra locking here; FLAG_SYSFS just signifies that the
398 * request and export were done by on behalf of userspace, so
399 * they may be undone on its behalf too.
400 */
401 if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
402 status = 0;
403 gpio_free(gpio);
404 }
405done:
406 if (status)
407 pr_debug("%s: status %d\n", __func__, status);
408 return status ? : len;
409}
410
411static struct class_attribute gpio_class_attrs[] = {
412 __ATTR(export, 0200, NULL, export_store),
413 __ATTR(unexport, 0200, NULL, unexport_store),
414 __ATTR_NULL,
415};
416
417static struct class gpio_class = {
418 .name = "gpio",
419 .owner = THIS_MODULE,
420
421 .class_attrs = gpio_class_attrs,
422};
423
424
425/**
426 * gpio_export - export a GPIO through sysfs
427 * @gpio: gpio to make available, already requested
428 * @direction_may_change: true if userspace may change gpio direction
429 * Context: arch_initcall or later
430 *
431 * When drivers want to make a GPIO accessible to userspace after they
432 * have requested it -- perhaps while debugging, or as part of their
433 * public interface -- they may use this routine. If the GPIO can
434 * change direction (some can't) and the caller allows it, userspace
435 * will see "direction" sysfs attribute which may be used to change
436 * the gpio's direction. A "value" attribute will always be provided.
437 *
438 * Returns zero on success, else an error.
439 */
440int gpio_export(unsigned gpio, bool direction_may_change)
441{
442 unsigned long flags;
443 struct gpio_desc *desc;
444 int status = -EINVAL;
926b663c 445 char *ioname = NULL;
d8f388d8
DB
446
447 /* can't export until sysfs is available ... */
448 if (!gpio_class.p) {
449 pr_debug("%s: called too early!\n", __func__);
450 return -ENOENT;
451 }
452
453 if (!gpio_is_valid(gpio))
454 goto done;
455
456 mutex_lock(&sysfs_lock);
457
458 spin_lock_irqsave(&gpio_lock, flags);
459 desc = &gpio_desc[gpio];
460 if (test_bit(FLAG_REQUESTED, &desc->flags)
461 && !test_bit(FLAG_EXPORT, &desc->flags)) {
462 status = 0;
463 if (!desc->chip->direction_input
464 || !desc->chip->direction_output)
465 direction_may_change = false;
466 }
467 spin_unlock_irqrestore(&gpio_lock, flags);
468
926b663c
DS
469 if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
470 ioname = desc->chip->names[gpio - desc->chip->base];
471
d8f388d8
DB
472 if (status == 0) {
473 struct device *dev;
474
475 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
926b663c 476 desc, ioname ? ioname : "gpio%d", gpio);
d8f388d8
DB
477 if (dev) {
478 if (direction_may_change)
479 status = sysfs_create_group(&dev->kobj,
480 &gpio_attr_group);
481 else
482 status = device_create_file(dev,
483 &dev_attr_value);
484 if (status != 0)
485 device_unregister(dev);
486 } else
487 status = -ENODEV;
488 if (status == 0)
489 set_bit(FLAG_EXPORT, &desc->flags);
490 }
491
492 mutex_unlock(&sysfs_lock);
493
494done:
495 if (status)
496 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
497
498 return status;
499}
500EXPORT_SYMBOL_GPL(gpio_export);
501
502static int match_export(struct device *dev, void *data)
503{
504 return dev_get_drvdata(dev) == data;
505}
506
507/**
508 * gpio_unexport - reverse effect of gpio_export()
509 * @gpio: gpio to make unavailable
510 *
511 * This is implicit on gpio_free().
512 */
513void gpio_unexport(unsigned gpio)
514{
515 struct gpio_desc *desc;
516 int status = -EINVAL;
517
518 if (!gpio_is_valid(gpio))
519 goto done;
520
521 mutex_lock(&sysfs_lock);
522
523 desc = &gpio_desc[gpio];
926b663c 524
d8f388d8
DB
525 if (test_bit(FLAG_EXPORT, &desc->flags)) {
526 struct device *dev = NULL;
527
528 dev = class_find_device(&gpio_class, NULL, desc, match_export);
529 if (dev) {
530 clear_bit(FLAG_EXPORT, &desc->flags);
531 put_device(dev);
532 device_unregister(dev);
533 status = 0;
534 } else
535 status = -ENODEV;
536 }
537
538 mutex_unlock(&sysfs_lock);
539done:
540 if (status)
541 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
542}
543EXPORT_SYMBOL_GPL(gpio_unexport);
544
545static int gpiochip_export(struct gpio_chip *chip)
546{
547 int status;
548 struct device *dev;
549
550 /* Many systems register gpio chips for SOC support very early,
551 * before driver model support is available. In those cases we
552 * export this later, in gpiolib_sysfs_init() ... here we just
553 * verify that _some_ field of gpio_class got initialized.
554 */
555 if (!gpio_class.p)
556 return 0;
557
558 /* use chip->base for the ID; it's already known to be unique */
559 mutex_lock(&sysfs_lock);
560 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
561 "gpiochip%d", chip->base);
562 if (dev) {
563 status = sysfs_create_group(&dev->kobj,
564 &gpiochip_attr_group);
565 } else
566 status = -ENODEV;
567 chip->exported = (status == 0);
568 mutex_unlock(&sysfs_lock);
569
570 if (status) {
571 unsigned long flags;
572 unsigned gpio;
573
574 spin_lock_irqsave(&gpio_lock, flags);
575 gpio = chip->base;
576 while (gpio_desc[gpio].chip == chip)
577 gpio_desc[gpio++].chip = NULL;
578 spin_unlock_irqrestore(&gpio_lock, flags);
579
580 pr_debug("%s: chip %s status %d\n", __func__,
581 chip->label, status);
582 }
583
584 return status;
585}
586
587static void gpiochip_unexport(struct gpio_chip *chip)
588{
589 int status;
590 struct device *dev;
591
592 mutex_lock(&sysfs_lock);
593 dev = class_find_device(&gpio_class, NULL, chip, match_export);
594 if (dev) {
595 put_device(dev);
596 device_unregister(dev);
597 chip->exported = 0;
598 status = 0;
599 } else
600 status = -ENODEV;
601 mutex_unlock(&sysfs_lock);
602
603 if (status)
604 pr_debug("%s: chip %s status %d\n", __func__,
605 chip->label, status);
606}
607
608static int __init gpiolib_sysfs_init(void)
609{
610 int status;
611 unsigned long flags;
612 unsigned gpio;
613
614 status = class_register(&gpio_class);
615 if (status < 0)
616 return status;
617
618 /* Scan and register the gpio_chips which registered very
619 * early (e.g. before the class_register above was called).
620 *
621 * We run before arch_initcall() so chip->dev nodes can have
622 * registered, and so arch_initcall() can always gpio_export().
623 */
624 spin_lock_irqsave(&gpio_lock, flags);
625 for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
626 struct gpio_chip *chip;
627
628 chip = gpio_desc[gpio].chip;
629 if (!chip || chip->exported)
630 continue;
631
632 spin_unlock_irqrestore(&gpio_lock, flags);
633 status = gpiochip_export(chip);
634 spin_lock_irqsave(&gpio_lock, flags);
635 }
636 spin_unlock_irqrestore(&gpio_lock, flags);
637
638
639 return status;
640}
641postcore_initcall(gpiolib_sysfs_init);
642
643#else
644static inline int gpiochip_export(struct gpio_chip *chip)
645{
646 return 0;
647}
648
649static inline void gpiochip_unexport(struct gpio_chip *chip)
650{
651}
652
653#endif /* CONFIG_GPIO_SYSFS */
654
d2876d08
DB
655/**
656 * gpiochip_add() - register a gpio_chip
657 * @chip: the chip to register, with chip->base initialized
658 * Context: potentially before irqs or kmalloc will work
659 *
660 * Returns a negative errno if the chip can't be registered, such as
661 * because the chip->base is invalid or already associated with a
662 * different chip. Otherwise it returns zero as a success code.
8d0aab2f 663 *
d8f388d8
DB
664 * When gpiochip_add() is called very early during boot, so that GPIOs
665 * can be freely used, the chip->dev device must be registered before
666 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
667 * for GPIOs will fail rudely.
668 *
8d0aab2f
AV
669 * If chip->base is negative, this requests dynamic assignment of
670 * a range of valid GPIOs.
d2876d08
DB
671 */
672int gpiochip_add(struct gpio_chip *chip)
673{
674 unsigned long flags;
675 int status = 0;
676 unsigned id;
8d0aab2f 677 int base = chip->base;
d2876d08 678
bff5fda9 679 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
8d0aab2f 680 && base >= 0) {
d2876d08
DB
681 status = -EINVAL;
682 goto fail;
683 }
684
685 spin_lock_irqsave(&gpio_lock, flags);
686
8d0aab2f
AV
687 if (base < 0) {
688 base = gpiochip_find_base(chip->ngpio);
689 if (base < 0) {
690 status = base;
d8f388d8 691 goto unlock;
8d0aab2f
AV
692 }
693 chip->base = base;
694 }
695
d2876d08 696 /* these GPIO numbers must not be managed by another gpio_chip */
8d0aab2f 697 for (id = base; id < base + chip->ngpio; id++) {
d2876d08
DB
698 if (gpio_desc[id].chip != NULL) {
699 status = -EBUSY;
700 break;
701 }
702 }
703 if (status == 0) {
8d0aab2f 704 for (id = base; id < base + chip->ngpio; id++) {
d2876d08 705 gpio_desc[id].chip = chip;
d8f388d8
DB
706
707 /* REVISIT: most hardware initializes GPIOs as
708 * inputs (often with pullups enabled) so power
709 * usage is minimized. Linux code should set the
710 * gpio direction first thing; but until it does,
711 * we may expose the wrong direction in sysfs.
712 */
713 gpio_desc[id].flags = !chip->direction_input
714 ? (1 << FLAG_IS_OUT)
715 : 0;
d2876d08
DB
716 }
717 }
718
d8f388d8 719unlock:
d2876d08 720 spin_unlock_irqrestore(&gpio_lock, flags);
d8f388d8
DB
721 if (status == 0)
722 status = gpiochip_export(chip);
d2876d08
DB
723fail:
724 /* failures here can mean systems won't boot... */
725 if (status)
726 pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n",
bff5fda9 727 chip->base, chip->base + chip->ngpio - 1,
d2876d08
DB
728 chip->label ? : "generic");
729 return status;
730}
731EXPORT_SYMBOL_GPL(gpiochip_add);
732
733/**
734 * gpiochip_remove() - unregister a gpio_chip
735 * @chip: the chip to unregister
736 *
737 * A gpio_chip with any GPIOs still requested may not be removed.
738 */
739int gpiochip_remove(struct gpio_chip *chip)
740{
741 unsigned long flags;
742 int status = 0;
743 unsigned id;
744
745 spin_lock_irqsave(&gpio_lock, flags);
746
747 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
748 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
749 status = -EBUSY;
750 break;
751 }
752 }
753 if (status == 0) {
754 for (id = chip->base; id < chip->base + chip->ngpio; id++)
755 gpio_desc[id].chip = NULL;
756 }
757
758 spin_unlock_irqrestore(&gpio_lock, flags);
d8f388d8
DB
759
760 if (status == 0)
761 gpiochip_unexport(chip);
762
d2876d08
DB
763 return status;
764}
765EXPORT_SYMBOL_GPL(gpiochip_remove);
766
767
768/* These "optional" allocation calls help prevent drivers from stomping
769 * on each other, and help provide better diagnostics in debugfs.
770 * They're called even less than the "set direction" calls.
771 */
772int gpio_request(unsigned gpio, const char *label)
773{
774 struct gpio_desc *desc;
35e8bb51 775 struct gpio_chip *chip;
d2876d08
DB
776 int status = -EINVAL;
777 unsigned long flags;
778
779 spin_lock_irqsave(&gpio_lock, flags);
780
e6de1808 781 if (!gpio_is_valid(gpio))
d2876d08
DB
782 goto done;
783 desc = &gpio_desc[gpio];
35e8bb51
DB
784 chip = desc->chip;
785 if (chip == NULL)
d2876d08
DB
786 goto done;
787
35e8bb51 788 if (!try_module_get(chip->owner))
438d8908
GL
789 goto done;
790
d2876d08 791 /* NOTE: gpio_request() can be called in early boot,
35e8bb51 792 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
d2876d08
DB
793 */
794
795 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
796 desc_set_label(desc, label ? : "?");
797 status = 0;
438d8908 798 } else {
d2876d08 799 status = -EBUSY;
35e8bb51 800 module_put(chip->owner);
7460db56 801 goto done;
35e8bb51
DB
802 }
803
804 if (chip->request) {
805 /* chip->request may sleep */
806 spin_unlock_irqrestore(&gpio_lock, flags);
807 status = chip->request(chip, gpio - chip->base);
808 spin_lock_irqsave(&gpio_lock, flags);
809
810 if (status < 0) {
811 desc_set_label(desc, NULL);
812 module_put(chip->owner);
813 clear_bit(FLAG_REQUESTED, &desc->flags);
814 }
438d8908 815 }
d2876d08
DB
816
817done:
818 if (status)
819 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
820 gpio, label ? : "?", status);
821 spin_unlock_irqrestore(&gpio_lock, flags);
822 return status;
823}
824EXPORT_SYMBOL_GPL(gpio_request);
825
826void gpio_free(unsigned gpio)
827{
828 unsigned long flags;
829 struct gpio_desc *desc;
35e8bb51 830 struct gpio_chip *chip;
d2876d08 831
3d599d1c
UKK
832 might_sleep();
833
e6de1808 834 if (!gpio_is_valid(gpio)) {
d2876d08
DB
835 WARN_ON(extra_checks);
836 return;
837 }
838
d8f388d8
DB
839 gpio_unexport(gpio);
840
d2876d08
DB
841 spin_lock_irqsave(&gpio_lock, flags);
842
843 desc = &gpio_desc[gpio];
35e8bb51
DB
844 chip = desc->chip;
845 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
846 if (chip->free) {
847 spin_unlock_irqrestore(&gpio_lock, flags);
848 might_sleep_if(extra_checks && chip->can_sleep);
849 chip->free(chip, gpio - chip->base);
850 spin_lock_irqsave(&gpio_lock, flags);
851 }
d2876d08 852 desc_set_label(desc, NULL);
438d8908 853 module_put(desc->chip->owner);
35e8bb51 854 clear_bit(FLAG_REQUESTED, &desc->flags);
438d8908 855 } else
d2876d08
DB
856 WARN_ON(extra_checks);
857
858 spin_unlock_irqrestore(&gpio_lock, flags);
859}
860EXPORT_SYMBOL_GPL(gpio_free);
861
862
863/**
864 * gpiochip_is_requested - return string iff signal was requested
865 * @chip: controller managing the signal
866 * @offset: of signal within controller's 0..(ngpio - 1) range
867 *
868 * Returns NULL if the GPIO is not currently requested, else a string.
869 * If debugfs support is enabled, the string returned is the label passed
870 * to gpio_request(); otherwise it is a meaningless constant.
871 *
872 * This function is for use by GPIO controller drivers. The label can
873 * help with diagnostics, and knowing that the signal is used as a GPIO
874 * can help avoid accidentally multiplexing it to another controller.
875 */
876const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
877{
878 unsigned gpio = chip->base + offset;
879
e6de1808 880 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
d2876d08
DB
881 return NULL;
882 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
883 return NULL;
884#ifdef CONFIG_DEBUG_FS
885 return gpio_desc[gpio].label;
886#else
887 return "?";
888#endif
889}
890EXPORT_SYMBOL_GPL(gpiochip_is_requested);
891
892
893/* Drivers MUST set GPIO direction before making get/set calls. In
894 * some cases this is done in early boot, before IRQs are enabled.
895 *
896 * As a rule these aren't called more than once (except for drivers
897 * using the open-drain emulation idiom) so these are natural places
898 * to accumulate extra debugging checks. Note that we can't (yet)
899 * rely on gpio_request() having been called beforehand.
900 */
901
902int gpio_direction_input(unsigned gpio)
903{
904 unsigned long flags;
905 struct gpio_chip *chip;
906 struct gpio_desc *desc = &gpio_desc[gpio];
907 int status = -EINVAL;
908
909 spin_lock_irqsave(&gpio_lock, flags);
910
e6de1808 911 if (!gpio_is_valid(gpio))
d2876d08
DB
912 goto fail;
913 chip = desc->chip;
914 if (!chip || !chip->get || !chip->direction_input)
915 goto fail;
916 gpio -= chip->base;
917 if (gpio >= chip->ngpio)
918 goto fail;
35e8bb51
DB
919 status = gpio_ensure_requested(desc, gpio);
920 if (status < 0)
921 goto fail;
d2876d08
DB
922
923 /* now we know the gpio is valid and chip won't vanish */
924
925 spin_unlock_irqrestore(&gpio_lock, flags);
926
927 might_sleep_if(extra_checks && chip->can_sleep);
928
35e8bb51
DB
929 if (status) {
930 status = chip->request(chip, gpio);
931 if (status < 0) {
932 pr_debug("GPIO-%d: chip request fail, %d\n",
933 chip->base + gpio, status);
934 /* and it's not available to anyone else ...
935 * gpio_request() is the fully clean solution.
936 */
937 goto lose;
938 }
939 }
940
d2876d08
DB
941 status = chip->direction_input(chip, gpio);
942 if (status == 0)
943 clear_bit(FLAG_IS_OUT, &desc->flags);
35e8bb51 944lose:
d2876d08
DB
945 return status;
946fail:
947 spin_unlock_irqrestore(&gpio_lock, flags);
948 if (status)
949 pr_debug("%s: gpio-%d status %d\n",
145980a0 950 __func__, gpio, status);
d2876d08
DB
951 return status;
952}
953EXPORT_SYMBOL_GPL(gpio_direction_input);
954
955int gpio_direction_output(unsigned gpio, int value)
956{
957 unsigned long flags;
958 struct gpio_chip *chip;
959 struct gpio_desc *desc = &gpio_desc[gpio];
960 int status = -EINVAL;
961
962 spin_lock_irqsave(&gpio_lock, flags);
963
e6de1808 964 if (!gpio_is_valid(gpio))
d2876d08
DB
965 goto fail;
966 chip = desc->chip;
967 if (!chip || !chip->set || !chip->direction_output)
968 goto fail;
969 gpio -= chip->base;
970 if (gpio >= chip->ngpio)
971 goto fail;
35e8bb51
DB
972 status = gpio_ensure_requested(desc, gpio);
973 if (status < 0)
974 goto fail;
d2876d08
DB
975
976 /* now we know the gpio is valid and chip won't vanish */
977
978 spin_unlock_irqrestore(&gpio_lock, flags);
979
980 might_sleep_if(extra_checks && chip->can_sleep);
981
35e8bb51
DB
982 if (status) {
983 status = chip->request(chip, gpio);
984 if (status < 0) {
985 pr_debug("GPIO-%d: chip request fail, %d\n",
986 chip->base + gpio, status);
987 /* and it's not available to anyone else ...
988 * gpio_request() is the fully clean solution.
989 */
990 goto lose;
991 }
992 }
993
d2876d08
DB
994 status = chip->direction_output(chip, gpio, value);
995 if (status == 0)
996 set_bit(FLAG_IS_OUT, &desc->flags);
35e8bb51 997lose:
d2876d08
DB
998 return status;
999fail:
1000 spin_unlock_irqrestore(&gpio_lock, flags);
1001 if (status)
1002 pr_debug("%s: gpio-%d status %d\n",
145980a0 1003 __func__, gpio, status);
d2876d08
DB
1004 return status;
1005}
1006EXPORT_SYMBOL_GPL(gpio_direction_output);
1007
1008
1009/* I/O calls are only valid after configuration completed; the relevant
1010 * "is this a valid GPIO" error checks should already have been done.
1011 *
1012 * "Get" operations are often inlinable as reading a pin value register,
1013 * and masking the relevant bit in that register.
1014 *
1015 * When "set" operations are inlinable, they involve writing that mask to
1016 * one register to set a low value, or a different register to set it high.
1017 * Otherwise locking is needed, so there may be little value to inlining.
1018 *
1019 *------------------------------------------------------------------------
1020 *
1021 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1022 * have requested the GPIO. That can include implicit requesting by
1023 * a direction setting call. Marking a gpio as requested locks its chip
1024 * in memory, guaranteeing that these table lookups need no more locking
1025 * and that gpiochip_remove() will fail.
1026 *
1027 * REVISIT when debugging, consider adding some instrumentation to ensure
1028 * that the GPIO was actually requested.
1029 */
1030
1031/**
1032 * __gpio_get_value() - return a gpio's value
1033 * @gpio: gpio whose value will be returned
1034 * Context: any
1035 *
1036 * This is used directly or indirectly to implement gpio_get_value().
1037 * It returns the zero or nonzero value provided by the associated
1038 * gpio_chip.get() method; or zero if no such method is provided.
1039 */
1040int __gpio_get_value(unsigned gpio)
1041{
1042 struct gpio_chip *chip;
1043
1044 chip = gpio_to_chip(gpio);
1045 WARN_ON(extra_checks && chip->can_sleep);
1046 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1047}
1048EXPORT_SYMBOL_GPL(__gpio_get_value);
1049
1050/**
1051 * __gpio_set_value() - assign a gpio's value
1052 * @gpio: gpio whose value will be assigned
1053 * @value: value to assign
1054 * Context: any
1055 *
1056 * This is used directly or indirectly to implement gpio_set_value().
1057 * It invokes the associated gpio_chip.set() method.
1058 */
1059void __gpio_set_value(unsigned gpio, int value)
1060{
1061 struct gpio_chip *chip;
1062
1063 chip = gpio_to_chip(gpio);
1064 WARN_ON(extra_checks && chip->can_sleep);
1065 chip->set(chip, gpio - chip->base, value);
1066}
1067EXPORT_SYMBOL_GPL(__gpio_set_value);
1068
1069/**
1070 * __gpio_cansleep() - report whether gpio value access will sleep
1071 * @gpio: gpio in question
1072 * Context: any
1073 *
1074 * This is used directly or indirectly to implement gpio_cansleep(). It
1075 * returns nonzero if access reading or writing the GPIO value can sleep.
1076 */
1077int __gpio_cansleep(unsigned gpio)
1078{
1079 struct gpio_chip *chip;
1080
1081 /* only call this on GPIOs that are valid! */
1082 chip = gpio_to_chip(gpio);
1083
1084 return chip->can_sleep;
1085}
1086EXPORT_SYMBOL_GPL(__gpio_cansleep);
1087
0f6d504e
DB
1088/**
1089 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1090 * @gpio: gpio whose IRQ will be returned (already requested)
1091 * Context: any
1092 *
1093 * This is used directly or indirectly to implement gpio_to_irq().
1094 * It returns the number of the IRQ signaled by this (input) GPIO,
1095 * or a negative errno.
1096 */
1097int __gpio_to_irq(unsigned gpio)
1098{
1099 struct gpio_chip *chip;
1100
1101 chip = gpio_to_chip(gpio);
1102 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1103}
1104EXPORT_SYMBOL_GPL(__gpio_to_irq);
1105
d2876d08
DB
1106
1107
1108/* There's no value in making it easy to inline GPIO calls that may sleep.
1109 * Common examples include ones connected to I2C or SPI chips.
1110 */
1111
1112int gpio_get_value_cansleep(unsigned gpio)
1113{
1114 struct gpio_chip *chip;
1115
1116 might_sleep_if(extra_checks);
1117 chip = gpio_to_chip(gpio);
978ccaa8 1118 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
d2876d08
DB
1119}
1120EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1121
1122void gpio_set_value_cansleep(unsigned gpio, int value)
1123{
1124 struct gpio_chip *chip;
1125
1126 might_sleep_if(extra_checks);
1127 chip = gpio_to_chip(gpio);
1128 chip->set(chip, gpio - chip->base, value);
1129}
1130EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1131
1132
1133#ifdef CONFIG_DEBUG_FS
1134
d2876d08
DB
1135static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1136{
1137 unsigned i;
1138 unsigned gpio = chip->base;
1139 struct gpio_desc *gdesc = &gpio_desc[gpio];
1140 int is_out;
1141
1142 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1143 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1144 continue;
1145
1146 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
6e8ba729 1147 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
d2876d08
DB
1148 gpio, gdesc->label,
1149 is_out ? "out" : "in ",
1150 chip->get
1151 ? (chip->get(chip, i) ? "hi" : "lo")
1152 : "? ");
1153
1154 if (!is_out) {
1155 int irq = gpio_to_irq(gpio);
08678b08 1156 struct irq_desc *desc = irq_to_desc(irq);
d2876d08
DB
1157
1158 /* This races with request_irq(), set_irq_type(),
1159 * and set_irq_wake() ... but those are "rare".
1160 *
1161 * More significantly, trigger type flags aren't
1162 * currently maintained by genirq.
1163 */
1164 if (irq >= 0 && desc->action) {
1165 char *trigger;
1166
1167 switch (desc->status & IRQ_TYPE_SENSE_MASK) {
1168 case IRQ_TYPE_NONE:
1169 trigger = "(default)";
1170 break;
1171 case IRQ_TYPE_EDGE_FALLING:
1172 trigger = "edge-falling";
1173 break;
1174 case IRQ_TYPE_EDGE_RISING:
1175 trigger = "edge-rising";
1176 break;
1177 case IRQ_TYPE_EDGE_BOTH:
1178 trigger = "edge-both";
1179 break;
1180 case IRQ_TYPE_LEVEL_HIGH:
1181 trigger = "level-high";
1182 break;
1183 case IRQ_TYPE_LEVEL_LOW:
1184 trigger = "level-low";
1185 break;
1186 default:
1187 trigger = "?trigger?";
1188 break;
1189 }
1190
1191 seq_printf(s, " irq-%d %s%s",
1192 irq, trigger,
1193 (desc->status & IRQ_WAKEUP)
1194 ? " wakeup" : "");
1195 }
1196 }
1197
1198 seq_printf(s, "\n");
1199 }
1200}
1201
1202static int gpiolib_show(struct seq_file *s, void *unused)
1203{
1204 struct gpio_chip *chip = NULL;
1205 unsigned gpio;
1206 int started = 0;
1207
1208 /* REVISIT this isn't locked against gpio_chip removal ... */
1209
e6de1808 1210 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
d8f388d8
DB
1211 struct device *dev;
1212
d2876d08
DB
1213 if (chip == gpio_desc[gpio].chip)
1214 continue;
1215 chip = gpio_desc[gpio].chip;
1216 if (!chip)
1217 continue;
1218
d8f388d8 1219 seq_printf(s, "%sGPIOs %d-%d",
d2876d08 1220 started ? "\n" : "",
d8f388d8
DB
1221 chip->base, chip->base + chip->ngpio - 1);
1222 dev = chip->dev;
1223 if (dev)
1224 seq_printf(s, ", %s/%s",
1225 dev->bus ? dev->bus->name : "no-bus",
14ab309d 1226 dev_name(dev));
d8f388d8
DB
1227 if (chip->label)
1228 seq_printf(s, ", %s", chip->label);
1229 if (chip->can_sleep)
1230 seq_printf(s, ", can sleep");
1231 seq_printf(s, ":\n");
1232
d2876d08
DB
1233 started = 1;
1234 if (chip->dbg_show)
1235 chip->dbg_show(s, chip);
1236 else
1237 gpiolib_dbg_show(s, chip);
1238 }
1239 return 0;
1240}
1241
1242static int gpiolib_open(struct inode *inode, struct file *file)
1243{
1244 return single_open(file, gpiolib_show, NULL);
1245}
1246
1247static struct file_operations gpiolib_operations = {
1248 .open = gpiolib_open,
1249 .read = seq_read,
1250 .llseek = seq_lseek,
1251 .release = single_release,
1252};
1253
1254static int __init gpiolib_debugfs_init(void)
1255{
1256 /* /sys/kernel/debug/gpio */
1257 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1258 NULL, NULL, &gpiolib_operations);
1259 return 0;
1260}
1261subsys_initcall(gpiolib_debugfs_init);
1262
1263#endif /* DEBUG_FS */