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 static LIST_HEAD(gpio_chips);
77 #ifdef CONFIG_GPIO_SYSFS
78 static DEFINE_IDR(dirent_idr);
82 * Internal gpiod_* API using descriptors instead of the integer namespace.
83 * Most of this should eventually go public.
85 static int gpiod_request(struct gpio_desc *desc, const char *label);
86 static void gpiod_free(struct gpio_desc *desc);
87 static int gpiod_direction_input(struct gpio_desc *desc);
88 static int gpiod_direction_output(struct gpio_desc *desc, int value);
89 static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
90 static int gpiod_get_value_cansleep(struct gpio_desc *desc);
91 static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
92 static int gpiod_get_value(struct gpio_desc *desc);
93 static void gpiod_set_value(struct gpio_desc *desc, int value);
94 static int gpiod_cansleep(struct gpio_desc *desc);
95 static int gpiod_to_irq(struct gpio_desc *desc);
96 static int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
97 static int gpiod_export_link(struct device *dev, const char *name,
98 struct gpio_desc *desc);
99 static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
100 static void gpiod_unexport(struct gpio_desc *desc);
103 static inline void desc_set_label(struct gpio_desc *d, const char *label)
105 #ifdef CONFIG_DEBUG_FS
111 * Return the GPIO number of the passed descriptor relative to its chip
113 static int gpio_chip_hwgpio(const struct gpio_desc *desc)
115 return (desc - &gpio_desc[0]) - desc->chip->base;
119 * Convert a GPIO number to its descriptor
121 static struct gpio_desc *gpio_to_desc(unsigned gpio)
123 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
126 return &gpio_desc[gpio];
130 * Convert a GPIO descriptor to the integer namespace.
131 * This should disappear in the future but is needed since we still
132 * use GPIO numbers for error messages and sysfs nodes
134 static int desc_to_gpio(const struct gpio_desc *desc)
136 return desc - &gpio_desc[0];
140 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
141 * when setting direction, and otherwise illegal. Until board setup code
142 * and drivers use explicit requests everywhere (which won't happen when
143 * those calls have no teeth) we can't avoid autorequesting. This nag
144 * message should motivate switching to explicit requests... so should
145 * the weaker cleanup after faults, compared to gpio_request().
147 * NOTE: the autorequest mechanism is going away; at this point it's
148 * only "legal" in the sense that (old) code using it won't break yet,
149 * but instead only triggers a WARN() stack dump.
151 static int gpio_ensure_requested(struct gpio_desc *desc)
153 const struct gpio_chip *chip = desc->chip;
154 const int gpio = desc_to_gpio(desc);
156 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
157 "autorequest GPIO-%d\n", gpio)) {
158 if (!try_module_get(chip->owner)) {
159 pr_err("GPIO-%d: module can't be gotten \n", gpio);
160 clear_bit(FLAG_REQUESTED, &desc->flags);
164 desc_set_label(desc, "[auto]");
165 /* caller must chip->request() w/o spinlock */
172 /* caller holds gpio_lock *OR* gpio is marked as requested */
173 static struct gpio_chip *gpiod_to_chip(struct gpio_desc *desc)
178 struct gpio_chip *gpio_to_chip(unsigned gpio)
180 return gpiod_to_chip(gpio_to_desc(gpio));
183 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
184 static int gpiochip_find_base(int ngpio)
186 struct gpio_chip *chip;
187 int base = ARCH_NR_GPIOS - ngpio;
189 list_for_each_entry_reverse(chip, &gpio_chips, list) {
190 /* found a free space? */
191 if (chip->base + chip->ngpio <= base)
194 /* nope, check the space right before the chip */
195 base = chip->base - ngpio;
198 if (gpio_is_valid(base)) {
199 pr_debug("%s: found new base at %d\n", __func__, base);
202 pr_err("%s: cannot find free range\n", __func__);
207 /* caller ensures gpio is valid and requested, chip->get_direction may sleep */
208 static int gpiod_get_direction(struct gpio_desc *desc)
210 struct gpio_chip *chip;
212 int status = -EINVAL;
214 chip = gpiod_to_chip(desc);
215 offset = gpio_chip_hwgpio(desc);
217 if (!chip->get_direction)
220 status = chip->get_direction(chip, offset);
222 /* GPIOF_DIR_IN, or other positive */
224 clear_bit(FLAG_IS_OUT, &desc->flags);
228 set_bit(FLAG_IS_OUT, &desc->flags);
233 #ifdef CONFIG_GPIO_SYSFS
235 /* lock protects against unexport_gpio() being called while
236 * sysfs files are active.
238 static DEFINE_MUTEX(sysfs_lock);
241 * /sys/class/gpio/gpioN... only for GPIOs that are exported
243 * * MAY BE OMITTED if kernel won't allow direction changes
244 * * is read/write as "in" or "out"
245 * * may also be written as "high" or "low", initializing
246 * output value as specified ("out" implies "low")
248 * * always readable, subject to hardware behavior
249 * * may be writable, as zero/nonzero
251 * * configures behavior of poll(2) on /value
252 * * available only if pin can generate IRQs on input
253 * * is read/write as "none", "falling", "rising", or "both"
255 * * configures polarity of /value
256 * * is read/write as zero/nonzero
257 * * also affects existing and subsequent "falling" and "rising"
258 * /edge configuration
261 static ssize_t gpio_direction_show(struct device *dev,
262 struct device_attribute *attr, char *buf)
264 struct gpio_desc *desc = dev_get_drvdata(dev);
267 mutex_lock(&sysfs_lock);
269 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
272 gpiod_get_direction(desc);
273 status = sprintf(buf, "%s\n",
274 test_bit(FLAG_IS_OUT, &desc->flags)
278 mutex_unlock(&sysfs_lock);
282 static ssize_t gpio_direction_store(struct device *dev,
283 struct device_attribute *attr, const char *buf, size_t size)
285 struct gpio_desc *desc = dev_get_drvdata(dev);
288 mutex_lock(&sysfs_lock);
290 if (!test_bit(FLAG_EXPORT, &desc->flags))
292 else if (sysfs_streq(buf, "high"))
293 status = gpiod_direction_output(desc, 1);
294 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
295 status = gpiod_direction_output(desc, 0);
296 else if (sysfs_streq(buf, "in"))
297 status = gpiod_direction_input(desc);
301 mutex_unlock(&sysfs_lock);
302 return status ? : size;
305 static /* const */ DEVICE_ATTR(direction, 0644,
306 gpio_direction_show, gpio_direction_store);
308 static ssize_t gpio_value_show(struct device *dev,
309 struct device_attribute *attr, char *buf)
311 struct gpio_desc *desc = dev_get_drvdata(dev);
314 mutex_lock(&sysfs_lock);
316 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
321 value = !!gpiod_get_value_cansleep(desc);
322 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
325 status = sprintf(buf, "%d\n", value);
328 mutex_unlock(&sysfs_lock);
332 static ssize_t gpio_value_store(struct device *dev,
333 struct device_attribute *attr, const char *buf, size_t size)
335 struct gpio_desc *desc = dev_get_drvdata(dev);
338 mutex_lock(&sysfs_lock);
340 if (!test_bit(FLAG_EXPORT, &desc->flags))
342 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
347 status = strict_strtol(buf, 0, &value);
349 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
351 gpiod_set_value_cansleep(desc, value != 0);
356 mutex_unlock(&sysfs_lock);
360 static const DEVICE_ATTR(value, 0644,
361 gpio_value_show, gpio_value_store);
363 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
365 struct sysfs_dirent *value_sd = priv;
367 sysfs_notify_dirent(value_sd);
371 static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
372 unsigned long gpio_flags)
374 struct sysfs_dirent *value_sd;
375 unsigned long irq_flags;
378 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
381 irq = gpiod_to_irq(desc);
385 id = desc->flags >> ID_SHIFT;
386 value_sd = idr_find(&dirent_idr, id);
388 free_irq(irq, value_sd);
390 desc->flags &= ~GPIO_TRIGGER_MASK;
397 irq_flags = IRQF_SHARED;
398 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
399 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
400 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
401 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
402 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
403 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
406 value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
414 if (idr_pre_get(&dirent_idr, GFP_KERNEL))
415 ret = idr_get_new_above(&dirent_idr, value_sd,
417 } while (ret == -EAGAIN);
422 desc->flags &= GPIO_FLAGS_MASK;
423 desc->flags |= (unsigned long)id << ID_SHIFT;
425 if (desc->flags >> ID_SHIFT != id) {
431 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
432 "gpiolib", value_sd);
436 desc->flags |= gpio_flags;
440 idr_remove(&dirent_idr, id);
441 desc->flags &= GPIO_FLAGS_MASK;
449 static const struct {
452 } trigger_types[] = {
454 { "falling", BIT(FLAG_TRIG_FALL) },
455 { "rising", BIT(FLAG_TRIG_RISE) },
456 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
459 static ssize_t gpio_edge_show(struct device *dev,
460 struct device_attribute *attr, char *buf)
462 const struct gpio_desc *desc = dev_get_drvdata(dev);
465 mutex_lock(&sysfs_lock);
467 if (!test_bit(FLAG_EXPORT, &desc->flags))
473 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
474 if ((desc->flags & GPIO_TRIGGER_MASK)
475 == trigger_types[i].flags) {
476 status = sprintf(buf, "%s\n",
477 trigger_types[i].name);
482 mutex_unlock(&sysfs_lock);
486 static ssize_t gpio_edge_store(struct device *dev,
487 struct device_attribute *attr, const char *buf, size_t size)
489 struct gpio_desc *desc = dev_get_drvdata(dev);
493 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
494 if (sysfs_streq(trigger_types[i].name, buf))
499 mutex_lock(&sysfs_lock);
501 if (!test_bit(FLAG_EXPORT, &desc->flags))
504 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
509 mutex_unlock(&sysfs_lock);
514 static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
516 static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
521 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
525 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
527 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
529 /* reconfigure poll(2) support if enabled on one edge only */
530 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
531 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
532 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
534 gpio_setup_irq(desc, dev, 0);
535 status = gpio_setup_irq(desc, dev, trigger_flags);
541 static ssize_t gpio_active_low_show(struct device *dev,
542 struct device_attribute *attr, char *buf)
544 const struct gpio_desc *desc = dev_get_drvdata(dev);
547 mutex_lock(&sysfs_lock);
549 if (!test_bit(FLAG_EXPORT, &desc->flags))
552 status = sprintf(buf, "%d\n",
553 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
555 mutex_unlock(&sysfs_lock);
560 static ssize_t gpio_active_low_store(struct device *dev,
561 struct device_attribute *attr, const char *buf, size_t size)
563 struct gpio_desc *desc = dev_get_drvdata(dev);
566 mutex_lock(&sysfs_lock);
568 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
573 status = strict_strtol(buf, 0, &value);
575 status = sysfs_set_active_low(desc, dev, value != 0);
578 mutex_unlock(&sysfs_lock);
580 return status ? : size;
583 static const DEVICE_ATTR(active_low, 0644,
584 gpio_active_low_show, gpio_active_low_store);
586 static const struct attribute *gpio_attrs[] = {
587 &dev_attr_value.attr,
588 &dev_attr_active_low.attr,
592 static const struct attribute_group gpio_attr_group = {
593 .attrs = (struct attribute **) gpio_attrs,
597 * /sys/class/gpio/gpiochipN/
598 * /base ... matching gpio_chip.base (N)
599 * /label ... matching gpio_chip.label
600 * /ngpio ... matching gpio_chip.ngpio
603 static ssize_t chip_base_show(struct device *dev,
604 struct device_attribute *attr, char *buf)
606 const struct gpio_chip *chip = dev_get_drvdata(dev);
608 return sprintf(buf, "%d\n", chip->base);
610 static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
612 static ssize_t chip_label_show(struct device *dev,
613 struct device_attribute *attr, char *buf)
615 const struct gpio_chip *chip = dev_get_drvdata(dev);
617 return sprintf(buf, "%s\n", chip->label ? : "");
619 static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
621 static ssize_t chip_ngpio_show(struct device *dev,
622 struct device_attribute *attr, char *buf)
624 const struct gpio_chip *chip = dev_get_drvdata(dev);
626 return sprintf(buf, "%u\n", chip->ngpio);
628 static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
630 static const struct attribute *gpiochip_attrs[] = {
632 &dev_attr_label.attr,
633 &dev_attr_ngpio.attr,
637 static const struct attribute_group gpiochip_attr_group = {
638 .attrs = (struct attribute **) gpiochip_attrs,
642 * /sys/class/gpio/export ... write-only
643 * integer N ... number of GPIO to export (full access)
644 * /sys/class/gpio/unexport ... write-only
645 * integer N ... number of GPIO to unexport
647 static ssize_t export_store(struct class *class,
648 struct class_attribute *attr,
649 const char *buf, size_t len)
652 struct gpio_desc *desc;
655 status = strict_strtol(buf, 0, &gpio);
659 desc = gpio_to_desc(gpio);
661 /* No extra locking here; FLAG_SYSFS just signifies that the
662 * request and export were done by on behalf of userspace, so
663 * they may be undone on its behalf too.
666 status = gpiod_request(desc, "sysfs");
668 if (status == -EPROBE_DEFER)
672 status = gpiod_export(desc, true);
676 set_bit(FLAG_SYSFS, &desc->flags);
680 pr_debug("%s: status %d\n", __func__, status);
681 return status ? : len;
684 static ssize_t unexport_store(struct class *class,
685 struct class_attribute *attr,
686 const char *buf, size_t len)
689 struct gpio_desc *desc;
692 status = strict_strtol(buf, 0, &gpio);
698 desc = gpio_to_desc(gpio);
699 /* reject bogus commands (gpio_unexport ignores them) */
703 /* No extra locking here; FLAG_SYSFS just signifies that the
704 * request and export were done by on behalf of userspace, so
705 * they may be undone on its behalf too.
707 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
713 pr_debug("%s: status %d\n", __func__, status);
714 return status ? : len;
717 static struct class_attribute gpio_class_attrs[] = {
718 __ATTR(export, 0200, NULL, export_store),
719 __ATTR(unexport, 0200, NULL, unexport_store),
723 static struct class gpio_class = {
725 .owner = THIS_MODULE,
727 .class_attrs = gpio_class_attrs,
732 * gpio_export - export a GPIO through sysfs
733 * @gpio: gpio to make available, already requested
734 * @direction_may_change: true if userspace may change gpio direction
735 * Context: arch_initcall or later
737 * When drivers want to make a GPIO accessible to userspace after they
738 * have requested it -- perhaps while debugging, or as part of their
739 * public interface -- they may use this routine. If the GPIO can
740 * change direction (some can't) and the caller allows it, userspace
741 * will see "direction" sysfs attribute which may be used to change
742 * the gpio's direction. A "value" attribute will always be provided.
744 * Returns zero on success, else an error.
746 static int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
750 const char *ioname = NULL;
754 /* can't export until sysfs is available ... */
756 pr_debug("%s: called too early!\n", __func__);
761 pr_debug("%s: invalid gpio descriptor\n", __func__);
765 mutex_lock(&sysfs_lock);
767 spin_lock_irqsave(&gpio_lock, flags);
768 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
769 test_bit(FLAG_EXPORT, &desc->flags)) {
770 spin_unlock_irqrestore(&gpio_lock, flags);
771 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
772 __func__, desc_to_gpio(desc),
773 test_bit(FLAG_REQUESTED, &desc->flags),
774 test_bit(FLAG_EXPORT, &desc->flags));
779 if (!desc->chip->direction_input || !desc->chip->direction_output)
780 direction_may_change = false;
781 spin_unlock_irqrestore(&gpio_lock, flags);
783 offset = gpio_chip_hwgpio(desc);
784 if (desc->chip->names && desc->chip->names[offset])
785 ioname = desc->chip->names[offset];
787 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
788 desc, ioname ? ioname : "gpio%u",
791 status = PTR_ERR(dev);
795 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
797 goto fail_unregister_device;
799 if (direction_may_change) {
800 status = device_create_file(dev, &dev_attr_direction);
802 goto fail_unregister_device;
805 if (gpiod_to_irq(desc) >= 0 && (direction_may_change ||
806 !test_bit(FLAG_IS_OUT, &desc->flags))) {
807 status = device_create_file(dev, &dev_attr_edge);
809 goto fail_unregister_device;
812 set_bit(FLAG_EXPORT, &desc->flags);
813 mutex_unlock(&sysfs_lock);
816 fail_unregister_device:
817 device_unregister(dev);
819 mutex_unlock(&sysfs_lock);
820 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
825 int gpio_export(unsigned gpio, bool direction_may_change)
827 return gpiod_export(gpio_to_desc(gpio), direction_may_change);
829 EXPORT_SYMBOL_GPL(gpio_export);
831 static int match_export(struct device *dev, void *data)
833 return dev_get_drvdata(dev) == data;
837 * gpio_export_link - create a sysfs link to an exported GPIO node
838 * @dev: device under which to create symlink
839 * @name: name of the symlink
840 * @gpio: gpio to create symlink to, already exported
842 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
843 * node. Caller is responsible for unlinking.
845 * Returns zero on success, else an error.
847 static int gpiod_export_link(struct device *dev, const char *name,
848 struct gpio_desc *desc)
850 int status = -EINVAL;
855 mutex_lock(&sysfs_lock);
857 if (test_bit(FLAG_EXPORT, &desc->flags)) {
860 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
862 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
869 mutex_unlock(&sysfs_lock);
873 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
879 int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
881 return gpiod_export_link(dev, name, gpio_to_desc(gpio));
883 EXPORT_SYMBOL_GPL(gpio_export_link);
886 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
887 * @gpio: gpio to change
888 * @value: non-zero to use active low, i.e. inverted values
890 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
891 * The GPIO does not have to be exported yet. If poll(2) support has
892 * been enabled for either rising or falling edge, it will be
893 * reconfigured to follow the new polarity.
895 * Returns zero on success, else an error.
897 static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
899 struct device *dev = NULL;
900 int status = -EINVAL;
905 mutex_lock(&sysfs_lock);
907 if (test_bit(FLAG_EXPORT, &desc->flags)) {
908 dev = class_find_device(&gpio_class, NULL, desc, match_export);
915 status = sysfs_set_active_low(desc, dev, value);
918 mutex_unlock(&sysfs_lock);
922 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
928 int gpio_sysfs_set_active_low(unsigned gpio, int value)
930 return gpiod_sysfs_set_active_low(gpio_to_desc(gpio), value);
932 EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
935 * gpio_unexport - reverse effect of gpio_export()
936 * @gpio: gpio to make unavailable
938 * This is implicit on gpio_free().
940 static void gpiod_unexport(struct gpio_desc *desc)
943 struct device *dev = NULL;
950 mutex_lock(&sysfs_lock);
952 if (test_bit(FLAG_EXPORT, &desc->flags)) {
954 dev = class_find_device(&gpio_class, NULL, desc, match_export);
956 gpio_setup_irq(desc, dev, 0);
957 clear_bit(FLAG_EXPORT, &desc->flags);
962 mutex_unlock(&sysfs_lock);
965 device_unregister(dev);
970 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
974 void gpio_unexport(unsigned gpio)
976 gpiod_unexport(gpio_to_desc(gpio));
978 EXPORT_SYMBOL_GPL(gpio_unexport);
980 static int gpiochip_export(struct gpio_chip *chip)
985 /* Many systems register gpio chips for SOC support very early,
986 * before driver model support is available. In those cases we
987 * export this later, in gpiolib_sysfs_init() ... here we just
988 * verify that _some_ field of gpio_class got initialized.
993 /* use chip->base for the ID; it's already known to be unique */
994 mutex_lock(&sysfs_lock);
995 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
996 "gpiochip%d", chip->base);
998 status = sysfs_create_group(&dev->kobj,
999 &gpiochip_attr_group);
1001 status = PTR_ERR(dev);
1002 chip->exported = (status == 0);
1003 mutex_unlock(&sysfs_lock);
1006 unsigned long flags;
1009 spin_lock_irqsave(&gpio_lock, flags);
1011 while (gpio_desc[gpio].chip == chip)
1012 gpio_desc[gpio++].chip = NULL;
1013 spin_unlock_irqrestore(&gpio_lock, flags);
1015 pr_debug("%s: chip %s status %d\n", __func__,
1016 chip->label, status);
1022 static void gpiochip_unexport(struct gpio_chip *chip)
1027 mutex_lock(&sysfs_lock);
1028 dev = class_find_device(&gpio_class, NULL, chip, match_export);
1031 device_unregister(dev);
1036 mutex_unlock(&sysfs_lock);
1039 pr_debug("%s: chip %s status %d\n", __func__,
1040 chip->label, status);
1043 static int __init gpiolib_sysfs_init(void)
1046 unsigned long flags;
1047 struct gpio_chip *chip;
1049 status = class_register(&gpio_class);
1053 /* Scan and register the gpio_chips which registered very
1054 * early (e.g. before the class_register above was called).
1056 * We run before arch_initcall() so chip->dev nodes can have
1057 * registered, and so arch_initcall() can always gpio_export().
1059 spin_lock_irqsave(&gpio_lock, flags);
1060 list_for_each_entry(chip, &gpio_chips, list) {
1061 if (!chip || chip->exported)
1064 spin_unlock_irqrestore(&gpio_lock, flags);
1065 status = gpiochip_export(chip);
1066 spin_lock_irqsave(&gpio_lock, flags);
1068 spin_unlock_irqrestore(&gpio_lock, flags);
1073 postcore_initcall(gpiolib_sysfs_init);
1076 static inline int gpiochip_export(struct gpio_chip *chip)
1081 static inline void gpiochip_unexport(struct gpio_chip *chip)
1085 static inline int gpiod_export(struct gpio_desc *desc,
1086 bool direction_may_change)
1091 static inline int gpiod_export_link(struct device *dev, const char *name,
1092 struct gpio_desc *desc)
1097 static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
1102 static inline void gpiod_unexport(struct gpio_desc *desc)
1106 #endif /* CONFIG_GPIO_SYSFS */
1109 * Add a new chip to the global chips list, keeping the list of chips sorted
1112 * Return -EBUSY if the new chip overlaps with some other chip's integer
1115 static int gpiochip_add_to_list(struct gpio_chip *chip)
1117 struct list_head *pos = &gpio_chips;
1118 struct gpio_chip *_chip;
1121 /* find where to insert our chip */
1122 list_for_each(pos, &gpio_chips) {
1123 _chip = list_entry(pos, struct gpio_chip, list);
1124 /* shall we insert before _chip? */
1125 if (_chip->base >= chip->base + chip->ngpio)
1129 /* are we stepping on the chip right before? */
1130 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
1131 _chip = list_entry(pos->prev, struct gpio_chip, list);
1132 if (_chip->base + _chip->ngpio > chip->base) {
1134 "GPIO integer space overlap, cannot add chip\n");
1140 list_add_tail(&chip->list, pos);
1146 * gpiochip_add() - register a gpio_chip
1147 * @chip: the chip to register, with chip->base initialized
1148 * Context: potentially before irqs or kmalloc will work
1150 * Returns a negative errno if the chip can't be registered, such as
1151 * because the chip->base is invalid or already associated with a
1152 * different chip. Otherwise it returns zero as a success code.
1154 * When gpiochip_add() is called very early during boot, so that GPIOs
1155 * can be freely used, the chip->dev device must be registered before
1156 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1157 * for GPIOs will fail rudely.
1159 * If chip->base is negative, this requests dynamic assignment of
1160 * a range of valid GPIOs.
1162 int gpiochip_add(struct gpio_chip *chip)
1164 unsigned long flags;
1167 int base = chip->base;
1169 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
1175 spin_lock_irqsave(&gpio_lock, flags);
1178 base = gpiochip_find_base(chip->ngpio);
1186 status = gpiochip_add_to_list(chip);
1189 for (id = base; id < base + chip->ngpio; id++) {
1190 gpio_desc[id].chip = chip;
1192 /* REVISIT: most hardware initializes GPIOs as
1193 * inputs (often with pullups enabled) so power
1194 * usage is minimized. Linux code should set the
1195 * gpio direction first thing; but until it does,
1196 * and in case chip->get_direction is not set,
1197 * we may expose the wrong direction in sysfs.
1199 gpio_desc[id].flags = !chip->direction_input
1200 ? (1 << FLAG_IS_OUT)
1205 #ifdef CONFIG_PINCTRL
1206 INIT_LIST_HEAD(&chip->pin_ranges);
1209 of_gpiochip_add(chip);
1212 spin_unlock_irqrestore(&gpio_lock, flags);
1217 status = gpiochip_export(chip);
1221 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
1222 chip->base, chip->base + chip->ngpio - 1,
1223 chip->label ? : "generic");
1227 /* failures here can mean systems won't boot... */
1228 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1229 chip->base, chip->base + chip->ngpio - 1,
1230 chip->label ? : "generic");
1233 EXPORT_SYMBOL_GPL(gpiochip_add);
1236 * gpiochip_remove() - unregister a gpio_chip
1237 * @chip: the chip to unregister
1239 * A gpio_chip with any GPIOs still requested may not be removed.
1241 int gpiochip_remove(struct gpio_chip *chip)
1243 unsigned long flags;
1247 spin_lock_irqsave(&gpio_lock, flags);
1249 gpiochip_remove_pin_ranges(chip);
1250 of_gpiochip_remove(chip);
1252 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
1253 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
1259 for (id = chip->base; id < chip->base + chip->ngpio; id++)
1260 gpio_desc[id].chip = NULL;
1262 list_del(&chip->list);
1265 spin_unlock_irqrestore(&gpio_lock, flags);
1268 gpiochip_unexport(chip);
1272 EXPORT_SYMBOL_GPL(gpiochip_remove);
1275 * gpiochip_find() - iterator for locating a specific gpio_chip
1276 * @data: data to pass to match function
1277 * @callback: Callback function to check gpio_chip
1279 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1280 * determined by a user supplied @match callback. The callback should return
1281 * 0 if the device doesn't match and non-zero if it does. If the callback is
1282 * non-zero, this function will return to the caller and not iterate over any
1285 struct gpio_chip *gpiochip_find(void *data,
1286 int (*match)(struct gpio_chip *chip,
1289 struct gpio_chip *chip;
1290 unsigned long flags;
1292 spin_lock_irqsave(&gpio_lock, flags);
1293 list_for_each_entry(chip, &gpio_chips, list)
1294 if (match(chip, data))
1298 if (&chip->list == &gpio_chips)
1300 spin_unlock_irqrestore(&gpio_lock, flags);
1304 EXPORT_SYMBOL_GPL(gpiochip_find);
1306 #ifdef CONFIG_PINCTRL
1309 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1310 * @chip: the gpiochip to add the range for
1311 * @pinctrl_name: the dev_name() of the pin controller to map to
1312 * @gpio_offset: the start offset in the current gpio_chip number space
1313 * @pin_offset: the start offset in the pin controller number space
1314 * @npins: the number of pins from the offset of each pin space (GPIO and
1315 * pin controller) to accumulate in this range
1317 int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
1318 unsigned int gpio_offset, unsigned int pin_offset,
1321 struct gpio_pin_range *pin_range;
1324 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1326 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1331 /* Use local offset as range ID */
1332 pin_range->range.id = gpio_offset;
1333 pin_range->range.gc = chip;
1334 pin_range->range.name = chip->label;
1335 pin_range->range.base = chip->base + gpio_offset;
1336 pin_range->range.pin_base = pin_offset;
1337 pin_range->range.npins = npins;
1338 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
1340 if (IS_ERR(pin_range->pctldev)) {
1341 ret = PTR_ERR(pin_range->pctldev);
1342 pr_err("%s: GPIO chip: could not create pin range\n",
1347 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1348 chip->label, gpio_offset, gpio_offset + npins - 1,
1350 pin_offset, pin_offset + npins - 1);
1352 list_add_tail(&pin_range->node, &chip->pin_ranges);
1356 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
1359 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1360 * @chip: the chip to remove all the mappings for
1362 void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1364 struct gpio_pin_range *pin_range, *tmp;
1366 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1367 list_del(&pin_range->node);
1368 pinctrl_remove_gpio_range(pin_range->pctldev,
1373 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1375 #endif /* CONFIG_PINCTRL */
1377 /* These "optional" allocation calls help prevent drivers from stomping
1378 * on each other, and help provide better diagnostics in debugfs.
1379 * They're called even less than the "set direction" calls.
1381 static int gpiod_request(struct gpio_desc *desc, const char *label)
1383 struct gpio_chip *chip;
1384 int status = -EPROBE_DEFER;
1385 unsigned long flags;
1387 spin_lock_irqsave(&gpio_lock, flags);
1397 if (!try_module_get(chip->owner))
1400 /* NOTE: gpio_request() can be called in early boot,
1401 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1404 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1405 desc_set_label(desc, label ? : "?");
1409 module_put(chip->owner);
1413 if (chip->request) {
1414 /* chip->request may sleep */
1415 spin_unlock_irqrestore(&gpio_lock, flags);
1416 status = chip->request(chip, gpio_chip_hwgpio(desc));
1417 spin_lock_irqsave(&gpio_lock, flags);
1420 desc_set_label(desc, NULL);
1421 module_put(chip->owner);
1422 clear_bit(FLAG_REQUESTED, &desc->flags);
1426 if (chip->get_direction) {
1427 /* chip->get_direction may sleep */
1428 spin_unlock_irqrestore(&gpio_lock, flags);
1429 gpiod_get_direction(desc);
1430 spin_lock_irqsave(&gpio_lock, flags);
1434 pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
1435 desc ? desc_to_gpio(desc) : -1,
1436 label ? : "?", status);
1437 spin_unlock_irqrestore(&gpio_lock, flags);
1441 int gpio_request(unsigned gpio, const char *label)
1443 return gpiod_request(gpio_to_desc(gpio), label);
1445 EXPORT_SYMBOL_GPL(gpio_request);
1447 static void gpiod_free(struct gpio_desc *desc)
1449 unsigned long flags;
1450 struct gpio_chip *chip;
1455 WARN_ON(extra_checks);
1459 gpiod_unexport(desc);
1461 spin_lock_irqsave(&gpio_lock, flags);
1464 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1466 spin_unlock_irqrestore(&gpio_lock, flags);
1467 might_sleep_if(chip->can_sleep);
1468 chip->free(chip, gpio_chip_hwgpio(desc));
1469 spin_lock_irqsave(&gpio_lock, flags);
1471 desc_set_label(desc, NULL);
1472 module_put(desc->chip->owner);
1473 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1474 clear_bit(FLAG_REQUESTED, &desc->flags);
1475 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1476 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1478 WARN_ON(extra_checks);
1480 spin_unlock_irqrestore(&gpio_lock, flags);
1483 void gpio_free(unsigned gpio)
1485 gpiod_free(gpio_to_desc(gpio));
1487 EXPORT_SYMBOL_GPL(gpio_free);
1490 * gpio_request_one - request a single GPIO with initial configuration
1491 * @gpio: the GPIO number
1492 * @flags: GPIO configuration as specified by GPIOF_*
1493 * @label: a literal description string of this GPIO
1495 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1497 struct gpio_desc *desc;
1500 desc = gpio_to_desc(gpio);
1502 err = gpiod_request(desc, label);
1506 if (flags & GPIOF_OPEN_DRAIN)
1507 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
1509 if (flags & GPIOF_OPEN_SOURCE)
1510 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
1512 if (flags & GPIOF_DIR_IN)
1513 err = gpiod_direction_input(desc);
1515 err = gpiod_direction_output(desc,
1516 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1521 if (flags & GPIOF_EXPORT) {
1522 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
1533 EXPORT_SYMBOL_GPL(gpio_request_one);
1536 * gpio_request_array - request multiple GPIOs in a single call
1537 * @array: array of the 'struct gpio'
1538 * @num: how many GPIOs in the array
1540 int gpio_request_array(const struct gpio *array, size_t num)
1544 for (i = 0; i < num; i++, array++) {
1545 err = gpio_request_one(array->gpio, array->flags, array->label);
1553 gpio_free((--array)->gpio);
1556 EXPORT_SYMBOL_GPL(gpio_request_array);
1559 * gpio_free_array - release multiple GPIOs in a single call
1560 * @array: array of the 'struct gpio'
1561 * @num: how many GPIOs in the array
1563 void gpio_free_array(const struct gpio *array, size_t num)
1566 gpio_free((array++)->gpio);
1568 EXPORT_SYMBOL_GPL(gpio_free_array);
1571 * gpiochip_is_requested - return string iff signal was requested
1572 * @chip: controller managing the signal
1573 * @offset: of signal within controller's 0..(ngpio - 1) range
1575 * Returns NULL if the GPIO is not currently requested, else a string.
1576 * If debugfs support is enabled, the string returned is the label passed
1577 * to gpio_request(); otherwise it is a meaningless constant.
1579 * This function is for use by GPIO controller drivers. The label can
1580 * help with diagnostics, and knowing that the signal is used as a GPIO
1581 * can help avoid accidentally multiplexing it to another controller.
1583 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1585 unsigned gpio = chip->base + offset;
1586 struct gpio_desc *desc = &gpio_desc[gpio];
1588 if (!gpio_is_valid(gpio) || desc->chip != chip)
1590 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
1592 #ifdef CONFIG_DEBUG_FS
1598 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1601 /* Drivers MUST set GPIO direction before making get/set calls. In
1602 * some cases this is done in early boot, before IRQs are enabled.
1604 * As a rule these aren't called more than once (except for drivers
1605 * using the open-drain emulation idiom) so these are natural places
1606 * to accumulate extra debugging checks. Note that we can't (yet)
1607 * rely on gpio_request() having been called beforehand.
1610 static int gpiod_direction_input(struct gpio_desc *desc)
1612 unsigned long flags;
1613 struct gpio_chip *chip;
1614 int status = -EINVAL;
1617 spin_lock_irqsave(&gpio_lock, flags);
1622 if (!chip || !chip->get || !chip->direction_input)
1624 status = gpio_ensure_requested(desc);
1628 /* now we know the gpio is valid and chip won't vanish */
1630 spin_unlock_irqrestore(&gpio_lock, flags);
1632 might_sleep_if(chip->can_sleep);
1634 offset = gpio_chip_hwgpio(desc);
1636 status = chip->request(chip, offset);
1638 pr_debug("GPIO-%d: chip request fail, %d\n",
1639 desc_to_gpio(desc), status);
1640 /* and it's not available to anyone else ...
1641 * gpio_request() is the fully clean solution.
1647 status = chip->direction_input(chip, offset);
1649 clear_bit(FLAG_IS_OUT, &desc->flags);
1651 trace_gpio_direction(desc_to_gpio(desc), 1, status);
1655 spin_unlock_irqrestore(&gpio_lock, flags);
1659 gpio = desc_to_gpio(desc);
1660 pr_debug("%s: gpio-%d status %d\n",
1661 __func__, gpio, status);
1666 int gpio_direction_input(unsigned gpio)
1668 return gpiod_direction_input(gpio_to_desc(gpio));
1670 EXPORT_SYMBOL_GPL(gpio_direction_input);
1672 static int gpiod_direction_output(struct gpio_desc *desc, int value)
1674 unsigned long flags;
1675 struct gpio_chip *chip;
1676 int status = -EINVAL;
1679 /* Open drain pin should not be driven to 1 */
1680 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1681 return gpiod_direction_input(desc);
1683 /* Open source pin should not be driven to 0 */
1684 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1685 return gpiod_direction_input(desc);
1687 spin_lock_irqsave(&gpio_lock, flags);
1692 if (!chip || !chip->set || !chip->direction_output)
1694 status = gpio_ensure_requested(desc);
1698 /* now we know the gpio is valid and chip won't vanish */
1700 spin_unlock_irqrestore(&gpio_lock, flags);
1702 might_sleep_if(chip->can_sleep);
1704 offset = gpio_chip_hwgpio(desc);
1706 status = chip->request(chip, offset);
1708 pr_debug("GPIO-%d: chip request fail, %d\n",
1709 desc_to_gpio(desc), status);
1710 /* and it's not available to anyone else ...
1711 * gpio_request() is the fully clean solution.
1717 status = chip->direction_output(chip, offset, value);
1719 set_bit(FLAG_IS_OUT, &desc->flags);
1720 trace_gpio_value(desc_to_gpio(desc), 0, value);
1721 trace_gpio_direction(desc_to_gpio(desc), 0, status);
1725 spin_unlock_irqrestore(&gpio_lock, flags);
1729 gpio = desc_to_gpio(desc);
1730 pr_debug("%s: gpio-%d status %d\n",
1731 __func__, gpio, status);
1736 int gpio_direction_output(unsigned gpio, int value)
1738 return gpiod_direction_output(gpio_to_desc(gpio), value);
1740 EXPORT_SYMBOL_GPL(gpio_direction_output);
1743 * gpio_set_debounce - sets @debounce time for a @gpio
1744 * @gpio: the gpio to set debounce time
1745 * @debounce: debounce time is microseconds
1747 static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
1749 unsigned long flags;
1750 struct gpio_chip *chip;
1751 int status = -EINVAL;
1754 spin_lock_irqsave(&gpio_lock, flags);
1759 if (!chip || !chip->set || !chip->set_debounce)
1762 status = gpio_ensure_requested(desc);
1766 /* now we know the gpio is valid and chip won't vanish */
1768 spin_unlock_irqrestore(&gpio_lock, flags);
1770 might_sleep_if(chip->can_sleep);
1772 offset = gpio_chip_hwgpio(desc);
1773 return chip->set_debounce(chip, offset, debounce);
1776 spin_unlock_irqrestore(&gpio_lock, flags);
1780 gpio = desc_to_gpio(desc);
1781 pr_debug("%s: gpio-%d status %d\n",
1782 __func__, gpio, status);
1788 int gpio_set_debounce(unsigned gpio, unsigned debounce)
1790 return gpiod_set_debounce(gpio_to_desc(gpio), debounce);
1792 EXPORT_SYMBOL_GPL(gpio_set_debounce);
1794 /* I/O calls are only valid after configuration completed; the relevant
1795 * "is this a valid GPIO" error checks should already have been done.
1797 * "Get" operations are often inlinable as reading a pin value register,
1798 * and masking the relevant bit in that register.
1800 * When "set" operations are inlinable, they involve writing that mask to
1801 * one register to set a low value, or a different register to set it high.
1802 * Otherwise locking is needed, so there may be little value to inlining.
1804 *------------------------------------------------------------------------
1806 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1807 * have requested the GPIO. That can include implicit requesting by
1808 * a direction setting call. Marking a gpio as requested locks its chip
1809 * in memory, guaranteeing that these table lookups need no more locking
1810 * and that gpiochip_remove() will fail.
1812 * REVISIT when debugging, consider adding some instrumentation to ensure
1813 * that the GPIO was actually requested.
1817 * __gpio_get_value() - return a gpio's value
1818 * @gpio: gpio whose value will be returned
1821 * This is used directly or indirectly to implement gpio_get_value().
1822 * It returns the zero or nonzero value provided by the associated
1823 * gpio_chip.get() method; or zero if no such method is provided.
1825 static int gpiod_get_value(struct gpio_desc *desc)
1827 struct gpio_chip *chip;
1832 offset = gpio_chip_hwgpio(desc);
1833 /* Should be using gpio_get_value_cansleep() */
1834 WARN_ON(chip->can_sleep);
1835 value = chip->get ? chip->get(chip, offset) : 0;
1836 trace_gpio_value(desc_to_gpio(desc), 1, value);
1840 int __gpio_get_value(unsigned gpio)
1842 return gpiod_get_value(gpio_to_desc(gpio));
1844 EXPORT_SYMBOL_GPL(__gpio_get_value);
1847 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1848 * @gpio: Gpio whose state need to be set.
1850 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1852 static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
1855 struct gpio_chip *chip = desc->chip;
1856 int offset = gpio_chip_hwgpio(desc);
1859 err = chip->direction_input(chip, offset);
1861 clear_bit(FLAG_IS_OUT, &desc->flags);
1863 err = chip->direction_output(chip, offset, 0);
1865 set_bit(FLAG_IS_OUT, &desc->flags);
1867 trace_gpio_direction(desc_to_gpio(desc), value, err);
1869 pr_err("%s: Error in set_value for open drain gpio%d err %d\n",
1870 __func__, desc_to_gpio(desc), err);
1874 * _gpio_set_open_source() - Set the open source gpio's value.
1875 * @gpio: Gpio whose state need to be set.
1877 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1879 static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
1882 struct gpio_chip *chip = desc->chip;
1883 int offset = gpio_chip_hwgpio(desc);
1886 err = chip->direction_output(chip, offset, 1);
1888 set_bit(FLAG_IS_OUT, &desc->flags);
1890 err = chip->direction_input(chip, offset);
1892 clear_bit(FLAG_IS_OUT, &desc->flags);
1894 trace_gpio_direction(desc_to_gpio(desc), !value, err);
1896 pr_err("%s: Error in set_value for open source gpio%d err %d\n",
1897 __func__, desc_to_gpio(desc), err);
1901 * __gpio_set_value() - assign a gpio's value
1902 * @gpio: gpio whose value will be assigned
1903 * @value: value to assign
1906 * This is used directly or indirectly to implement gpio_set_value().
1907 * It invokes the associated gpio_chip.set() method.
1909 static void gpiod_set_value(struct gpio_desc *desc, int value)
1911 struct gpio_chip *chip;
1914 /* Should be using gpio_set_value_cansleep() */
1915 WARN_ON(chip->can_sleep);
1916 trace_gpio_value(desc_to_gpio(desc), 0, value);
1917 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1918 _gpio_set_open_drain_value(desc, value);
1919 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1920 _gpio_set_open_source_value(desc, value);
1922 chip->set(chip, gpio_chip_hwgpio(desc), value);
1925 void __gpio_set_value(unsigned gpio, int value)
1927 return gpiod_set_value(gpio_to_desc(gpio), value);
1929 EXPORT_SYMBOL_GPL(__gpio_set_value);
1932 * __gpio_cansleep() - report whether gpio value access will sleep
1933 * @gpio: gpio in question
1936 * This is used directly or indirectly to implement gpio_cansleep(). It
1937 * returns nonzero if access reading or writing the GPIO value can sleep.
1939 static int gpiod_cansleep(struct gpio_desc *desc)
1941 /* only call this on GPIOs that are valid! */
1942 return desc->chip->can_sleep;
1945 int __gpio_cansleep(unsigned gpio)
1947 return gpiod_cansleep(gpio_to_desc(gpio));
1949 EXPORT_SYMBOL_GPL(__gpio_cansleep);
1952 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1953 * @gpio: gpio whose IRQ will be returned (already requested)
1956 * This is used directly or indirectly to implement gpio_to_irq().
1957 * It returns the number of the IRQ signaled by this (input) GPIO,
1958 * or a negative errno.
1960 static int gpiod_to_irq(struct gpio_desc *desc)
1962 struct gpio_chip *chip;
1966 offset = gpio_chip_hwgpio(desc);
1967 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1970 int __gpio_to_irq(unsigned gpio)
1972 return gpiod_to_irq(gpio_to_desc(gpio));
1974 EXPORT_SYMBOL_GPL(__gpio_to_irq);
1977 /* There's no value in making it easy to inline GPIO calls that may sleep.
1978 * Common examples include ones connected to I2C or SPI chips.
1981 static int gpiod_get_value_cansleep(struct gpio_desc *desc)
1983 struct gpio_chip *chip;
1987 might_sleep_if(extra_checks);
1989 offset = gpio_chip_hwgpio(desc);
1990 value = chip->get ? chip->get(chip, offset) : 0;
1991 trace_gpio_value(desc_to_gpio(desc), 1, value);
1995 int gpio_get_value_cansleep(unsigned gpio)
1997 return gpiod_get_value_cansleep(gpio_to_desc(gpio));
1999 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
2001 static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
2003 struct gpio_chip *chip;
2005 might_sleep_if(extra_checks);
2007 trace_gpio_value(desc_to_gpio(desc), 0, value);
2008 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2009 _gpio_set_open_drain_value(desc, value);
2010 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2011 _gpio_set_open_source_value(desc, value);
2013 chip->set(chip, gpio_chip_hwgpio(desc), value);
2016 void gpio_set_value_cansleep(unsigned gpio, int value)
2018 return gpiod_set_value_cansleep(gpio_to_desc(gpio), value);
2020 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
2022 #ifdef CONFIG_DEBUG_FS
2024 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2027 unsigned gpio = chip->base;
2028 struct gpio_desc *gdesc = &gpio_desc[gpio];
2031 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2032 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2035 gpiod_get_direction(gdesc);
2036 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
2037 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
2039 is_out ? "out" : "in ",
2041 ? (chip->get(chip, i) ? "hi" : "lo")
2043 seq_printf(s, "\n");
2047 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
2049 struct gpio_chip *chip = NULL;
2050 loff_t index = *pos;
2052 /* REVISIT this isn't locked against gpio_chip removal ... */
2056 list_for_each_entry(chip, &gpio_chips, list)
2063 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2065 struct gpio_chip *chip = v;
2068 if (list_is_last(&chip->list, &gpio_chips))
2071 ret = list_entry(chip->list.next, struct gpio_chip, list);
2079 static void gpiolib_seq_stop(struct seq_file *s, void *v)
2083 static int gpiolib_seq_show(struct seq_file *s, void *v)
2085 struct gpio_chip *chip = v;
2088 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2089 chip->base, chip->base + chip->ngpio - 1);
2092 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2095 seq_printf(s, ", %s", chip->label);
2096 if (chip->can_sleep)
2097 seq_printf(s, ", can sleep");
2098 seq_printf(s, ":\n");
2101 chip->dbg_show(s, chip);
2103 gpiolib_dbg_show(s, chip);
2108 static const struct seq_operations gpiolib_seq_ops = {
2109 .start = gpiolib_seq_start,
2110 .next = gpiolib_seq_next,
2111 .stop = gpiolib_seq_stop,
2112 .show = gpiolib_seq_show,
2115 static int gpiolib_open(struct inode *inode, struct file *file)
2117 return seq_open(file, &gpiolib_seq_ops);
2120 static const struct file_operations gpiolib_operations = {
2121 .owner = THIS_MODULE,
2122 .open = gpiolib_open,
2124 .llseek = seq_lseek,
2125 .release = seq_release,
2128 static int __init gpiolib_debugfs_init(void)
2130 /* /sys/kernel/debug/gpio */
2131 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2132 NULL, NULL, &gpiolib_operations);
2135 subsys_initcall(gpiolib_debugfs_init);
2137 #endif /* DEBUG_FS */