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_unexport - reverse effect of gpio_export()
509 * @gpio: gpio to make unavailable
511 * This is implicit on gpio_free().
513 void gpio_unexport(unsigned gpio)
515 struct gpio_desc *desc;
516 int status = -EINVAL;
518 if (!gpio_is_valid(gpio))
521 mutex_lock(&sysfs_lock);
523 desc = &gpio_desc[gpio];
525 if (test_bit(FLAG_EXPORT, &desc->flags)) {
526 struct device *dev = NULL;
528 dev = class_find_device(&gpio_class, NULL, desc, match_export);
530 clear_bit(FLAG_EXPORT, &desc->flags);
532 device_unregister(dev);
538 mutex_unlock(&sysfs_lock);
541 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
543 EXPORT_SYMBOL_GPL(gpio_unexport);
545 static int gpiochip_export(struct gpio_chip *chip)
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.
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);
563 status = sysfs_create_group(&dev->kobj,
564 &gpiochip_attr_group);
567 chip->exported = (status == 0);
568 mutex_unlock(&sysfs_lock);
574 spin_lock_irqsave(&gpio_lock, flags);
576 while (gpio_desc[gpio].chip == chip)
577 gpio_desc[gpio++].chip = NULL;
578 spin_unlock_irqrestore(&gpio_lock, flags);
580 pr_debug("%s: chip %s status %d\n", __func__,
581 chip->label, status);
587 static void gpiochip_unexport(struct gpio_chip *chip)
592 mutex_lock(&sysfs_lock);
593 dev = class_find_device(&gpio_class, NULL, chip, match_export);
596 device_unregister(dev);
601 mutex_unlock(&sysfs_lock);
604 pr_debug("%s: chip %s status %d\n", __func__,
605 chip->label, status);
608 static int __init gpiolib_sysfs_init(void)
614 status = class_register(&gpio_class);
618 /* Scan and register the gpio_chips which registered very
619 * early (e.g. before the class_register above was called).
621 * We run before arch_initcall() so chip->dev nodes can have
622 * registered, and so arch_initcall() can always gpio_export().
624 spin_lock_irqsave(&gpio_lock, flags);
625 for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
626 struct gpio_chip *chip;
628 chip = gpio_desc[gpio].chip;
629 if (!chip || chip->exported)
632 spin_unlock_irqrestore(&gpio_lock, flags);
633 status = gpiochip_export(chip);
634 spin_lock_irqsave(&gpio_lock, flags);
636 spin_unlock_irqrestore(&gpio_lock, flags);
641 postcore_initcall(gpiolib_sysfs_init);
644 static inline int gpiochip_export(struct gpio_chip *chip)
649 static inline void gpiochip_unexport(struct gpio_chip *chip)
653 #endif /* CONFIG_GPIO_SYSFS */
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
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.
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.
669 * If chip->base is negative, this requests dynamic assignment of
670 * a range of valid GPIOs.
672 int gpiochip_add(struct gpio_chip *chip)
677 int base = chip->base;
679 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
685 spin_lock_irqsave(&gpio_lock, flags);
688 base = gpiochip_find_base(chip->ngpio);
696 /* these GPIO numbers must not be managed by another gpio_chip */
697 for (id = base; id < base + chip->ngpio; id++) {
698 if (gpio_desc[id].chip != NULL) {
704 for (id = base; id < base + chip->ngpio; id++) {
705 gpio_desc[id].chip = chip;
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.
713 gpio_desc[id].flags = !chip->direction_input
720 spin_unlock_irqrestore(&gpio_lock, flags);
722 status = gpiochip_export(chip);
724 /* failures here can mean systems won't boot... */
726 pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n",
727 chip->base, chip->base + chip->ngpio - 1,
728 chip->label ? : "generic");
731 EXPORT_SYMBOL_GPL(gpiochip_add);
734 * gpiochip_remove() - unregister a gpio_chip
735 * @chip: the chip to unregister
737 * A gpio_chip with any GPIOs still requested may not be removed.
739 int gpiochip_remove(struct gpio_chip *chip)
745 spin_lock_irqsave(&gpio_lock, flags);
747 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
748 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
754 for (id = chip->base; id < chip->base + chip->ngpio; id++)
755 gpio_desc[id].chip = NULL;
758 spin_unlock_irqrestore(&gpio_lock, flags);
761 gpiochip_unexport(chip);
765 EXPORT_SYMBOL_GPL(gpiochip_remove);
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.
772 int gpio_request(unsigned gpio, const char *label)
774 struct gpio_desc *desc;
775 struct gpio_chip *chip;
776 int status = -EINVAL;
779 spin_lock_irqsave(&gpio_lock, flags);
781 if (!gpio_is_valid(gpio))
783 desc = &gpio_desc[gpio];
788 if (!try_module_get(chip->owner))
791 /* NOTE: gpio_request() can be called in early boot,
792 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
795 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
796 desc_set_label(desc, label ? : "?");
800 module_put(chip->owner);
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);
811 desc_set_label(desc, NULL);
812 module_put(chip->owner);
813 clear_bit(FLAG_REQUESTED, &desc->flags);
819 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
820 gpio, label ? : "?", status);
821 spin_unlock_irqrestore(&gpio_lock, flags);
824 EXPORT_SYMBOL_GPL(gpio_request);
826 void gpio_free(unsigned gpio)
829 struct gpio_desc *desc;
830 struct gpio_chip *chip;
834 if (!gpio_is_valid(gpio)) {
835 WARN_ON(extra_checks);
841 spin_lock_irqsave(&gpio_lock, flags);
843 desc = &gpio_desc[gpio];
845 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
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);
852 desc_set_label(desc, NULL);
853 module_put(desc->chip->owner);
854 clear_bit(FLAG_REQUESTED, &desc->flags);
856 WARN_ON(extra_checks);
858 spin_unlock_irqrestore(&gpio_lock, flags);
860 EXPORT_SYMBOL_GPL(gpio_free);
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
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.
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.
876 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
878 unsigned gpio = chip->base + offset;
880 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
882 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
884 #ifdef CONFIG_DEBUG_FS
885 return gpio_desc[gpio].label;
890 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
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.
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.
902 int gpio_direction_input(unsigned gpio)
905 struct gpio_chip *chip;
906 struct gpio_desc *desc = &gpio_desc[gpio];
907 int status = -EINVAL;
909 spin_lock_irqsave(&gpio_lock, flags);
911 if (!gpio_is_valid(gpio))
914 if (!chip || !chip->get || !chip->direction_input)
917 if (gpio >= chip->ngpio)
919 status = gpio_ensure_requested(desc, gpio);
923 /* now we know the gpio is valid and chip won't vanish */
925 spin_unlock_irqrestore(&gpio_lock, flags);
927 might_sleep_if(extra_checks && chip->can_sleep);
930 status = chip->request(chip, gpio);
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.
941 status = chip->direction_input(chip, gpio);
943 clear_bit(FLAG_IS_OUT, &desc->flags);
947 spin_unlock_irqrestore(&gpio_lock, flags);
949 pr_debug("%s: gpio-%d status %d\n",
950 __func__, gpio, status);
953 EXPORT_SYMBOL_GPL(gpio_direction_input);
955 int gpio_direction_output(unsigned gpio, int value)
958 struct gpio_chip *chip;
959 struct gpio_desc *desc = &gpio_desc[gpio];
960 int status = -EINVAL;
962 spin_lock_irqsave(&gpio_lock, flags);
964 if (!gpio_is_valid(gpio))
967 if (!chip || !chip->set || !chip->direction_output)
970 if (gpio >= chip->ngpio)
972 status = gpio_ensure_requested(desc, gpio);
976 /* now we know the gpio is valid and chip won't vanish */
978 spin_unlock_irqrestore(&gpio_lock, flags);
980 might_sleep_if(extra_checks && chip->can_sleep);
983 status = chip->request(chip, gpio);
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.
994 status = chip->direction_output(chip, gpio, value);
996 set_bit(FLAG_IS_OUT, &desc->flags);
1000 spin_unlock_irqrestore(&gpio_lock, flags);
1002 pr_debug("%s: gpio-%d status %d\n",
1003 __func__, gpio, status);
1006 EXPORT_SYMBOL_GPL(gpio_direction_output);
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.
1012 * "Get" operations are often inlinable as reading a pin value register,
1013 * and masking the relevant bit in that register.
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.
1019 *------------------------------------------------------------------------
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.
1027 * REVISIT when debugging, consider adding some instrumentation to ensure
1028 * that the GPIO was actually requested.
1032 * __gpio_get_value() - return a gpio's value
1033 * @gpio: gpio whose value will be returned
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.
1040 int __gpio_get_value(unsigned gpio)
1042 struct gpio_chip *chip;
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;
1048 EXPORT_SYMBOL_GPL(__gpio_get_value);
1051 * __gpio_set_value() - assign a gpio's value
1052 * @gpio: gpio whose value will be assigned
1053 * @value: value to assign
1056 * This is used directly or indirectly to implement gpio_set_value().
1057 * It invokes the associated gpio_chip.set() method.
1059 void __gpio_set_value(unsigned gpio, int value)
1061 struct gpio_chip *chip;
1063 chip = gpio_to_chip(gpio);
1064 WARN_ON(extra_checks && chip->can_sleep);
1065 chip->set(chip, gpio - chip->base, value);
1067 EXPORT_SYMBOL_GPL(__gpio_set_value);
1070 * __gpio_cansleep() - report whether gpio value access will sleep
1071 * @gpio: gpio in question
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.
1077 int __gpio_cansleep(unsigned gpio)
1079 struct gpio_chip *chip;
1081 /* only call this on GPIOs that are valid! */
1082 chip = gpio_to_chip(gpio);
1084 return chip->can_sleep;
1086 EXPORT_SYMBOL_GPL(__gpio_cansleep);
1089 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1090 * @gpio: gpio whose IRQ will be returned (already requested)
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.
1097 int __gpio_to_irq(unsigned gpio)
1099 struct gpio_chip *chip;
1101 chip = gpio_to_chip(gpio);
1102 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1104 EXPORT_SYMBOL_GPL(__gpio_to_irq);
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.
1112 int gpio_get_value_cansleep(unsigned gpio)
1114 struct gpio_chip *chip;
1116 might_sleep_if(extra_checks);
1117 chip = gpio_to_chip(gpio);
1118 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1120 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1122 void gpio_set_value_cansleep(unsigned gpio, int value)
1124 struct gpio_chip *chip;
1126 might_sleep_if(extra_checks);
1127 chip = gpio_to_chip(gpio);
1128 chip->set(chip, gpio - chip->base, value);
1130 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1133 #ifdef CONFIG_DEBUG_FS
1135 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1138 unsigned gpio = chip->base;
1139 struct gpio_desc *gdesc = &gpio_desc[gpio];
1142 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1143 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1146 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
1147 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
1149 is_out ? "out" : "in ",
1151 ? (chip->get(chip, i) ? "hi" : "lo")
1155 int irq = gpio_to_irq(gpio);
1156 struct irq_desc *desc = irq_to_desc(irq);
1158 /* This races with request_irq(), set_irq_type(),
1159 * and set_irq_wake() ... but those are "rare".
1161 * More significantly, trigger type flags aren't
1162 * currently maintained by genirq.
1164 if (irq >= 0 && desc->action) {
1167 switch (desc->status & IRQ_TYPE_SENSE_MASK) {
1169 trigger = "(default)";
1171 case IRQ_TYPE_EDGE_FALLING:
1172 trigger = "edge-falling";
1174 case IRQ_TYPE_EDGE_RISING:
1175 trigger = "edge-rising";
1177 case IRQ_TYPE_EDGE_BOTH:
1178 trigger = "edge-both";
1180 case IRQ_TYPE_LEVEL_HIGH:
1181 trigger = "level-high";
1183 case IRQ_TYPE_LEVEL_LOW:
1184 trigger = "level-low";
1187 trigger = "?trigger?";
1191 seq_printf(s, " irq-%d %s%s",
1193 (desc->status & IRQ_WAKEUP)
1198 seq_printf(s, "\n");
1202 static int gpiolib_show(struct seq_file *s, void *unused)
1204 struct gpio_chip *chip = NULL;
1208 /* REVISIT this isn't locked against gpio_chip removal ... */
1210 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
1213 if (chip == gpio_desc[gpio].chip)
1215 chip = gpio_desc[gpio].chip;
1219 seq_printf(s, "%sGPIOs %d-%d",
1220 started ? "\n" : "",
1221 chip->base, chip->base + chip->ngpio - 1);
1224 seq_printf(s, ", %s/%s",
1225 dev->bus ? dev->bus->name : "no-bus",
1228 seq_printf(s, ", %s", chip->label);
1229 if (chip->can_sleep)
1230 seq_printf(s, ", can sleep");
1231 seq_printf(s, ":\n");
1235 chip->dbg_show(s, chip);
1237 gpiolib_dbg_show(s, chip);
1242 static int gpiolib_open(struct inode *inode, struct file *file)
1244 return single_open(file, gpiolib_show, NULL);
1247 static struct file_operations gpiolib_operations = {
1248 .open = gpiolib_open,
1250 .llseek = seq_lseek,
1251 .release = single_release,
1254 static int __init gpiolib_debugfs_init(void)
1256 /* /sys/kernel/debug/gpio */
1257 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1258 NULL, NULL, &gpiolib_operations);
1261 subsys_initcall(gpiolib_debugfs_init);
1263 #endif /* DEBUG_FS */