1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/interrupt.h>
5 #include <linux/spinlock.h>
6 #include <linux/list.h>
7 #include <linux/device.h>
9 #include <linux/debugfs.h>
10 #include <linux/seq_file.h>
11 #include <linux/gpio.h>
12 #include <linux/of_gpio.h>
13 #include <linux/idr.h>
14 #include <linux/slab.h>
16 #define CREATE_TRACE_POINTS
17 #include <trace/events/gpio.h>
19 /* Optional implementation infrastructure for GPIO interfaces.
21 * Platforms may want to use this if they tend to use very many GPIOs
22 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
24 * When kernel footprint or instruction count is an issue, simpler
25 * implementations may be preferred. The GPIO programming interface
26 * allows for inlining speed-critical get/set operations for common
27 * cases, so that access to SOC-integrated GPIOs can sometimes cost
28 * only an instruction or two per bit.
32 /* When debugging, extend minimal trust to callers and platform code.
33 * Also emit diagnostic messages that may help initial bringup, when
34 * board setup or driver bugs are most common.
36 * Otherwise, minimize overhead in what may be bitbanging codepaths.
39 #define extra_checks 1
41 #define extra_checks 0
44 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
45 * While any GPIO is requested, its gpio_chip is not removable;
46 * each GPIO's "requested" flag serves as a lock and refcount.
48 static DEFINE_SPINLOCK(gpio_lock);
51 struct gpio_chip *chip;
53 /* flag symbols are bit numbers */
54 #define FLAG_REQUESTED 0
56 #define FLAG_EXPORT 2 /* protected by sysfs_lock */
57 #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
58 #define FLAG_TRIG_FALL 4 /* trigger on falling edge */
59 #define FLAG_TRIG_RISE 5 /* trigger on rising edge */
60 #define FLAG_ACTIVE_LOW 6 /* sysfs value has active low */
61 #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
62 #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
64 #define ID_SHIFT 16 /* add new flags before this one */
66 #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
67 #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
69 #ifdef CONFIG_DEBUG_FS
73 static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
75 #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
77 static LIST_HEAD(gpio_chips);
79 #ifdef CONFIG_GPIO_SYSFS
80 static DEFINE_IDR(dirent_idr);
84 * Internal gpiod_* API using descriptors instead of the integer namespace.
85 * Most of this should eventually go public.
87 static int gpiod_request(struct gpio_desc *desc, const char *label);
88 static void gpiod_free(struct gpio_desc *desc);
89 static int gpiod_direction_input(struct gpio_desc *desc);
90 static int gpiod_direction_output(struct gpio_desc *desc, int value);
91 static int gpiod_get_direction(const struct gpio_desc *desc);
92 static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
93 static int gpiod_get_value_cansleep(const struct gpio_desc *desc);
94 static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
95 static int gpiod_get_value(const struct gpio_desc *desc);
96 static void gpiod_set_value(struct gpio_desc *desc, int value);
97 static int gpiod_cansleep(const struct gpio_desc *desc);
98 static int gpiod_to_irq(const struct gpio_desc *desc);
99 static int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
100 static int gpiod_export_link(struct device *dev, const char *name,
101 struct gpio_desc *desc);
102 static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
103 static void gpiod_unexport(struct gpio_desc *desc);
105 #define gpiod_emerg(desc, fmt, ...) \
106 pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
107 #define gpiod_crit(desc, fmt, ...) \
108 pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
109 #define gpiod_err(desc, fmt, ...) \
110 pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
111 #define gpiod_warn(desc, fmt, ...) \
112 pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
113 #define gpiod_info(desc, fmt, ...) \
114 pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
115 #define gpiod_dbg(desc, fmt, ...) \
116 pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
118 static inline void desc_set_label(struct gpio_desc *d, const char *label)
120 #ifdef CONFIG_DEBUG_FS
126 * Return the GPIO number of the passed descriptor relative to its chip
128 static int gpio_chip_hwgpio(const struct gpio_desc *desc)
130 return desc - &desc->chip->desc[0];
134 * Convert a GPIO number to its descriptor
136 static struct gpio_desc *gpio_to_desc(unsigned gpio)
138 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
141 return &gpio_desc[gpio];
145 * Convert a GPIO descriptor to the integer namespace.
146 * This should disappear in the future but is needed since we still
147 * use GPIO numbers for error messages and sysfs nodes
149 static int desc_to_gpio(const struct gpio_desc *desc)
151 return desc->chip->base + gpio_chip_hwgpio(desc);
155 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
156 * when setting direction, and otherwise illegal. Until board setup code
157 * and drivers use explicit requests everywhere (which won't happen when
158 * those calls have no teeth) we can't avoid autorequesting. This nag
159 * message should motivate switching to explicit requests... so should
160 * the weaker cleanup after faults, compared to gpio_request().
162 * NOTE: the autorequest mechanism is going away; at this point it's
163 * only "legal" in the sense that (old) code using it won't break yet,
164 * but instead only triggers a WARN() stack dump.
166 static int gpio_ensure_requested(struct gpio_desc *desc)
168 const struct gpio_chip *chip = desc->chip;
169 const int gpio = desc_to_gpio(desc);
171 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
172 "autorequest GPIO-%d\n", gpio)) {
173 if (!try_module_get(chip->owner)) {
174 pr_err("GPIO-%d: module can't be gotten \n", gpio);
175 clear_bit(FLAG_REQUESTED, &desc->flags);
179 desc_set_label(desc, "[auto]");
180 /* caller must chip->request() w/o spinlock */
187 static struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
189 return desc ? desc->chip : NULL;
192 /* caller holds gpio_lock *OR* gpio is marked as requested */
193 struct gpio_chip *gpio_to_chip(unsigned gpio)
195 return gpiod_to_chip(gpio_to_desc(gpio));
198 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
199 static int gpiochip_find_base(int ngpio)
201 struct gpio_chip *chip;
202 int base = ARCH_NR_GPIOS - ngpio;
204 list_for_each_entry_reverse(chip, &gpio_chips, list) {
205 /* found a free space? */
206 if (chip->base + chip->ngpio <= base)
209 /* nope, check the space right before the chip */
210 base = chip->base - ngpio;
213 if (gpio_is_valid(base)) {
214 pr_debug("%s: found new base at %d\n", __func__, base);
217 pr_err("%s: cannot find free range\n", __func__);
222 /* caller ensures gpio is valid and requested, chip->get_direction may sleep */
223 static int gpiod_get_direction(const struct gpio_desc *desc)
225 struct gpio_chip *chip;
227 int status = -EINVAL;
229 chip = gpiod_to_chip(desc);
230 offset = gpio_chip_hwgpio(desc);
232 if (!chip->get_direction)
235 status = chip->get_direction(chip, offset);
237 /* GPIOF_DIR_IN, or other positive */
239 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
240 * so it does not affect constness per se */
241 clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
245 set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
250 #ifdef CONFIG_GPIO_SYSFS
252 /* lock protects against unexport_gpio() being called while
253 * sysfs files are active.
255 static DEFINE_MUTEX(sysfs_lock);
258 * /sys/class/gpio/gpioN... only for GPIOs that are exported
260 * * MAY BE OMITTED if kernel won't allow direction changes
261 * * is read/write as "in" or "out"
262 * * may also be written as "high" or "low", initializing
263 * output value as specified ("out" implies "low")
265 * * always readable, subject to hardware behavior
266 * * may be writable, as zero/nonzero
268 * * configures behavior of poll(2) on /value
269 * * available only if pin can generate IRQs on input
270 * * is read/write as "none", "falling", "rising", or "both"
272 * * configures polarity of /value
273 * * is read/write as zero/nonzero
274 * * also affects existing and subsequent "falling" and "rising"
275 * /edge configuration
278 static ssize_t gpio_direction_show(struct device *dev,
279 struct device_attribute *attr, char *buf)
281 const struct gpio_desc *desc = dev_get_drvdata(dev);
284 mutex_lock(&sysfs_lock);
286 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
289 gpiod_get_direction(desc);
290 status = sprintf(buf, "%s\n",
291 test_bit(FLAG_IS_OUT, &desc->flags)
295 mutex_unlock(&sysfs_lock);
299 static ssize_t gpio_direction_store(struct device *dev,
300 struct device_attribute *attr, const char *buf, size_t size)
302 struct gpio_desc *desc = dev_get_drvdata(dev);
305 mutex_lock(&sysfs_lock);
307 if (!test_bit(FLAG_EXPORT, &desc->flags))
309 else if (sysfs_streq(buf, "high"))
310 status = gpiod_direction_output(desc, 1);
311 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
312 status = gpiod_direction_output(desc, 0);
313 else if (sysfs_streq(buf, "in"))
314 status = gpiod_direction_input(desc);
318 mutex_unlock(&sysfs_lock);
319 return status ? : size;
322 static /* const */ DEVICE_ATTR(direction, 0644,
323 gpio_direction_show, gpio_direction_store);
325 static ssize_t gpio_value_show(struct device *dev,
326 struct device_attribute *attr, char *buf)
328 struct gpio_desc *desc = dev_get_drvdata(dev);
331 mutex_lock(&sysfs_lock);
333 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
338 value = !!gpiod_get_value_cansleep(desc);
339 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
342 status = sprintf(buf, "%d\n", value);
345 mutex_unlock(&sysfs_lock);
349 static ssize_t gpio_value_store(struct device *dev,
350 struct device_attribute *attr, const char *buf, size_t size)
352 struct gpio_desc *desc = dev_get_drvdata(dev);
355 mutex_lock(&sysfs_lock);
357 if (!test_bit(FLAG_EXPORT, &desc->flags))
359 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
364 status = kstrtol(buf, 0, &value);
366 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
368 gpiod_set_value_cansleep(desc, value != 0);
373 mutex_unlock(&sysfs_lock);
377 static const DEVICE_ATTR(value, 0644,
378 gpio_value_show, gpio_value_store);
380 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
382 struct sysfs_dirent *value_sd = priv;
384 sysfs_notify_dirent(value_sd);
388 static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
389 unsigned long gpio_flags)
391 struct sysfs_dirent *value_sd;
392 unsigned long irq_flags;
395 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
398 irq = gpiod_to_irq(desc);
402 id = desc->flags >> ID_SHIFT;
403 value_sd = idr_find(&dirent_idr, id);
405 free_irq(irq, value_sd);
407 desc->flags &= ~GPIO_TRIGGER_MASK;
414 irq_flags = IRQF_SHARED;
415 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
416 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
417 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
418 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
419 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
420 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
423 value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
429 ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
434 desc->flags &= GPIO_FLAGS_MASK;
435 desc->flags |= (unsigned long)id << ID_SHIFT;
437 if (desc->flags >> ID_SHIFT != id) {
443 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
444 "gpiolib", value_sd);
448 desc->flags |= gpio_flags;
452 idr_remove(&dirent_idr, id);
453 desc->flags &= GPIO_FLAGS_MASK;
461 static const struct {
464 } trigger_types[] = {
466 { "falling", BIT(FLAG_TRIG_FALL) },
467 { "rising", BIT(FLAG_TRIG_RISE) },
468 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
471 static ssize_t gpio_edge_show(struct device *dev,
472 struct device_attribute *attr, char *buf)
474 const struct gpio_desc *desc = dev_get_drvdata(dev);
477 mutex_lock(&sysfs_lock);
479 if (!test_bit(FLAG_EXPORT, &desc->flags))
485 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
486 if ((desc->flags & GPIO_TRIGGER_MASK)
487 == trigger_types[i].flags) {
488 status = sprintf(buf, "%s\n",
489 trigger_types[i].name);
494 mutex_unlock(&sysfs_lock);
498 static ssize_t gpio_edge_store(struct device *dev,
499 struct device_attribute *attr, const char *buf, size_t size)
501 struct gpio_desc *desc = dev_get_drvdata(dev);
505 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
506 if (sysfs_streq(trigger_types[i].name, buf))
511 mutex_lock(&sysfs_lock);
513 if (!test_bit(FLAG_EXPORT, &desc->flags))
516 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
521 mutex_unlock(&sysfs_lock);
526 static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
528 static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
533 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
537 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
539 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
541 /* reconfigure poll(2) support if enabled on one edge only */
542 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
543 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
544 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
546 gpio_setup_irq(desc, dev, 0);
547 status = gpio_setup_irq(desc, dev, trigger_flags);
553 static ssize_t gpio_active_low_show(struct device *dev,
554 struct device_attribute *attr, char *buf)
556 const struct gpio_desc *desc = dev_get_drvdata(dev);
559 mutex_lock(&sysfs_lock);
561 if (!test_bit(FLAG_EXPORT, &desc->flags))
564 status = sprintf(buf, "%d\n",
565 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
567 mutex_unlock(&sysfs_lock);
572 static ssize_t gpio_active_low_store(struct device *dev,
573 struct device_attribute *attr, const char *buf, size_t size)
575 struct gpio_desc *desc = dev_get_drvdata(dev);
578 mutex_lock(&sysfs_lock);
580 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
585 status = kstrtol(buf, 0, &value);
587 status = sysfs_set_active_low(desc, dev, value != 0);
590 mutex_unlock(&sysfs_lock);
592 return status ? : size;
595 static const DEVICE_ATTR(active_low, 0644,
596 gpio_active_low_show, gpio_active_low_store);
598 static const struct attribute *gpio_attrs[] = {
599 &dev_attr_value.attr,
600 &dev_attr_active_low.attr,
604 static const struct attribute_group gpio_attr_group = {
605 .attrs = (struct attribute **) gpio_attrs,
609 * /sys/class/gpio/gpiochipN/
610 * /base ... matching gpio_chip.base (N)
611 * /label ... matching gpio_chip.label
612 * /ngpio ... matching gpio_chip.ngpio
615 static ssize_t chip_base_show(struct device *dev,
616 struct device_attribute *attr, char *buf)
618 const struct gpio_chip *chip = dev_get_drvdata(dev);
620 return sprintf(buf, "%d\n", chip->base);
622 static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
624 static ssize_t chip_label_show(struct device *dev,
625 struct device_attribute *attr, char *buf)
627 const struct gpio_chip *chip = dev_get_drvdata(dev);
629 return sprintf(buf, "%s\n", chip->label ? : "");
631 static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
633 static ssize_t chip_ngpio_show(struct device *dev,
634 struct device_attribute *attr, char *buf)
636 const struct gpio_chip *chip = dev_get_drvdata(dev);
638 return sprintf(buf, "%u\n", chip->ngpio);
640 static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
642 static const struct attribute *gpiochip_attrs[] = {
644 &dev_attr_label.attr,
645 &dev_attr_ngpio.attr,
649 static const struct attribute_group gpiochip_attr_group = {
650 .attrs = (struct attribute **) gpiochip_attrs,
654 * /sys/class/gpio/export ... write-only
655 * integer N ... number of GPIO to export (full access)
656 * /sys/class/gpio/unexport ... write-only
657 * integer N ... number of GPIO to unexport
659 static ssize_t export_store(struct class *class,
660 struct class_attribute *attr,
661 const char *buf, size_t len)
664 struct gpio_desc *desc;
667 status = kstrtol(buf, 0, &gpio);
671 desc = gpio_to_desc(gpio);
672 /* reject invalid GPIOs */
674 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
678 /* No extra locking here; FLAG_SYSFS just signifies that the
679 * request and export were done by on behalf of userspace, so
680 * they may be undone on its behalf too.
683 status = gpiod_request(desc, "sysfs");
685 if (status == -EPROBE_DEFER)
689 status = gpiod_export(desc, true);
693 set_bit(FLAG_SYSFS, &desc->flags);
697 pr_debug("%s: status %d\n", __func__, status);
698 return status ? : len;
701 static ssize_t unexport_store(struct class *class,
702 struct class_attribute *attr,
703 const char *buf, size_t len)
706 struct gpio_desc *desc;
709 status = kstrtol(buf, 0, &gpio);
713 desc = gpio_to_desc(gpio);
714 /* reject bogus commands (gpio_unexport ignores them) */
716 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
722 /* No extra locking here; FLAG_SYSFS just signifies that the
723 * request and export were done by on behalf of userspace, so
724 * they may be undone on its behalf too.
726 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
732 pr_debug("%s: status %d\n", __func__, status);
733 return status ? : len;
736 static struct class_attribute gpio_class_attrs[] = {
737 __ATTR(export, 0200, NULL, export_store),
738 __ATTR(unexport, 0200, NULL, unexport_store),
742 static struct class gpio_class = {
744 .owner = THIS_MODULE,
746 .class_attrs = gpio_class_attrs,
751 * gpio_export - export a GPIO through sysfs
752 * @gpio: gpio to make available, already requested
753 * @direction_may_change: true if userspace may change gpio direction
754 * Context: arch_initcall or later
756 * When drivers want to make a GPIO accessible to userspace after they
757 * have requested it -- perhaps while debugging, or as part of their
758 * public interface -- they may use this routine. If the GPIO can
759 * change direction (some can't) and the caller allows it, userspace
760 * will see "direction" sysfs attribute which may be used to change
761 * the gpio's direction. A "value" attribute will always be provided.
763 * Returns zero on success, else an error.
765 static int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
769 const char *ioname = NULL;
773 /* can't export until sysfs is available ... */
775 pr_debug("%s: called too early!\n", __func__);
780 pr_debug("%s: invalid gpio descriptor\n", __func__);
784 mutex_lock(&sysfs_lock);
786 spin_lock_irqsave(&gpio_lock, flags);
787 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
788 test_bit(FLAG_EXPORT, &desc->flags)) {
789 spin_unlock_irqrestore(&gpio_lock, flags);
790 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
791 __func__, desc_to_gpio(desc),
792 test_bit(FLAG_REQUESTED, &desc->flags),
793 test_bit(FLAG_EXPORT, &desc->flags));
798 if (!desc->chip->direction_input || !desc->chip->direction_output)
799 direction_may_change = false;
800 spin_unlock_irqrestore(&gpio_lock, flags);
802 offset = gpio_chip_hwgpio(desc);
803 if (desc->chip->names && desc->chip->names[offset])
804 ioname = desc->chip->names[offset];
806 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
807 desc, ioname ? ioname : "gpio%u",
810 status = PTR_ERR(dev);
814 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
816 goto fail_unregister_device;
818 if (direction_may_change) {
819 status = device_create_file(dev, &dev_attr_direction);
821 goto fail_unregister_device;
824 if (gpiod_to_irq(desc) >= 0 && (direction_may_change ||
825 !test_bit(FLAG_IS_OUT, &desc->flags))) {
826 status = device_create_file(dev, &dev_attr_edge);
828 goto fail_unregister_device;
831 set_bit(FLAG_EXPORT, &desc->flags);
832 mutex_unlock(&sysfs_lock);
835 fail_unregister_device:
836 device_unregister(dev);
838 mutex_unlock(&sysfs_lock);
839 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
844 int gpio_export(unsigned gpio, bool direction_may_change)
846 return gpiod_export(gpio_to_desc(gpio), direction_may_change);
848 EXPORT_SYMBOL_GPL(gpio_export);
850 static int match_export(struct device *dev, const void *data)
852 return dev_get_drvdata(dev) == data;
856 * gpio_export_link - create a sysfs link to an exported GPIO node
857 * @dev: device under which to create symlink
858 * @name: name of the symlink
859 * @gpio: gpio to create symlink to, already exported
861 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
862 * node. Caller is responsible for unlinking.
864 * Returns zero on success, else an error.
866 static int gpiod_export_link(struct device *dev, const char *name,
867 struct gpio_desc *desc)
869 int status = -EINVAL;
872 pr_warn("%s: invalid GPIO\n", __func__);
876 mutex_lock(&sysfs_lock);
878 if (test_bit(FLAG_EXPORT, &desc->flags)) {
881 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
883 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
890 mutex_unlock(&sysfs_lock);
893 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
899 int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
901 return gpiod_export_link(dev, name, gpio_to_desc(gpio));
903 EXPORT_SYMBOL_GPL(gpio_export_link);
906 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
907 * @gpio: gpio to change
908 * @value: non-zero to use active low, i.e. inverted values
910 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
911 * The GPIO does not have to be exported yet. If poll(2) support has
912 * been enabled for either rising or falling edge, it will be
913 * reconfigured to follow the new polarity.
915 * Returns zero on success, else an error.
917 static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
919 struct device *dev = NULL;
920 int status = -EINVAL;
923 pr_warn("%s: invalid GPIO\n", __func__);
927 mutex_lock(&sysfs_lock);
929 if (test_bit(FLAG_EXPORT, &desc->flags)) {
930 dev = class_find_device(&gpio_class, NULL, desc, match_export);
937 status = sysfs_set_active_low(desc, dev, value);
940 mutex_unlock(&sysfs_lock);
943 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
949 int gpio_sysfs_set_active_low(unsigned gpio, int value)
951 return gpiod_sysfs_set_active_low(gpio_to_desc(gpio), value);
953 EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
956 * gpio_unexport - reverse effect of gpio_export()
957 * @gpio: gpio to make unavailable
959 * This is implicit on gpio_free().
961 static void gpiod_unexport(struct gpio_desc *desc)
964 struct device *dev = NULL;
967 pr_warn("%s: invalid GPIO\n", __func__);
971 mutex_lock(&sysfs_lock);
973 if (test_bit(FLAG_EXPORT, &desc->flags)) {
975 dev = class_find_device(&gpio_class, NULL, desc, match_export);
977 gpio_setup_irq(desc, dev, 0);
978 clear_bit(FLAG_EXPORT, &desc->flags);
983 mutex_unlock(&sysfs_lock);
986 device_unregister(dev);
991 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
995 void gpio_unexport(unsigned gpio)
997 gpiod_unexport(gpio_to_desc(gpio));
999 EXPORT_SYMBOL_GPL(gpio_unexport);
1001 static int gpiochip_export(struct gpio_chip *chip)
1006 /* Many systems register gpio chips for SOC support very early,
1007 * before driver model support is available. In those cases we
1008 * export this later, in gpiolib_sysfs_init() ... here we just
1009 * verify that _some_ field of gpio_class got initialized.
1014 /* use chip->base for the ID; it's already known to be unique */
1015 mutex_lock(&sysfs_lock);
1016 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
1017 "gpiochip%d", chip->base);
1019 status = sysfs_create_group(&dev->kobj,
1020 &gpiochip_attr_group);
1022 status = PTR_ERR(dev);
1023 chip->exported = (status == 0);
1024 mutex_unlock(&sysfs_lock);
1027 unsigned long flags;
1030 spin_lock_irqsave(&gpio_lock, flags);
1032 while (gpio < chip->ngpio)
1033 chip->desc[gpio++].chip = NULL;
1034 spin_unlock_irqrestore(&gpio_lock, flags);
1036 pr_debug("%s: chip %s status %d\n", __func__,
1037 chip->label, status);
1043 static void gpiochip_unexport(struct gpio_chip *chip)
1048 mutex_lock(&sysfs_lock);
1049 dev = class_find_device(&gpio_class, NULL, chip, match_export);
1052 device_unregister(dev);
1057 mutex_unlock(&sysfs_lock);
1060 pr_debug("%s: chip %s status %d\n", __func__,
1061 chip->label, status);
1064 static int __init gpiolib_sysfs_init(void)
1067 unsigned long flags;
1068 struct gpio_chip *chip;
1070 status = class_register(&gpio_class);
1074 /* Scan and register the gpio_chips which registered very
1075 * early (e.g. before the class_register above was called).
1077 * We run before arch_initcall() so chip->dev nodes can have
1078 * registered, and so arch_initcall() can always gpio_export().
1080 spin_lock_irqsave(&gpio_lock, flags);
1081 list_for_each_entry(chip, &gpio_chips, list) {
1082 if (!chip || chip->exported)
1085 spin_unlock_irqrestore(&gpio_lock, flags);
1086 status = gpiochip_export(chip);
1087 spin_lock_irqsave(&gpio_lock, flags);
1089 spin_unlock_irqrestore(&gpio_lock, flags);
1094 postcore_initcall(gpiolib_sysfs_init);
1097 static inline int gpiochip_export(struct gpio_chip *chip)
1102 static inline void gpiochip_unexport(struct gpio_chip *chip)
1106 static inline int gpiod_export(struct gpio_desc *desc,
1107 bool direction_may_change)
1112 static inline int gpiod_export_link(struct device *dev, const char *name,
1113 struct gpio_desc *desc)
1118 static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
1123 static inline void gpiod_unexport(struct gpio_desc *desc)
1127 #endif /* CONFIG_GPIO_SYSFS */
1130 * Add a new chip to the global chips list, keeping the list of chips sorted
1133 * Return -EBUSY if the new chip overlaps with some other chip's integer
1136 static int gpiochip_add_to_list(struct gpio_chip *chip)
1138 struct list_head *pos = &gpio_chips;
1139 struct gpio_chip *_chip;
1142 /* find where to insert our chip */
1143 list_for_each(pos, &gpio_chips) {
1144 _chip = list_entry(pos, struct gpio_chip, list);
1145 /* shall we insert before _chip? */
1146 if (_chip->base >= chip->base + chip->ngpio)
1150 /* are we stepping on the chip right before? */
1151 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
1152 _chip = list_entry(pos->prev, struct gpio_chip, list);
1153 if (_chip->base + _chip->ngpio > chip->base) {
1155 "GPIO integer space overlap, cannot add chip\n");
1161 list_add_tail(&chip->list, pos);
1167 * gpiochip_add() - register a gpio_chip
1168 * @chip: the chip to register, with chip->base initialized
1169 * Context: potentially before irqs or kmalloc will work
1171 * Returns a negative errno if the chip can't be registered, such as
1172 * because the chip->base is invalid or already associated with a
1173 * different chip. Otherwise it returns zero as a success code.
1175 * When gpiochip_add() is called very early during boot, so that GPIOs
1176 * can be freely used, the chip->dev device must be registered before
1177 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1178 * for GPIOs will fail rudely.
1180 * If chip->base is negative, this requests dynamic assignment of
1181 * a range of valid GPIOs.
1183 int gpiochip_add(struct gpio_chip *chip)
1185 unsigned long flags;
1188 int base = chip->base;
1190 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
1196 spin_lock_irqsave(&gpio_lock, flags);
1199 base = gpiochip_find_base(chip->ngpio);
1207 status = gpiochip_add_to_list(chip);
1210 chip->desc = &gpio_desc[chip->base];
1212 for (id = 0; id < chip->ngpio; id++) {
1213 struct gpio_desc *desc = &chip->desc[id];
1216 /* REVISIT: most hardware initializes GPIOs as
1217 * inputs (often with pullups enabled) so power
1218 * usage is minimized. Linux code should set the
1219 * gpio direction first thing; but until it does,
1220 * and in case chip->get_direction is not set,
1221 * we may expose the wrong direction in sysfs.
1223 desc->flags = !chip->direction_input
1224 ? (1 << FLAG_IS_OUT)
1229 spin_unlock_irqrestore(&gpio_lock, flags);
1231 #ifdef CONFIG_PINCTRL
1232 INIT_LIST_HEAD(&chip->pin_ranges);
1235 of_gpiochip_add(chip);
1240 status = gpiochip_export(chip);
1244 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
1245 chip->base, chip->base + chip->ngpio - 1,
1246 chip->label ? : "generic");
1251 spin_unlock_irqrestore(&gpio_lock, flags);
1253 /* failures here can mean systems won't boot... */
1254 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1255 chip->base, chip->base + chip->ngpio - 1,
1256 chip->label ? : "generic");
1259 EXPORT_SYMBOL_GPL(gpiochip_add);
1262 * gpiochip_remove() - unregister a gpio_chip
1263 * @chip: the chip to unregister
1265 * A gpio_chip with any GPIOs still requested may not be removed.
1267 int gpiochip_remove(struct gpio_chip *chip)
1269 unsigned long flags;
1273 spin_lock_irqsave(&gpio_lock, flags);
1275 gpiochip_remove_pin_ranges(chip);
1276 of_gpiochip_remove(chip);
1278 for (id = 0; id < chip->ngpio; id++) {
1279 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
1285 for (id = 0; id < chip->ngpio; id++)
1286 chip->desc[id].chip = NULL;
1288 list_del(&chip->list);
1291 spin_unlock_irqrestore(&gpio_lock, flags);
1294 gpiochip_unexport(chip);
1298 EXPORT_SYMBOL_GPL(gpiochip_remove);
1301 * gpiochip_find() - iterator for locating a specific gpio_chip
1302 * @data: data to pass to match function
1303 * @callback: Callback function to check gpio_chip
1305 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1306 * determined by a user supplied @match callback. The callback should return
1307 * 0 if the device doesn't match and non-zero if it does. If the callback is
1308 * non-zero, this function will return to the caller and not iterate over any
1311 struct gpio_chip *gpiochip_find(void *data,
1312 int (*match)(struct gpio_chip *chip,
1315 struct gpio_chip *chip;
1316 unsigned long flags;
1318 spin_lock_irqsave(&gpio_lock, flags);
1319 list_for_each_entry(chip, &gpio_chips, list)
1320 if (match(chip, data))
1324 if (&chip->list == &gpio_chips)
1326 spin_unlock_irqrestore(&gpio_lock, flags);
1330 EXPORT_SYMBOL_GPL(gpiochip_find);
1332 #ifdef CONFIG_PINCTRL
1335 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1336 * @chip: the gpiochip to add the range for
1337 * @pinctrl_name: the dev_name() of the pin controller to map to
1338 * @gpio_offset: the start offset in the current gpio_chip number space
1339 * @pin_offset: the start offset in the pin controller number space
1340 * @npins: the number of pins from the offset of each pin space (GPIO and
1341 * pin controller) to accumulate in this range
1343 int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
1344 unsigned int gpio_offset, unsigned int pin_offset,
1347 struct gpio_pin_range *pin_range;
1350 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1352 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1357 /* Use local offset as range ID */
1358 pin_range->range.id = gpio_offset;
1359 pin_range->range.gc = chip;
1360 pin_range->range.name = chip->label;
1361 pin_range->range.base = chip->base + gpio_offset;
1362 pin_range->range.pin_base = pin_offset;
1363 pin_range->range.npins = npins;
1364 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
1366 if (IS_ERR(pin_range->pctldev)) {
1367 ret = PTR_ERR(pin_range->pctldev);
1368 pr_err("%s: GPIO chip: could not create pin range\n",
1373 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1374 chip->label, gpio_offset, gpio_offset + npins - 1,
1376 pin_offset, pin_offset + npins - 1);
1378 list_add_tail(&pin_range->node, &chip->pin_ranges);
1382 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
1385 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1386 * @chip: the chip to remove all the mappings for
1388 void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1390 struct gpio_pin_range *pin_range, *tmp;
1392 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1393 list_del(&pin_range->node);
1394 pinctrl_remove_gpio_range(pin_range->pctldev,
1399 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1401 #endif /* CONFIG_PINCTRL */
1403 /* These "optional" allocation calls help prevent drivers from stomping
1404 * on each other, and help provide better diagnostics in debugfs.
1405 * They're called even less than the "set direction" calls.
1407 static int gpiod_request(struct gpio_desc *desc, const char *label)
1409 struct gpio_chip *chip;
1410 int status = -EPROBE_DEFER;
1411 unsigned long flags;
1413 if (!desc || !desc->chip) {
1414 pr_warn("%s: invalid GPIO\n", __func__);
1418 spin_lock_irqsave(&gpio_lock, flags);
1422 if (!try_module_get(chip->owner))
1425 /* NOTE: gpio_request() can be called in early boot,
1426 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1429 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1430 desc_set_label(desc, label ? : "?");
1434 module_put(chip->owner);
1438 if (chip->request) {
1439 /* chip->request may sleep */
1440 spin_unlock_irqrestore(&gpio_lock, flags);
1441 status = chip->request(chip, gpio_chip_hwgpio(desc));
1442 spin_lock_irqsave(&gpio_lock, flags);
1445 desc_set_label(desc, NULL);
1446 module_put(chip->owner);
1447 clear_bit(FLAG_REQUESTED, &desc->flags);
1451 if (chip->get_direction) {
1452 /* chip->get_direction may sleep */
1453 spin_unlock_irqrestore(&gpio_lock, flags);
1454 gpiod_get_direction(desc);
1455 spin_lock_irqsave(&gpio_lock, flags);
1459 pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
1460 desc_to_gpio(desc), label ? : "?", status);
1461 spin_unlock_irqrestore(&gpio_lock, flags);
1465 int gpio_request(unsigned gpio, const char *label)
1467 return gpiod_request(gpio_to_desc(gpio), label);
1469 EXPORT_SYMBOL_GPL(gpio_request);
1471 static void gpiod_free(struct gpio_desc *desc)
1473 unsigned long flags;
1474 struct gpio_chip *chip;
1479 WARN_ON(extra_checks);
1483 gpiod_unexport(desc);
1485 spin_lock_irqsave(&gpio_lock, flags);
1488 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1490 spin_unlock_irqrestore(&gpio_lock, flags);
1491 might_sleep_if(chip->can_sleep);
1492 chip->free(chip, gpio_chip_hwgpio(desc));
1493 spin_lock_irqsave(&gpio_lock, flags);
1495 desc_set_label(desc, NULL);
1496 module_put(desc->chip->owner);
1497 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1498 clear_bit(FLAG_REQUESTED, &desc->flags);
1499 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1500 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1502 WARN_ON(extra_checks);
1504 spin_unlock_irqrestore(&gpio_lock, flags);
1507 void gpio_free(unsigned gpio)
1509 gpiod_free(gpio_to_desc(gpio));
1511 EXPORT_SYMBOL_GPL(gpio_free);
1514 * gpio_request_one - request a single GPIO with initial configuration
1515 * @gpio: the GPIO number
1516 * @flags: GPIO configuration as specified by GPIOF_*
1517 * @label: a literal description string of this GPIO
1519 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1521 struct gpio_desc *desc;
1524 desc = gpio_to_desc(gpio);
1526 err = gpiod_request(desc, label);
1530 if (flags & GPIOF_OPEN_DRAIN)
1531 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
1533 if (flags & GPIOF_OPEN_SOURCE)
1534 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
1536 if (flags & GPIOF_DIR_IN)
1537 err = gpiod_direction_input(desc);
1539 err = gpiod_direction_output(desc,
1540 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1545 if (flags & GPIOF_EXPORT) {
1546 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
1557 EXPORT_SYMBOL_GPL(gpio_request_one);
1560 * gpio_request_array - request multiple GPIOs in a single call
1561 * @array: array of the 'struct gpio'
1562 * @num: how many GPIOs in the array
1564 int gpio_request_array(const struct gpio *array, size_t num)
1568 for (i = 0; i < num; i++, array++) {
1569 err = gpio_request_one(array->gpio, array->flags, array->label);
1577 gpio_free((--array)->gpio);
1580 EXPORT_SYMBOL_GPL(gpio_request_array);
1583 * gpio_free_array - release multiple GPIOs in a single call
1584 * @array: array of the 'struct gpio'
1585 * @num: how many GPIOs in the array
1587 void gpio_free_array(const struct gpio *array, size_t num)
1590 gpio_free((array++)->gpio);
1592 EXPORT_SYMBOL_GPL(gpio_free_array);
1595 * gpiochip_is_requested - return string iff signal was requested
1596 * @chip: controller managing the signal
1597 * @offset: of signal within controller's 0..(ngpio - 1) range
1599 * Returns NULL if the GPIO is not currently requested, else a string.
1600 * If debugfs support is enabled, the string returned is the label passed
1601 * to gpio_request(); otherwise it is a meaningless constant.
1603 * This function is for use by GPIO controller drivers. The label can
1604 * help with diagnostics, and knowing that the signal is used as a GPIO
1605 * can help avoid accidentally multiplexing it to another controller.
1607 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1609 struct gpio_desc *desc;
1611 if (!GPIO_OFFSET_VALID(chip, offset))
1614 desc = &chip->desc[offset];
1616 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
1618 #ifdef CONFIG_DEBUG_FS
1624 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1627 /* Drivers MUST set GPIO direction before making get/set calls. In
1628 * some cases this is done in early boot, before IRQs are enabled.
1630 * As a rule these aren't called more than once (except for drivers
1631 * using the open-drain emulation idiom) so these are natural places
1632 * to accumulate extra debugging checks. Note that we can't (yet)
1633 * rely on gpio_request() having been called beforehand.
1636 static int gpiod_direction_input(struct gpio_desc *desc)
1638 unsigned long flags;
1639 struct gpio_chip *chip;
1640 int status = -EINVAL;
1643 if (!desc || !desc->chip) {
1644 pr_warn("%s: invalid GPIO\n", __func__);
1649 if (!chip->get || !chip->direction_input) {
1651 "%s: missing get() or direction_input() operations\n",
1656 spin_lock_irqsave(&gpio_lock, flags);
1658 status = gpio_ensure_requested(desc);
1662 /* now we know the gpio is valid and chip won't vanish */
1664 spin_unlock_irqrestore(&gpio_lock, flags);
1666 might_sleep_if(chip->can_sleep);
1668 offset = gpio_chip_hwgpio(desc);
1670 status = chip->request(chip, offset);
1672 gpiod_dbg(desc, "chip request fail, %d\n", status);
1673 /* and it's not available to anyone else ...
1674 * gpio_request() is the fully clean solution.
1680 status = chip->direction_input(chip, offset);
1682 clear_bit(FLAG_IS_OUT, &desc->flags);
1684 trace_gpio_direction(desc_to_gpio(desc), 1, status);
1688 spin_unlock_irqrestore(&gpio_lock, flags);
1690 gpiod_dbg(desc, "%s status %d\n", __func__, status);
1694 int gpio_direction_input(unsigned gpio)
1696 return gpiod_direction_input(gpio_to_desc(gpio));
1698 EXPORT_SYMBOL_GPL(gpio_direction_input);
1700 static int gpiod_direction_output(struct gpio_desc *desc, int value)
1702 unsigned long flags;
1703 struct gpio_chip *chip;
1704 int status = -EINVAL;
1707 if (!desc || !desc->chip) {
1708 pr_warn("%s: invalid GPIO\n", __func__);
1712 /* Open drain pin should not be driven to 1 */
1713 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1714 return gpiod_direction_input(desc);
1716 /* Open source pin should not be driven to 0 */
1717 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1718 return gpiod_direction_input(desc);
1721 if (!chip->set || !chip->direction_output) {
1723 "%s: missing set() or direction_output() operations\n",
1728 spin_lock_irqsave(&gpio_lock, flags);
1730 status = gpio_ensure_requested(desc);
1734 /* now we know the gpio is valid and chip won't vanish */
1736 spin_unlock_irqrestore(&gpio_lock, flags);
1738 might_sleep_if(chip->can_sleep);
1740 offset = gpio_chip_hwgpio(desc);
1742 status = chip->request(chip, offset);
1744 gpiod_dbg(desc, "chip request fail, %d\n", status);
1745 /* and it's not available to anyone else ...
1746 * gpio_request() is the fully clean solution.
1752 status = chip->direction_output(chip, offset, value);
1754 set_bit(FLAG_IS_OUT, &desc->flags);
1755 trace_gpio_value(desc_to_gpio(desc), 0, value);
1756 trace_gpio_direction(desc_to_gpio(desc), 0, status);
1760 spin_unlock_irqrestore(&gpio_lock, flags);
1762 gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status);
1766 int gpio_direction_output(unsigned gpio, int value)
1768 return gpiod_direction_output(gpio_to_desc(gpio), value);
1770 EXPORT_SYMBOL_GPL(gpio_direction_output);
1773 * gpio_set_debounce - sets @debounce time for a @gpio
1774 * @gpio: the gpio to set debounce time
1775 * @debounce: debounce time is microseconds
1777 * returns -ENOTSUPP if the controller does not support setting
1780 static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
1782 unsigned long flags;
1783 struct gpio_chip *chip;
1784 int status = -EINVAL;
1787 if (!desc || !desc->chip) {
1788 pr_warn("%s: invalid GPIO\n", __func__);
1793 if (!chip->set || !chip->set_debounce) {
1795 "%s: missing set() or set_debounce() operations\n",
1800 spin_lock_irqsave(&gpio_lock, flags);
1802 status = gpio_ensure_requested(desc);
1806 /* now we know the gpio is valid and chip won't vanish */
1808 spin_unlock_irqrestore(&gpio_lock, flags);
1810 might_sleep_if(chip->can_sleep);
1812 offset = gpio_chip_hwgpio(desc);
1813 return chip->set_debounce(chip, offset, debounce);
1816 spin_unlock_irqrestore(&gpio_lock, flags);
1818 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
1823 int gpio_set_debounce(unsigned gpio, unsigned debounce)
1825 return gpiod_set_debounce(gpio_to_desc(gpio), debounce);
1827 EXPORT_SYMBOL_GPL(gpio_set_debounce);
1829 /* I/O calls are only valid after configuration completed; the relevant
1830 * "is this a valid GPIO" error checks should already have been done.
1832 * "Get" operations are often inlinable as reading a pin value register,
1833 * and masking the relevant bit in that register.
1835 * When "set" operations are inlinable, they involve writing that mask to
1836 * one register to set a low value, or a different register to set it high.
1837 * Otherwise locking is needed, so there may be little value to inlining.
1839 *------------------------------------------------------------------------
1841 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1842 * have requested the GPIO. That can include implicit requesting by
1843 * a direction setting call. Marking a gpio as requested locks its chip
1844 * in memory, guaranteeing that these table lookups need no more locking
1845 * and that gpiochip_remove() will fail.
1847 * REVISIT when debugging, consider adding some instrumentation to ensure
1848 * that the GPIO was actually requested.
1852 * __gpio_get_value() - return a gpio's value
1853 * @gpio: gpio whose value will be returned
1856 * This is used directly or indirectly to implement gpio_get_value().
1857 * It returns the zero or nonzero value provided by the associated
1858 * gpio_chip.get() method; or zero if no such method is provided.
1860 static int gpiod_get_value(const struct gpio_desc *desc)
1862 struct gpio_chip *chip;
1869 offset = gpio_chip_hwgpio(desc);
1870 /* Should be using gpio_get_value_cansleep() */
1871 WARN_ON(chip->can_sleep);
1872 value = chip->get ? chip->get(chip, offset) : 0;
1873 trace_gpio_value(desc_to_gpio(desc), 1, value);
1877 int __gpio_get_value(unsigned gpio)
1879 return gpiod_get_value(gpio_to_desc(gpio));
1881 EXPORT_SYMBOL_GPL(__gpio_get_value);
1884 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1885 * @gpio: Gpio whose state need to be set.
1887 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1889 static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
1892 struct gpio_chip *chip = desc->chip;
1893 int offset = gpio_chip_hwgpio(desc);
1896 err = chip->direction_input(chip, offset);
1898 clear_bit(FLAG_IS_OUT, &desc->flags);
1900 err = chip->direction_output(chip, offset, 0);
1902 set_bit(FLAG_IS_OUT, &desc->flags);
1904 trace_gpio_direction(desc_to_gpio(desc), value, err);
1907 "%s: Error in set_value for open drain err %d\n",
1912 * _gpio_set_open_source() - Set the open source gpio's value.
1913 * @gpio: Gpio whose state need to be set.
1915 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1917 static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
1920 struct gpio_chip *chip = desc->chip;
1921 int offset = gpio_chip_hwgpio(desc);
1924 err = chip->direction_output(chip, offset, 1);
1926 set_bit(FLAG_IS_OUT, &desc->flags);
1928 err = chip->direction_input(chip, offset);
1930 clear_bit(FLAG_IS_OUT, &desc->flags);
1932 trace_gpio_direction(desc_to_gpio(desc), !value, err);
1935 "%s: Error in set_value for open source err %d\n",
1940 * __gpio_set_value() - assign a gpio's value
1941 * @gpio: gpio whose value will be assigned
1942 * @value: value to assign
1945 * This is used directly or indirectly to implement gpio_set_value().
1946 * It invokes the associated gpio_chip.set() method.
1948 static void gpiod_set_value(struct gpio_desc *desc, int value)
1950 struct gpio_chip *chip;
1955 /* Should be using gpio_set_value_cansleep() */
1956 WARN_ON(chip->can_sleep);
1957 trace_gpio_value(desc_to_gpio(desc), 0, value);
1958 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1959 _gpio_set_open_drain_value(desc, value);
1960 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1961 _gpio_set_open_source_value(desc, value);
1963 chip->set(chip, gpio_chip_hwgpio(desc), value);
1966 void __gpio_set_value(unsigned gpio, int value)
1968 return gpiod_set_value(gpio_to_desc(gpio), value);
1970 EXPORT_SYMBOL_GPL(__gpio_set_value);
1973 * __gpio_cansleep() - report whether gpio value access will sleep
1974 * @gpio: gpio in question
1977 * This is used directly or indirectly to implement gpio_cansleep(). It
1978 * returns nonzero if access reading or writing the GPIO value can sleep.
1980 static int gpiod_cansleep(const struct gpio_desc *desc)
1984 /* only call this on GPIOs that are valid! */
1985 return desc->chip->can_sleep;
1988 int __gpio_cansleep(unsigned gpio)
1990 return gpiod_cansleep(gpio_to_desc(gpio));
1992 EXPORT_SYMBOL_GPL(__gpio_cansleep);
1995 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1996 * @gpio: gpio whose IRQ will be returned (already requested)
1999 * This is used directly or indirectly to implement gpio_to_irq().
2000 * It returns the number of the IRQ signaled by this (input) GPIO,
2001 * or a negative errno.
2003 static int gpiod_to_irq(const struct gpio_desc *desc)
2005 struct gpio_chip *chip;
2011 offset = gpio_chip_hwgpio(desc);
2012 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
2015 int __gpio_to_irq(unsigned gpio)
2017 return gpiod_to_irq(gpio_to_desc(gpio));
2019 EXPORT_SYMBOL_GPL(__gpio_to_irq);
2022 /* There's no value in making it easy to inline GPIO calls that may sleep.
2023 * Common examples include ones connected to I2C or SPI chips.
2026 static int gpiod_get_value_cansleep(const struct gpio_desc *desc)
2028 struct gpio_chip *chip;
2032 might_sleep_if(extra_checks);
2036 offset = gpio_chip_hwgpio(desc);
2037 value = chip->get ? chip->get(chip, offset) : 0;
2038 trace_gpio_value(desc_to_gpio(desc), 1, value);
2042 int gpio_get_value_cansleep(unsigned gpio)
2044 return gpiod_get_value_cansleep(gpio_to_desc(gpio));
2046 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
2048 static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
2050 struct gpio_chip *chip;
2052 might_sleep_if(extra_checks);
2056 trace_gpio_value(desc_to_gpio(desc), 0, value);
2057 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2058 _gpio_set_open_drain_value(desc, value);
2059 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2060 _gpio_set_open_source_value(desc, value);
2062 chip->set(chip, gpio_chip_hwgpio(desc), value);
2065 void gpio_set_value_cansleep(unsigned gpio, int value)
2067 return gpiod_set_value_cansleep(gpio_to_desc(gpio), value);
2069 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
2071 #ifdef CONFIG_DEBUG_FS
2073 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2076 unsigned gpio = chip->base;
2077 struct gpio_desc *gdesc = &chip->desc[0];
2080 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2081 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2084 gpiod_get_direction(gdesc);
2085 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
2086 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
2088 is_out ? "out" : "in ",
2090 ? (chip->get(chip, i) ? "hi" : "lo")
2092 seq_printf(s, "\n");
2096 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
2098 unsigned long flags;
2099 struct gpio_chip *chip = NULL;
2100 loff_t index = *pos;
2104 spin_lock_irqsave(&gpio_lock, flags);
2105 list_for_each_entry(chip, &gpio_chips, list)
2107 spin_unlock_irqrestore(&gpio_lock, flags);
2110 spin_unlock_irqrestore(&gpio_lock, flags);
2115 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2117 unsigned long flags;
2118 struct gpio_chip *chip = v;
2121 spin_lock_irqsave(&gpio_lock, flags);
2122 if (list_is_last(&chip->list, &gpio_chips))
2125 ret = list_entry(chip->list.next, struct gpio_chip, list);
2126 spin_unlock_irqrestore(&gpio_lock, flags);
2134 static void gpiolib_seq_stop(struct seq_file *s, void *v)
2138 static int gpiolib_seq_show(struct seq_file *s, void *v)
2140 struct gpio_chip *chip = v;
2143 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2144 chip->base, chip->base + chip->ngpio - 1);
2147 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2150 seq_printf(s, ", %s", chip->label);
2151 if (chip->can_sleep)
2152 seq_printf(s, ", can sleep");
2153 seq_printf(s, ":\n");
2156 chip->dbg_show(s, chip);
2158 gpiolib_dbg_show(s, chip);
2163 static const struct seq_operations gpiolib_seq_ops = {
2164 .start = gpiolib_seq_start,
2165 .next = gpiolib_seq_next,
2166 .stop = gpiolib_seq_stop,
2167 .show = gpiolib_seq_show,
2170 static int gpiolib_open(struct inode *inode, struct file *file)
2172 return seq_open(file, &gpiolib_seq_ops);
2175 static const struct file_operations gpiolib_operations = {
2176 .owner = THIS_MODULE,
2177 .open = gpiolib_open,
2179 .llseek = seq_lseek,
2180 .release = seq_release,
2183 static int __init gpiolib_debugfs_init(void)
2185 /* /sys/kernel/debug/gpio */
2186 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2187 NULL, NULL, &gpiolib_operations);
2190 subsys_initcall(gpiolib_debugfs_init);
2192 #endif /* DEBUG_FS */