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 * 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.
77 static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
79 const struct gpio_chip *chip = desc->chip;
80 const int gpio = chip->base + offset;
82 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
83 "autorequest GPIO-%d\n", gpio)) {
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);
90 desc_set_label(desc, "[auto]");
91 /* caller must chip->request() w/o spinlock */
98 /* caller holds gpio_lock *OR* gpio is marked as requested */
99 static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
101 return gpio_desc[gpio].chip;
104 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
105 static int gpiochip_find_base(int ngpio)
111 for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
112 struct gpio_desc *desc = &gpio_desc[i];
113 struct gpio_chip *chip = desc->chip;
115 if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) {
117 if (spare == ngpio) {
124 i -= chip->ngpio - 1;
128 if (gpio_is_valid(base))
129 pr_debug("%s: found new base at %d\n", __func__, base);
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
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.
144 int __init gpiochip_reserve(int start, int ngpio)
150 if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1))
153 spin_lock_irqsave(&gpio_lock, flags);
155 for (i = start; i < start + ngpio; i++) {
156 struct gpio_desc *desc = &gpio_desc[i];
158 if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) {
163 set_bit(FLAG_RESERVED, &desc->flags);
166 pr_debug("%s: reserved gpios from %d to %d\n",
167 __func__, start, start + ngpio - 1);
169 spin_unlock_irqrestore(&gpio_lock, flags);
174 #ifdef CONFIG_GPIO_SYSFS
176 /* lock protects against unexport_gpio() being called while
177 * sysfs files are active.
179 static DEFINE_MUTEX(sysfs_lock);
182 * /sys/class/gpio/gpioN... only for GPIOs that are exported
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")
189 * * always readable, subject to hardware behavior
190 * * may be writable, as zero/nonzero
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".
197 static ssize_t gpio_direction_show(struct device *dev,
198 struct device_attribute *attr, char *buf)
200 const struct gpio_desc *desc = dev_get_drvdata(dev);
203 mutex_lock(&sysfs_lock);
205 if (!test_bit(FLAG_EXPORT, &desc->flags))
208 status = sprintf(buf, "%s\n",
209 test_bit(FLAG_IS_OUT, &desc->flags)
212 mutex_unlock(&sysfs_lock);
216 static ssize_t gpio_direction_store(struct device *dev,
217 struct device_attribute *attr, const char *buf, size_t size)
219 const struct gpio_desc *desc = dev_get_drvdata(dev);
220 unsigned gpio = desc - gpio_desc;
223 mutex_lock(&sysfs_lock);
225 if (!test_bit(FLAG_EXPORT, &desc->flags))
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);
236 mutex_unlock(&sysfs_lock);
237 return status ? : size;
240 static const DEVICE_ATTR(direction, 0644,
241 gpio_direction_show, gpio_direction_store);
243 static ssize_t gpio_value_show(struct device *dev,
244 struct device_attribute *attr, char *buf)
246 const struct gpio_desc *desc = dev_get_drvdata(dev);
247 unsigned gpio = desc - gpio_desc;
250 mutex_lock(&sysfs_lock);
252 if (!test_bit(FLAG_EXPORT, &desc->flags))
255 status = sprintf(buf, "%d\n", !!gpio_get_value_cansleep(gpio));
257 mutex_unlock(&sysfs_lock);
261 static ssize_t gpio_value_store(struct device *dev,
262 struct device_attribute *attr, const char *buf, size_t size)
264 const struct gpio_desc *desc = dev_get_drvdata(dev);
265 unsigned gpio = desc - gpio_desc;
268 mutex_lock(&sysfs_lock);
270 if (!test_bit(FLAG_EXPORT, &desc->flags))
272 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
277 status = strict_strtol(buf, 0, &value);
279 gpio_set_value_cansleep(gpio, value != 0);
284 mutex_unlock(&sysfs_lock);
288 static /*const*/ DEVICE_ATTR(value, 0644,
289 gpio_value_show, gpio_value_store);
291 static const struct attribute *gpio_attrs[] = {
292 &dev_attr_direction.attr,
293 &dev_attr_value.attr,
297 static const struct attribute_group gpio_attr_group = {
298 .attrs = (struct attribute **) gpio_attrs,
302 * /sys/class/gpio/gpiochipN/
303 * /base ... matching gpio_chip.base (N)
304 * /label ... matching gpio_chip.label
305 * /ngpio ... matching gpio_chip.ngpio
308 static ssize_t chip_base_show(struct device *dev,
309 struct device_attribute *attr, char *buf)
311 const struct gpio_chip *chip = dev_get_drvdata(dev);
313 return sprintf(buf, "%d\n", chip->base);
315 static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
317 static ssize_t chip_label_show(struct device *dev,
318 struct device_attribute *attr, char *buf)
320 const struct gpio_chip *chip = dev_get_drvdata(dev);
322 return sprintf(buf, "%s\n", chip->label ? : "");
324 static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
326 static ssize_t chip_ngpio_show(struct device *dev,
327 struct device_attribute *attr, char *buf)
329 const struct gpio_chip *chip = dev_get_drvdata(dev);
331 return sprintf(buf, "%u\n", chip->ngpio);
333 static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
335 static const struct attribute *gpiochip_attrs[] = {
337 &dev_attr_label.attr,
338 &dev_attr_ngpio.attr,
342 static const struct attribute_group gpiochip_attr_group = {
343 .attrs = (struct attribute **) gpiochip_attrs,
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
352 static ssize_t export_store(struct class *class, const char *buf, size_t len)
357 status = strict_strtol(buf, 0, &gpio);
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.
366 status = gpio_request(gpio, "sysfs");
370 status = gpio_export(gpio, true);
374 set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
378 pr_debug("%s: status %d\n", __func__, status);
379 return status ? : len;
382 static ssize_t unexport_store(struct class *class, const char *buf, size_t len)
387 status = strict_strtol(buf, 0, &gpio);
393 /* reject bogus commands (gpio_unexport ignores them) */
394 if (!gpio_is_valid(gpio))
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.
401 if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
407 pr_debug("%s: status %d\n", __func__, status);
408 return status ? : len;
411 static struct class_attribute gpio_class_attrs[] = {
412 __ATTR(export, 0200, NULL, export_store),
413 __ATTR(unexport, 0200, NULL, unexport_store),
417 static struct class gpio_class = {
419 .owner = THIS_MODULE,
421 .class_attrs = gpio_class_attrs,
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
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.
438 * Returns zero on success, else an error.
440 int gpio_export(unsigned gpio, bool direction_may_change)
443 struct gpio_desc *desc;
444 int status = -EINVAL;
447 /* can't export until sysfs is available ... */
449 pr_debug("%s: called too early!\n", __func__);
453 if (!gpio_is_valid(gpio))
456 mutex_lock(&sysfs_lock);
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)) {
463 if (!desc->chip->direction_input
464 || !desc->chip->direction_output)
465 direction_may_change = false;
467 spin_unlock_irqrestore(&gpio_lock, flags);
469 if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
470 ioname = desc->chip->names[gpio - desc->chip->base];
475 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
476 desc, ioname ? ioname : "gpio%d", gpio);
478 if (direction_may_change)
479 status = sysfs_create_group(&dev->kobj,
482 status = device_create_file(dev,
485 device_unregister(dev);
489 set_bit(FLAG_EXPORT, &desc->flags);
492 mutex_unlock(&sysfs_lock);
496 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
500 EXPORT_SYMBOL_GPL(gpio_export);
502 static int match_export(struct device *dev, void *data)
504 return dev_get_drvdata(dev) == data;
508 * gpio_export_link - create a sysfs link to an exported GPIO node
509 * @dev: device under which to create symlink
510 * @name: name of the symlink
511 * @gpio: gpio to create symlink to, already exported
513 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
514 * node. Caller is responsible for unlinking.
516 * Returns zero on success, else an error.
518 int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
520 struct gpio_desc *desc;
521 int status = -EINVAL;
523 if (!gpio_is_valid(gpio))
526 mutex_lock(&sysfs_lock);
528 desc = &gpio_desc[gpio];
530 if (test_bit(FLAG_EXPORT, &desc->flags)) {
533 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
535 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
542 mutex_unlock(&sysfs_lock);
546 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
550 EXPORT_SYMBOL_GPL(gpio_export_link);
553 * gpio_unexport - reverse effect of gpio_export()
554 * @gpio: gpio to make unavailable
556 * This is implicit on gpio_free().
558 void gpio_unexport(unsigned gpio)
560 struct gpio_desc *desc;
561 int status = -EINVAL;
563 if (!gpio_is_valid(gpio))
566 mutex_lock(&sysfs_lock);
568 desc = &gpio_desc[gpio];
570 if (test_bit(FLAG_EXPORT, &desc->flags)) {
571 struct device *dev = NULL;
573 dev = class_find_device(&gpio_class, NULL, desc, match_export);
575 clear_bit(FLAG_EXPORT, &desc->flags);
577 device_unregister(dev);
583 mutex_unlock(&sysfs_lock);
586 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
588 EXPORT_SYMBOL_GPL(gpio_unexport);
590 static int gpiochip_export(struct gpio_chip *chip)
595 /* Many systems register gpio chips for SOC support very early,
596 * before driver model support is available. In those cases we
597 * export this later, in gpiolib_sysfs_init() ... here we just
598 * verify that _some_ field of gpio_class got initialized.
603 /* use chip->base for the ID; it's already known to be unique */
604 mutex_lock(&sysfs_lock);
605 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
606 "gpiochip%d", chip->base);
608 status = sysfs_create_group(&dev->kobj,
609 &gpiochip_attr_group);
612 chip->exported = (status == 0);
613 mutex_unlock(&sysfs_lock);
619 spin_lock_irqsave(&gpio_lock, flags);
621 while (gpio_desc[gpio].chip == chip)
622 gpio_desc[gpio++].chip = NULL;
623 spin_unlock_irqrestore(&gpio_lock, flags);
625 pr_debug("%s: chip %s status %d\n", __func__,
626 chip->label, status);
632 static void gpiochip_unexport(struct gpio_chip *chip)
637 mutex_lock(&sysfs_lock);
638 dev = class_find_device(&gpio_class, NULL, chip, match_export);
641 device_unregister(dev);
646 mutex_unlock(&sysfs_lock);
649 pr_debug("%s: chip %s status %d\n", __func__,
650 chip->label, status);
653 static int __init gpiolib_sysfs_init(void)
659 status = class_register(&gpio_class);
663 /* Scan and register the gpio_chips which registered very
664 * early (e.g. before the class_register above was called).
666 * We run before arch_initcall() so chip->dev nodes can have
667 * registered, and so arch_initcall() can always gpio_export().
669 spin_lock_irqsave(&gpio_lock, flags);
670 for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
671 struct gpio_chip *chip;
673 chip = gpio_desc[gpio].chip;
674 if (!chip || chip->exported)
677 spin_unlock_irqrestore(&gpio_lock, flags);
678 status = gpiochip_export(chip);
679 spin_lock_irqsave(&gpio_lock, flags);
681 spin_unlock_irqrestore(&gpio_lock, flags);
686 postcore_initcall(gpiolib_sysfs_init);
689 static inline int gpiochip_export(struct gpio_chip *chip)
694 static inline void gpiochip_unexport(struct gpio_chip *chip)
698 #endif /* CONFIG_GPIO_SYSFS */
701 * gpiochip_add() - register a gpio_chip
702 * @chip: the chip to register, with chip->base initialized
703 * Context: potentially before irqs or kmalloc will work
705 * Returns a negative errno if the chip can't be registered, such as
706 * because the chip->base is invalid or already associated with a
707 * different chip. Otherwise it returns zero as a success code.
709 * When gpiochip_add() is called very early during boot, so that GPIOs
710 * can be freely used, the chip->dev device must be registered before
711 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
712 * for GPIOs will fail rudely.
714 * If chip->base is negative, this requests dynamic assignment of
715 * a range of valid GPIOs.
717 int gpiochip_add(struct gpio_chip *chip)
722 int base = chip->base;
724 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
730 spin_lock_irqsave(&gpio_lock, flags);
733 base = gpiochip_find_base(chip->ngpio);
741 /* these GPIO numbers must not be managed by another gpio_chip */
742 for (id = base; id < base + chip->ngpio; id++) {
743 if (gpio_desc[id].chip != NULL) {
749 for (id = base; id < base + chip->ngpio; id++) {
750 gpio_desc[id].chip = chip;
752 /* REVISIT: most hardware initializes GPIOs as
753 * inputs (often with pullups enabled) so power
754 * usage is minimized. Linux code should set the
755 * gpio direction first thing; but until it does,
756 * we may expose the wrong direction in sysfs.
758 gpio_desc[id].flags = !chip->direction_input
765 spin_unlock_irqrestore(&gpio_lock, flags);
767 status = gpiochip_export(chip);
769 /* failures here can mean systems won't boot... */
771 pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n",
772 chip->base, chip->base + chip->ngpio - 1,
773 chip->label ? : "generic");
776 EXPORT_SYMBOL_GPL(gpiochip_add);
779 * gpiochip_remove() - unregister a gpio_chip
780 * @chip: the chip to unregister
782 * A gpio_chip with any GPIOs still requested may not be removed.
784 int gpiochip_remove(struct gpio_chip *chip)
790 spin_lock_irqsave(&gpio_lock, flags);
792 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
793 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
799 for (id = chip->base; id < chip->base + chip->ngpio; id++)
800 gpio_desc[id].chip = NULL;
803 spin_unlock_irqrestore(&gpio_lock, flags);
806 gpiochip_unexport(chip);
810 EXPORT_SYMBOL_GPL(gpiochip_remove);
813 /* These "optional" allocation calls help prevent drivers from stomping
814 * on each other, and help provide better diagnostics in debugfs.
815 * They're called even less than the "set direction" calls.
817 int gpio_request(unsigned gpio, const char *label)
819 struct gpio_desc *desc;
820 struct gpio_chip *chip;
821 int status = -EINVAL;
824 spin_lock_irqsave(&gpio_lock, flags);
826 if (!gpio_is_valid(gpio))
828 desc = &gpio_desc[gpio];
833 if (!try_module_get(chip->owner))
836 /* NOTE: gpio_request() can be called in early boot,
837 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
840 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
841 desc_set_label(desc, label ? : "?");
845 module_put(chip->owner);
850 /* chip->request may sleep */
851 spin_unlock_irqrestore(&gpio_lock, flags);
852 status = chip->request(chip, gpio - chip->base);
853 spin_lock_irqsave(&gpio_lock, flags);
856 desc_set_label(desc, NULL);
857 module_put(chip->owner);
858 clear_bit(FLAG_REQUESTED, &desc->flags);
864 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
865 gpio, label ? : "?", status);
866 spin_unlock_irqrestore(&gpio_lock, flags);
869 EXPORT_SYMBOL_GPL(gpio_request);
871 void gpio_free(unsigned gpio)
874 struct gpio_desc *desc;
875 struct gpio_chip *chip;
879 if (!gpio_is_valid(gpio)) {
880 WARN_ON(extra_checks);
886 spin_lock_irqsave(&gpio_lock, flags);
888 desc = &gpio_desc[gpio];
890 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
892 spin_unlock_irqrestore(&gpio_lock, flags);
893 might_sleep_if(extra_checks && chip->can_sleep);
894 chip->free(chip, gpio - chip->base);
895 spin_lock_irqsave(&gpio_lock, flags);
897 desc_set_label(desc, NULL);
898 module_put(desc->chip->owner);
899 clear_bit(FLAG_REQUESTED, &desc->flags);
901 WARN_ON(extra_checks);
903 spin_unlock_irqrestore(&gpio_lock, flags);
905 EXPORT_SYMBOL_GPL(gpio_free);
909 * gpiochip_is_requested - return string iff signal was requested
910 * @chip: controller managing the signal
911 * @offset: of signal within controller's 0..(ngpio - 1) range
913 * Returns NULL if the GPIO is not currently requested, else a string.
914 * If debugfs support is enabled, the string returned is the label passed
915 * to gpio_request(); otherwise it is a meaningless constant.
917 * This function is for use by GPIO controller drivers. The label can
918 * help with diagnostics, and knowing that the signal is used as a GPIO
919 * can help avoid accidentally multiplexing it to another controller.
921 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
923 unsigned gpio = chip->base + offset;
925 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
927 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
929 #ifdef CONFIG_DEBUG_FS
930 return gpio_desc[gpio].label;
935 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
938 /* Drivers MUST set GPIO direction before making get/set calls. In
939 * some cases this is done in early boot, before IRQs are enabled.
941 * As a rule these aren't called more than once (except for drivers
942 * using the open-drain emulation idiom) so these are natural places
943 * to accumulate extra debugging checks. Note that we can't (yet)
944 * rely on gpio_request() having been called beforehand.
947 int gpio_direction_input(unsigned gpio)
950 struct gpio_chip *chip;
951 struct gpio_desc *desc = &gpio_desc[gpio];
952 int status = -EINVAL;
954 spin_lock_irqsave(&gpio_lock, flags);
956 if (!gpio_is_valid(gpio))
959 if (!chip || !chip->get || !chip->direction_input)
962 if (gpio >= chip->ngpio)
964 status = gpio_ensure_requested(desc, gpio);
968 /* now we know the gpio is valid and chip won't vanish */
970 spin_unlock_irqrestore(&gpio_lock, flags);
972 might_sleep_if(extra_checks && chip->can_sleep);
975 status = chip->request(chip, gpio);
977 pr_debug("GPIO-%d: chip request fail, %d\n",
978 chip->base + gpio, status);
979 /* and it's not available to anyone else ...
980 * gpio_request() is the fully clean solution.
986 status = chip->direction_input(chip, gpio);
988 clear_bit(FLAG_IS_OUT, &desc->flags);
992 spin_unlock_irqrestore(&gpio_lock, flags);
994 pr_debug("%s: gpio-%d status %d\n",
995 __func__, gpio, status);
998 EXPORT_SYMBOL_GPL(gpio_direction_input);
1000 int gpio_direction_output(unsigned gpio, int value)
1002 unsigned long flags;
1003 struct gpio_chip *chip;
1004 struct gpio_desc *desc = &gpio_desc[gpio];
1005 int status = -EINVAL;
1007 spin_lock_irqsave(&gpio_lock, flags);
1009 if (!gpio_is_valid(gpio))
1012 if (!chip || !chip->set || !chip->direction_output)
1015 if (gpio >= chip->ngpio)
1017 status = gpio_ensure_requested(desc, gpio);
1021 /* now we know the gpio is valid and chip won't vanish */
1023 spin_unlock_irqrestore(&gpio_lock, flags);
1025 might_sleep_if(extra_checks && chip->can_sleep);
1028 status = chip->request(chip, gpio);
1030 pr_debug("GPIO-%d: chip request fail, %d\n",
1031 chip->base + gpio, status);
1032 /* and it's not available to anyone else ...
1033 * gpio_request() is the fully clean solution.
1039 status = chip->direction_output(chip, gpio, value);
1041 set_bit(FLAG_IS_OUT, &desc->flags);
1045 spin_unlock_irqrestore(&gpio_lock, flags);
1047 pr_debug("%s: gpio-%d status %d\n",
1048 __func__, gpio, status);
1051 EXPORT_SYMBOL_GPL(gpio_direction_output);
1054 /* I/O calls are only valid after configuration completed; the relevant
1055 * "is this a valid GPIO" error checks should already have been done.
1057 * "Get" operations are often inlinable as reading a pin value register,
1058 * and masking the relevant bit in that register.
1060 * When "set" operations are inlinable, they involve writing that mask to
1061 * one register to set a low value, or a different register to set it high.
1062 * Otherwise locking is needed, so there may be little value to inlining.
1064 *------------------------------------------------------------------------
1066 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1067 * have requested the GPIO. That can include implicit requesting by
1068 * a direction setting call. Marking a gpio as requested locks its chip
1069 * in memory, guaranteeing that these table lookups need no more locking
1070 * and that gpiochip_remove() will fail.
1072 * REVISIT when debugging, consider adding some instrumentation to ensure
1073 * that the GPIO was actually requested.
1077 * __gpio_get_value() - return a gpio's value
1078 * @gpio: gpio whose value will be returned
1081 * This is used directly or indirectly to implement gpio_get_value().
1082 * It returns the zero or nonzero value provided by the associated
1083 * gpio_chip.get() method; or zero if no such method is provided.
1085 int __gpio_get_value(unsigned gpio)
1087 struct gpio_chip *chip;
1089 chip = gpio_to_chip(gpio);
1090 WARN_ON(extra_checks && chip->can_sleep);
1091 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1093 EXPORT_SYMBOL_GPL(__gpio_get_value);
1096 * __gpio_set_value() - assign a gpio's value
1097 * @gpio: gpio whose value will be assigned
1098 * @value: value to assign
1101 * This is used directly or indirectly to implement gpio_set_value().
1102 * It invokes the associated gpio_chip.set() method.
1104 void __gpio_set_value(unsigned gpio, int value)
1106 struct gpio_chip *chip;
1108 chip = gpio_to_chip(gpio);
1109 WARN_ON(extra_checks && chip->can_sleep);
1110 chip->set(chip, gpio - chip->base, value);
1112 EXPORT_SYMBOL_GPL(__gpio_set_value);
1115 * __gpio_cansleep() - report whether gpio value access will sleep
1116 * @gpio: gpio in question
1119 * This is used directly or indirectly to implement gpio_cansleep(). It
1120 * returns nonzero if access reading or writing the GPIO value can sleep.
1122 int __gpio_cansleep(unsigned gpio)
1124 struct gpio_chip *chip;
1126 /* only call this on GPIOs that are valid! */
1127 chip = gpio_to_chip(gpio);
1129 return chip->can_sleep;
1131 EXPORT_SYMBOL_GPL(__gpio_cansleep);
1134 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1135 * @gpio: gpio whose IRQ will be returned (already requested)
1138 * This is used directly or indirectly to implement gpio_to_irq().
1139 * It returns the number of the IRQ signaled by this (input) GPIO,
1140 * or a negative errno.
1142 int __gpio_to_irq(unsigned gpio)
1144 struct gpio_chip *chip;
1146 chip = gpio_to_chip(gpio);
1147 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1149 EXPORT_SYMBOL_GPL(__gpio_to_irq);
1153 /* There's no value in making it easy to inline GPIO calls that may sleep.
1154 * Common examples include ones connected to I2C or SPI chips.
1157 int gpio_get_value_cansleep(unsigned gpio)
1159 struct gpio_chip *chip;
1161 might_sleep_if(extra_checks);
1162 chip = gpio_to_chip(gpio);
1163 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1165 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1167 void gpio_set_value_cansleep(unsigned gpio, int value)
1169 struct gpio_chip *chip;
1171 might_sleep_if(extra_checks);
1172 chip = gpio_to_chip(gpio);
1173 chip->set(chip, gpio - chip->base, value);
1175 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1178 #ifdef CONFIG_DEBUG_FS
1180 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1183 unsigned gpio = chip->base;
1184 struct gpio_desc *gdesc = &gpio_desc[gpio];
1187 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1188 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1191 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
1192 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
1194 is_out ? "out" : "in ",
1196 ? (chip->get(chip, i) ? "hi" : "lo")
1200 int irq = gpio_to_irq(gpio);
1201 struct irq_desc *desc = irq_to_desc(irq);
1203 /* This races with request_irq(), set_irq_type(),
1204 * and set_irq_wake() ... but those are "rare".
1206 * More significantly, trigger type flags aren't
1207 * currently maintained by genirq.
1209 if (irq >= 0 && desc->action) {
1212 switch (desc->status & IRQ_TYPE_SENSE_MASK) {
1214 trigger = "(default)";
1216 case IRQ_TYPE_EDGE_FALLING:
1217 trigger = "edge-falling";
1219 case IRQ_TYPE_EDGE_RISING:
1220 trigger = "edge-rising";
1222 case IRQ_TYPE_EDGE_BOTH:
1223 trigger = "edge-both";
1225 case IRQ_TYPE_LEVEL_HIGH:
1226 trigger = "level-high";
1228 case IRQ_TYPE_LEVEL_LOW:
1229 trigger = "level-low";
1232 trigger = "?trigger?";
1236 seq_printf(s, " irq-%d %s%s",
1238 (desc->status & IRQ_WAKEUP)
1243 seq_printf(s, "\n");
1247 static int gpiolib_show(struct seq_file *s, void *unused)
1249 struct gpio_chip *chip = NULL;
1253 /* REVISIT this isn't locked against gpio_chip removal ... */
1255 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
1258 if (chip == gpio_desc[gpio].chip)
1260 chip = gpio_desc[gpio].chip;
1264 seq_printf(s, "%sGPIOs %d-%d",
1265 started ? "\n" : "",
1266 chip->base, chip->base + chip->ngpio - 1);
1269 seq_printf(s, ", %s/%s",
1270 dev->bus ? dev->bus->name : "no-bus",
1273 seq_printf(s, ", %s", chip->label);
1274 if (chip->can_sleep)
1275 seq_printf(s, ", can sleep");
1276 seq_printf(s, ":\n");
1280 chip->dbg_show(s, chip);
1282 gpiolib_dbg_show(s, chip);
1287 static int gpiolib_open(struct inode *inode, struct file *file)
1289 return single_open(file, gpiolib_show, NULL);
1292 static struct file_operations gpiolib_operations = {
1293 .open = gpiolib_open,
1295 .llseek = seq_lseek,
1296 .release = single_release,
1299 static int __init gpiolib_debugfs_init(void)
1301 /* /sys/kernel/debug/gpio */
1302 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1303 NULL, NULL, &gpiolib_operations);
1306 subsys_initcall(gpiolib_debugfs_init);
1308 #endif /* DEBUG_FS */