1 #include <linux/kernel.h>
2 #include <linux/module.h>
4 #include <linux/spinlock.h>
5 #include <linux/device.h>
7 #include <linux/debugfs.h>
8 #include <linux/seq_file.h>
9 #include <linux/gpio.h>
12 /* Optional implementation infrastructure for GPIO interfaces.
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.
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.
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.
29 * Otherwise, minimize overhead in what may be bitbanging codepaths.
32 #define extra_checks 1
34 #define extra_checks 0
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.
41 static DEFINE_SPINLOCK(gpio_lock);
44 struct gpio_chip *chip;
46 /* flag symbols are bit numbers */
47 #define FLAG_REQUESTED 0
49 #define FLAG_RESERVED 2
50 #define FLAG_EXPORT 3 /* protected by sysfs_lock */
51 #define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */
53 #ifdef CONFIG_DEBUG_FS
57 static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
59 static inline void desc_set_label(struct gpio_desc *d, const char *label)
61 #ifdef CONFIG_DEBUG_FS
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
70 * message should motivate switching to explicit requests... so should
71 * the weaker cleanup after faults, compared to gpio_request().
73 static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
75 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
76 struct gpio_chip *chip = desc->chip;
77 int gpio = chip->base + offset;
79 if (!try_module_get(chip->owner)) {
80 pr_err("GPIO-%d: module can't be gotten \n", gpio);
81 clear_bit(FLAG_REQUESTED, &desc->flags);
85 pr_warning("GPIO-%d autorequested\n", gpio);
86 desc_set_label(desc, "[auto]");
87 /* caller must chip->request() w/o spinlock */
94 /* caller holds gpio_lock *OR* gpio is marked as requested */
95 static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
97 return gpio_desc[gpio].chip;
100 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
101 static int gpiochip_find_base(int ngpio)
107 for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
108 struct gpio_desc *desc = &gpio_desc[i];
109 struct gpio_chip *chip = desc->chip;
111 if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) {
113 if (spare == ngpio) {
120 i -= chip->ngpio - 1;
124 if (gpio_is_valid(base))
125 pr_debug("%s: found new base at %d\n", __func__, base);
130 * gpiochip_reserve() - reserve range of gpios to use with platform code only
131 * @start: starting gpio number
132 * @ngpio: number of gpios to reserve
133 * Context: platform init, potentially before irqs or kmalloc will work
135 * Returns a negative errno if any gpio within the range is already reserved
136 * or registered, else returns zero as a success code. Use this function
137 * to mark a range of gpios as unavailable for dynamic gpio number allocation,
138 * for example because its driver support is not yet loaded.
140 int __init gpiochip_reserve(int start, int ngpio)
146 if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1))
149 spin_lock_irqsave(&gpio_lock, flags);
151 for (i = start; i < start + ngpio; i++) {
152 struct gpio_desc *desc = &gpio_desc[i];
154 if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) {
159 set_bit(FLAG_RESERVED, &desc->flags);
162 pr_debug("%s: reserved gpios from %d to %d\n",
163 __func__, start, start + ngpio - 1);
165 spin_unlock_irqrestore(&gpio_lock, flags);
170 #ifdef CONFIG_GPIO_SYSFS
172 /* lock protects against unexport_gpio() being called while
173 * sysfs files are active.
175 static DEFINE_MUTEX(sysfs_lock);
178 * /sys/class/gpio/gpioN... only for GPIOs that are exported
180 * * MAY BE OMITTED if kernel won't allow direction changes
181 * * is read/write as "in" or "out"
182 * * may also be written as "high" or "low", initializing
183 * output value as specified ("out" implies "low")
185 * * always readable, subject to hardware behavior
186 * * may be writable, as zero/nonzero
188 * REVISIT there will likely be an attribute for configuring async
189 * notifications, e.g. to specify polling interval or IRQ trigger type
190 * that would for example trigger a poll() on the "value".
193 static ssize_t gpio_direction_show(struct device *dev,
194 struct device_attribute *attr, char *buf)
196 const struct gpio_desc *desc = dev_get_drvdata(dev);
199 mutex_lock(&sysfs_lock);
201 if (!test_bit(FLAG_EXPORT, &desc->flags))
204 status = sprintf(buf, "%s\n",
205 test_bit(FLAG_IS_OUT, &desc->flags)
208 mutex_unlock(&sysfs_lock);
212 static ssize_t gpio_direction_store(struct device *dev,
213 struct device_attribute *attr, const char *buf, size_t size)
215 const struct gpio_desc *desc = dev_get_drvdata(dev);
216 unsigned gpio = desc - gpio_desc;
219 mutex_lock(&sysfs_lock);
221 if (!test_bit(FLAG_EXPORT, &desc->flags))
223 else if (sysfs_streq(buf, "high"))
224 status = gpio_direction_output(gpio, 1);
225 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
226 status = gpio_direction_output(gpio, 0);
227 else if (sysfs_streq(buf, "in"))
228 status = gpio_direction_input(gpio);
232 mutex_unlock(&sysfs_lock);
233 return status ? : size;
236 static const DEVICE_ATTR(direction, 0644,
237 gpio_direction_show, gpio_direction_store);
239 static ssize_t gpio_value_show(struct device *dev,
240 struct device_attribute *attr, char *buf)
242 const struct gpio_desc *desc = dev_get_drvdata(dev);
243 unsigned gpio = desc - gpio_desc;
246 mutex_lock(&sysfs_lock);
248 if (!test_bit(FLAG_EXPORT, &desc->flags))
251 status = sprintf(buf, "%d\n", gpio_get_value_cansleep(gpio));
253 mutex_unlock(&sysfs_lock);
257 static ssize_t gpio_value_store(struct device *dev,
258 struct device_attribute *attr, const char *buf, size_t size)
260 const struct gpio_desc *desc = dev_get_drvdata(dev);
261 unsigned gpio = desc - gpio_desc;
264 mutex_lock(&sysfs_lock);
266 if (!test_bit(FLAG_EXPORT, &desc->flags))
268 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
273 status = strict_strtol(buf, 0, &value);
275 gpio_set_value_cansleep(gpio, value != 0);
280 mutex_unlock(&sysfs_lock);
284 static /*const*/ DEVICE_ATTR(value, 0644,
285 gpio_value_show, gpio_value_store);
287 static const struct attribute *gpio_attrs[] = {
288 &dev_attr_direction.attr,
289 &dev_attr_value.attr,
293 static const struct attribute_group gpio_attr_group = {
294 .attrs = (struct attribute **) gpio_attrs,
298 * /sys/class/gpio/gpiochipN/
299 * /base ... matching gpio_chip.base (N)
300 * /label ... matching gpio_chip.label
301 * /ngpio ... matching gpio_chip.ngpio
304 static ssize_t chip_base_show(struct device *dev,
305 struct device_attribute *attr, char *buf)
307 const struct gpio_chip *chip = dev_get_drvdata(dev);
309 return sprintf(buf, "%d\n", chip->base);
311 static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
313 static ssize_t chip_label_show(struct device *dev,
314 struct device_attribute *attr, char *buf)
316 const struct gpio_chip *chip = dev_get_drvdata(dev);
318 return sprintf(buf, "%s\n", chip->label ? : "");
320 static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
322 static ssize_t chip_ngpio_show(struct device *dev,
323 struct device_attribute *attr, char *buf)
325 const struct gpio_chip *chip = dev_get_drvdata(dev);
327 return sprintf(buf, "%u\n", chip->ngpio);
329 static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
331 static const struct attribute *gpiochip_attrs[] = {
333 &dev_attr_label.attr,
334 &dev_attr_ngpio.attr,
338 static const struct attribute_group gpiochip_attr_group = {
339 .attrs = (struct attribute **) gpiochip_attrs,
343 * /sys/class/gpio/export ... write-only
344 * integer N ... number of GPIO to export (full access)
345 * /sys/class/gpio/unexport ... write-only
346 * integer N ... number of GPIO to unexport
348 static ssize_t export_store(struct class *class, const char *buf, size_t len)
353 status = strict_strtol(buf, 0, &gpio);
357 /* No extra locking here; FLAG_SYSFS just signifies that the
358 * request and export were done by on behalf of userspace, so
359 * they may be undone on its behalf too.
362 status = gpio_request(gpio, "sysfs");
366 status = gpio_export(gpio, true);
370 set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
374 pr_debug("%s: status %d\n", __func__, status);
375 return status ? : len;
378 static ssize_t unexport_store(struct class *class, const char *buf, size_t len)
383 status = strict_strtol(buf, 0, &gpio);
389 /* reject bogus commands (gpio_unexport ignores them) */
390 if (!gpio_is_valid(gpio))
393 /* No extra locking here; FLAG_SYSFS just signifies that the
394 * request and export were done by on behalf of userspace, so
395 * they may be undone on its behalf too.
397 if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
403 pr_debug("%s: status %d\n", __func__, status);
404 return status ? : len;
407 static struct class_attribute gpio_class_attrs[] = {
408 __ATTR(export, 0200, NULL, export_store),
409 __ATTR(unexport, 0200, NULL, unexport_store),
413 static struct class gpio_class = {
415 .owner = THIS_MODULE,
417 .class_attrs = gpio_class_attrs,
422 * gpio_export - export a GPIO through sysfs
423 * @gpio: gpio to make available, already requested
424 * @direction_may_change: true if userspace may change gpio direction
425 * Context: arch_initcall or later
427 * When drivers want to make a GPIO accessible to userspace after they
428 * have requested it -- perhaps while debugging, or as part of their
429 * public interface -- they may use this routine. If the GPIO can
430 * change direction (some can't) and the caller allows it, userspace
431 * will see "direction" sysfs attribute which may be used to change
432 * the gpio's direction. A "value" attribute will always be provided.
434 * Returns zero on success, else an error.
436 int gpio_export(unsigned gpio, bool direction_may_change)
439 struct gpio_desc *desc;
440 int status = -EINVAL;
442 /* can't export until sysfs is available ... */
444 pr_debug("%s: called too early!\n", __func__);
448 if (!gpio_is_valid(gpio))
451 mutex_lock(&sysfs_lock);
453 spin_lock_irqsave(&gpio_lock, flags);
454 desc = &gpio_desc[gpio];
455 if (test_bit(FLAG_REQUESTED, &desc->flags)
456 && !test_bit(FLAG_EXPORT, &desc->flags)) {
458 if (!desc->chip->direction_input
459 || !desc->chip->direction_output)
460 direction_may_change = false;
462 spin_unlock_irqrestore(&gpio_lock, flags);
467 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
468 desc, "gpio%d", gpio);
470 if (direction_may_change)
471 status = sysfs_create_group(&dev->kobj,
474 status = device_create_file(dev,
477 device_unregister(dev);
481 set_bit(FLAG_EXPORT, &desc->flags);
484 mutex_unlock(&sysfs_lock);
488 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
492 EXPORT_SYMBOL_GPL(gpio_export);
494 static int match_export(struct device *dev, void *data)
496 return dev_get_drvdata(dev) == data;
500 * gpio_unexport - reverse effect of gpio_export()
501 * @gpio: gpio to make unavailable
503 * This is implicit on gpio_free().
505 void gpio_unexport(unsigned gpio)
507 struct gpio_desc *desc;
508 int status = -EINVAL;
510 if (!gpio_is_valid(gpio))
513 mutex_lock(&sysfs_lock);
515 desc = &gpio_desc[gpio];
516 if (test_bit(FLAG_EXPORT, &desc->flags)) {
517 struct device *dev = NULL;
519 dev = class_find_device(&gpio_class, NULL, desc, match_export);
521 clear_bit(FLAG_EXPORT, &desc->flags);
523 device_unregister(dev);
529 mutex_unlock(&sysfs_lock);
532 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
534 EXPORT_SYMBOL_GPL(gpio_unexport);
536 static int gpiochip_export(struct gpio_chip *chip)
541 /* Many systems register gpio chips for SOC support very early,
542 * before driver model support is available. In those cases we
543 * export this later, in gpiolib_sysfs_init() ... here we just
544 * verify that _some_ field of gpio_class got initialized.
549 /* use chip->base for the ID; it's already known to be unique */
550 mutex_lock(&sysfs_lock);
551 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
552 "gpiochip%d", chip->base);
554 status = sysfs_create_group(&dev->kobj,
555 &gpiochip_attr_group);
558 chip->exported = (status == 0);
559 mutex_unlock(&sysfs_lock);
565 spin_lock_irqsave(&gpio_lock, flags);
567 while (gpio_desc[gpio].chip == chip)
568 gpio_desc[gpio++].chip = NULL;
569 spin_unlock_irqrestore(&gpio_lock, flags);
571 pr_debug("%s: chip %s status %d\n", __func__,
572 chip->label, status);
578 static void gpiochip_unexport(struct gpio_chip *chip)
583 mutex_lock(&sysfs_lock);
584 dev = class_find_device(&gpio_class, NULL, chip, match_export);
587 device_unregister(dev);
592 mutex_unlock(&sysfs_lock);
595 pr_debug("%s: chip %s status %d\n", __func__,
596 chip->label, status);
599 static int __init gpiolib_sysfs_init(void)
605 status = class_register(&gpio_class);
609 /* Scan and register the gpio_chips which registered very
610 * early (e.g. before the class_register above was called).
612 * We run before arch_initcall() so chip->dev nodes can have
613 * registered, and so arch_initcall() can always gpio_export().
615 spin_lock_irqsave(&gpio_lock, flags);
616 for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
617 struct gpio_chip *chip;
619 chip = gpio_desc[gpio].chip;
620 if (!chip || chip->exported)
623 spin_unlock_irqrestore(&gpio_lock, flags);
624 status = gpiochip_export(chip);
625 spin_lock_irqsave(&gpio_lock, flags);
627 spin_unlock_irqrestore(&gpio_lock, flags);
632 postcore_initcall(gpiolib_sysfs_init);
635 static inline int gpiochip_export(struct gpio_chip *chip)
640 static inline void gpiochip_unexport(struct gpio_chip *chip)
644 #endif /* CONFIG_GPIO_SYSFS */
647 * gpiochip_add() - register a gpio_chip
648 * @chip: the chip to register, with chip->base initialized
649 * Context: potentially before irqs or kmalloc will work
651 * Returns a negative errno if the chip can't be registered, such as
652 * because the chip->base is invalid or already associated with a
653 * different chip. Otherwise it returns zero as a success code.
655 * When gpiochip_add() is called very early during boot, so that GPIOs
656 * can be freely used, the chip->dev device must be registered before
657 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
658 * for GPIOs will fail rudely.
660 * If chip->base is negative, this requests dynamic assignment of
661 * a range of valid GPIOs.
663 int gpiochip_add(struct gpio_chip *chip)
668 int base = chip->base;
670 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
676 spin_lock_irqsave(&gpio_lock, flags);
679 base = gpiochip_find_base(chip->ngpio);
687 /* these GPIO numbers must not be managed by another gpio_chip */
688 for (id = base; id < base + chip->ngpio; id++) {
689 if (gpio_desc[id].chip != NULL) {
695 for (id = base; id < base + chip->ngpio; id++) {
696 gpio_desc[id].chip = chip;
698 /* REVISIT: most hardware initializes GPIOs as
699 * inputs (often with pullups enabled) so power
700 * usage is minimized. Linux code should set the
701 * gpio direction first thing; but until it does,
702 * we may expose the wrong direction in sysfs.
704 gpio_desc[id].flags = !chip->direction_input
711 spin_unlock_irqrestore(&gpio_lock, flags);
713 status = gpiochip_export(chip);
715 /* failures here can mean systems won't boot... */
717 pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n",
718 chip->base, chip->base + chip->ngpio - 1,
719 chip->label ? : "generic");
722 EXPORT_SYMBOL_GPL(gpiochip_add);
725 * gpiochip_remove() - unregister a gpio_chip
726 * @chip: the chip to unregister
728 * A gpio_chip with any GPIOs still requested may not be removed.
730 int gpiochip_remove(struct gpio_chip *chip)
736 spin_lock_irqsave(&gpio_lock, flags);
738 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
739 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
745 for (id = chip->base; id < chip->base + chip->ngpio; id++)
746 gpio_desc[id].chip = NULL;
749 spin_unlock_irqrestore(&gpio_lock, flags);
752 gpiochip_unexport(chip);
756 EXPORT_SYMBOL_GPL(gpiochip_remove);
759 /* These "optional" allocation calls help prevent drivers from stomping
760 * on each other, and help provide better diagnostics in debugfs.
761 * They're called even less than the "set direction" calls.
763 int gpio_request(unsigned gpio, const char *label)
765 struct gpio_desc *desc;
766 struct gpio_chip *chip;
767 int status = -EINVAL;
770 spin_lock_irqsave(&gpio_lock, flags);
772 if (!gpio_is_valid(gpio))
774 desc = &gpio_desc[gpio];
779 if (!try_module_get(chip->owner))
782 /* NOTE: gpio_request() can be called in early boot,
783 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
786 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
787 desc_set_label(desc, label ? : "?");
791 module_put(chip->owner);
795 /* chip->request may sleep */
796 spin_unlock_irqrestore(&gpio_lock, flags);
797 status = chip->request(chip, gpio - chip->base);
798 spin_lock_irqsave(&gpio_lock, flags);
801 desc_set_label(desc, NULL);
802 module_put(chip->owner);
803 clear_bit(FLAG_REQUESTED, &desc->flags);
809 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
810 gpio, label ? : "?", status);
811 spin_unlock_irqrestore(&gpio_lock, flags);
814 EXPORT_SYMBOL_GPL(gpio_request);
816 void gpio_free(unsigned gpio)
819 struct gpio_desc *desc;
820 struct gpio_chip *chip;
822 if (!gpio_is_valid(gpio)) {
823 WARN_ON(extra_checks);
829 spin_lock_irqsave(&gpio_lock, flags);
831 desc = &gpio_desc[gpio];
833 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
835 spin_unlock_irqrestore(&gpio_lock, flags);
836 might_sleep_if(extra_checks && chip->can_sleep);
837 chip->free(chip, gpio - chip->base);
838 spin_lock_irqsave(&gpio_lock, flags);
840 desc_set_label(desc, NULL);
841 module_put(desc->chip->owner);
842 clear_bit(FLAG_REQUESTED, &desc->flags);
844 WARN_ON(extra_checks);
846 spin_unlock_irqrestore(&gpio_lock, flags);
848 EXPORT_SYMBOL_GPL(gpio_free);
852 * gpiochip_is_requested - return string iff signal was requested
853 * @chip: controller managing the signal
854 * @offset: of signal within controller's 0..(ngpio - 1) range
856 * Returns NULL if the GPIO is not currently requested, else a string.
857 * If debugfs support is enabled, the string returned is the label passed
858 * to gpio_request(); otherwise it is a meaningless constant.
860 * This function is for use by GPIO controller drivers. The label can
861 * help with diagnostics, and knowing that the signal is used as a GPIO
862 * can help avoid accidentally multiplexing it to another controller.
864 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
866 unsigned gpio = chip->base + offset;
868 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
870 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
872 #ifdef CONFIG_DEBUG_FS
873 return gpio_desc[gpio].label;
878 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
881 /* Drivers MUST set GPIO direction before making get/set calls. In
882 * some cases this is done in early boot, before IRQs are enabled.
884 * As a rule these aren't called more than once (except for drivers
885 * using the open-drain emulation idiom) so these are natural places
886 * to accumulate extra debugging checks. Note that we can't (yet)
887 * rely on gpio_request() having been called beforehand.
890 int gpio_direction_input(unsigned gpio)
893 struct gpio_chip *chip;
894 struct gpio_desc *desc = &gpio_desc[gpio];
895 int status = -EINVAL;
897 spin_lock_irqsave(&gpio_lock, flags);
899 if (!gpio_is_valid(gpio))
902 if (!chip || !chip->get || !chip->direction_input)
905 if (gpio >= chip->ngpio)
907 status = gpio_ensure_requested(desc, gpio);
911 /* now we know the gpio is valid and chip won't vanish */
913 spin_unlock_irqrestore(&gpio_lock, flags);
915 might_sleep_if(extra_checks && chip->can_sleep);
918 status = chip->request(chip, gpio);
920 pr_debug("GPIO-%d: chip request fail, %d\n",
921 chip->base + gpio, status);
922 /* and it's not available to anyone else ...
923 * gpio_request() is the fully clean solution.
929 status = chip->direction_input(chip, gpio);
931 clear_bit(FLAG_IS_OUT, &desc->flags);
935 spin_unlock_irqrestore(&gpio_lock, flags);
937 pr_debug("%s: gpio-%d status %d\n",
938 __func__, gpio, status);
941 EXPORT_SYMBOL_GPL(gpio_direction_input);
943 int gpio_direction_output(unsigned gpio, int value)
946 struct gpio_chip *chip;
947 struct gpio_desc *desc = &gpio_desc[gpio];
948 int status = -EINVAL;
950 spin_lock_irqsave(&gpio_lock, flags);
952 if (!gpio_is_valid(gpio))
955 if (!chip || !chip->set || !chip->direction_output)
958 if (gpio >= chip->ngpio)
960 status = gpio_ensure_requested(desc, gpio);
964 /* now we know the gpio is valid and chip won't vanish */
966 spin_unlock_irqrestore(&gpio_lock, flags);
968 might_sleep_if(extra_checks && chip->can_sleep);
971 status = chip->request(chip, gpio);
973 pr_debug("GPIO-%d: chip request fail, %d\n",
974 chip->base + gpio, status);
975 /* and it's not available to anyone else ...
976 * gpio_request() is the fully clean solution.
982 status = chip->direction_output(chip, gpio, value);
984 set_bit(FLAG_IS_OUT, &desc->flags);
988 spin_unlock_irqrestore(&gpio_lock, flags);
990 pr_debug("%s: gpio-%d status %d\n",
991 __func__, gpio, status);
994 EXPORT_SYMBOL_GPL(gpio_direction_output);
997 /* I/O calls are only valid after configuration completed; the relevant
998 * "is this a valid GPIO" error checks should already have been done.
1000 * "Get" operations are often inlinable as reading a pin value register,
1001 * and masking the relevant bit in that register.
1003 * When "set" operations are inlinable, they involve writing that mask to
1004 * one register to set a low value, or a different register to set it high.
1005 * Otherwise locking is needed, so there may be little value to inlining.
1007 *------------------------------------------------------------------------
1009 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1010 * have requested the GPIO. That can include implicit requesting by
1011 * a direction setting call. Marking a gpio as requested locks its chip
1012 * in memory, guaranteeing that these table lookups need no more locking
1013 * and that gpiochip_remove() will fail.
1015 * REVISIT when debugging, consider adding some instrumentation to ensure
1016 * that the GPIO was actually requested.
1020 * __gpio_get_value() - return a gpio's value
1021 * @gpio: gpio whose value will be returned
1024 * This is used directly or indirectly to implement gpio_get_value().
1025 * It returns the zero or nonzero value provided by the associated
1026 * gpio_chip.get() method; or zero if no such method is provided.
1028 int __gpio_get_value(unsigned gpio)
1030 struct gpio_chip *chip;
1032 chip = gpio_to_chip(gpio);
1033 WARN_ON(extra_checks && chip->can_sleep);
1034 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1036 EXPORT_SYMBOL_GPL(__gpio_get_value);
1039 * __gpio_set_value() - assign a gpio's value
1040 * @gpio: gpio whose value will be assigned
1041 * @value: value to assign
1044 * This is used directly or indirectly to implement gpio_set_value().
1045 * It invokes the associated gpio_chip.set() method.
1047 void __gpio_set_value(unsigned gpio, int value)
1049 struct gpio_chip *chip;
1051 chip = gpio_to_chip(gpio);
1052 WARN_ON(extra_checks && chip->can_sleep);
1053 chip->set(chip, gpio - chip->base, value);
1055 EXPORT_SYMBOL_GPL(__gpio_set_value);
1058 * __gpio_cansleep() - report whether gpio value access will sleep
1059 * @gpio: gpio in question
1062 * This is used directly or indirectly to implement gpio_cansleep(). It
1063 * returns nonzero if access reading or writing the GPIO value can sleep.
1065 int __gpio_cansleep(unsigned gpio)
1067 struct gpio_chip *chip;
1069 /* only call this on GPIOs that are valid! */
1070 chip = gpio_to_chip(gpio);
1072 return chip->can_sleep;
1074 EXPORT_SYMBOL_GPL(__gpio_cansleep);
1077 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1078 * @gpio: gpio whose IRQ will be returned (already requested)
1081 * This is used directly or indirectly to implement gpio_to_irq().
1082 * It returns the number of the IRQ signaled by this (input) GPIO,
1083 * or a negative errno.
1085 int __gpio_to_irq(unsigned gpio)
1087 struct gpio_chip *chip;
1089 chip = gpio_to_chip(gpio);
1090 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1092 EXPORT_SYMBOL_GPL(__gpio_to_irq);
1096 /* There's no value in making it easy to inline GPIO calls that may sleep.
1097 * Common examples include ones connected to I2C or SPI chips.
1100 int gpio_get_value_cansleep(unsigned gpio)
1102 struct gpio_chip *chip;
1104 might_sleep_if(extra_checks);
1105 chip = gpio_to_chip(gpio);
1106 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1108 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1110 void gpio_set_value_cansleep(unsigned gpio, int value)
1112 struct gpio_chip *chip;
1114 might_sleep_if(extra_checks);
1115 chip = gpio_to_chip(gpio);
1116 chip->set(chip, gpio - chip->base, value);
1118 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1121 #ifdef CONFIG_DEBUG_FS
1123 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1126 unsigned gpio = chip->base;
1127 struct gpio_desc *gdesc = &gpio_desc[gpio];
1130 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1131 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1134 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
1135 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
1137 is_out ? "out" : "in ",
1139 ? (chip->get(chip, i) ? "hi" : "lo")
1143 int irq = gpio_to_irq(gpio);
1144 struct irq_desc *desc = irq_desc + irq;
1146 /* This races with request_irq(), set_irq_type(),
1147 * and set_irq_wake() ... but those are "rare".
1149 * More significantly, trigger type flags aren't
1150 * currently maintained by genirq.
1152 if (irq >= 0 && desc->action) {
1155 switch (desc->status & IRQ_TYPE_SENSE_MASK) {
1157 trigger = "(default)";
1159 case IRQ_TYPE_EDGE_FALLING:
1160 trigger = "edge-falling";
1162 case IRQ_TYPE_EDGE_RISING:
1163 trigger = "edge-rising";
1165 case IRQ_TYPE_EDGE_BOTH:
1166 trigger = "edge-both";
1168 case IRQ_TYPE_LEVEL_HIGH:
1169 trigger = "level-high";
1171 case IRQ_TYPE_LEVEL_LOW:
1172 trigger = "level-low";
1175 trigger = "?trigger?";
1179 seq_printf(s, " irq-%d %s%s",
1181 (desc->status & IRQ_WAKEUP)
1186 seq_printf(s, "\n");
1190 static int gpiolib_show(struct seq_file *s, void *unused)
1192 struct gpio_chip *chip = NULL;
1196 /* REVISIT this isn't locked against gpio_chip removal ... */
1198 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
1201 if (chip == gpio_desc[gpio].chip)
1203 chip = gpio_desc[gpio].chip;
1207 seq_printf(s, "%sGPIOs %d-%d",
1208 started ? "\n" : "",
1209 chip->base, chip->base + chip->ngpio - 1);
1212 seq_printf(s, ", %s/%s",
1213 dev->bus ? dev->bus->name : "no-bus",
1216 seq_printf(s, ", %s", chip->label);
1217 if (chip->can_sleep)
1218 seq_printf(s, ", can sleep");
1219 seq_printf(s, ":\n");
1223 chip->dbg_show(s, chip);
1225 gpiolib_dbg_show(s, chip);
1230 static int gpiolib_open(struct inode *inode, struct file *file)
1232 return single_open(file, gpiolib_show, NULL);
1235 static struct file_operations gpiolib_operations = {
1236 .open = gpiolib_open,
1238 .llseek = seq_lseek,
1239 .release = single_release,
1242 static int __init gpiolib_debugfs_init(void)
1244 /* /sys/kernel/debug/gpio */
1245 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1246 NULL, NULL, &gpiolib_operations);
1249 subsys_initcall(gpiolib_debugfs_init);
1251 #endif /* DEBUG_FS */