1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/interrupt.h>
5 #include <linux/spinlock.h>
6 #include <linux/device.h>
8 #include <linux/debugfs.h>
9 #include <linux/seq_file.h>
10 #include <linux/gpio.h>
11 #include <linux/of_gpio.h>
12 #include <linux/idr.h>
13 #include <linux/slab.h>
15 #define CREATE_TRACE_POINTS
16 #include <trace/events/gpio.h>
18 /* Optional implementation infrastructure for GPIO interfaces.
20 * Platforms may want to use this if they tend to use very many GPIOs
21 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
23 * When kernel footprint or instruction count is an issue, simpler
24 * implementations may be preferred. The GPIO programming interface
25 * allows for inlining speed-critical get/set operations for common
26 * cases, so that access to SOC-integrated GPIOs can sometimes cost
27 * only an instruction or two per bit.
31 /* When debugging, extend minimal trust to callers and platform code.
32 * Also emit diagnostic messages that may help initial bringup, when
33 * board setup or driver bugs are most common.
35 * Otherwise, minimize overhead in what may be bitbanging codepaths.
38 #define extra_checks 1
40 #define extra_checks 0
43 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
44 * While any GPIO is requested, its gpio_chip is not removable;
45 * each GPIO's "requested" flag serves as a lock and refcount.
47 static DEFINE_SPINLOCK(gpio_lock);
50 struct gpio_chip *chip;
52 /* flag symbols are bit numbers */
53 #define FLAG_REQUESTED 0
55 #define FLAG_RESERVED 2
56 #define FLAG_EXPORT 3 /* protected by sysfs_lock */
57 #define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */
58 #define FLAG_TRIG_FALL 5 /* trigger on falling edge */
59 #define FLAG_TRIG_RISE 6 /* trigger on rising edge */
60 #define FLAG_ACTIVE_LOW 7 /* sysfs value has active low */
61 #define FLAG_SYSFS_DIR 10 /* show sysfs direction attribute */
63 #define ID_SHIFT 16 /* add new flags before this one */
65 #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
66 #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
68 #ifdef CONFIG_DEBUG_FS
72 static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
74 #ifdef CONFIG_GPIO_SYSFS
75 static DEFINE_IDR(dirent_idr);
78 static inline void desc_set_label(struct gpio_desc *d, const char *label)
80 #ifdef CONFIG_DEBUG_FS
85 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
86 * when setting direction, and otherwise illegal. Until board setup code
87 * and drivers use explicit requests everywhere (which won't happen when
88 * those calls have no teeth) we can't avoid autorequesting. This nag
89 * message should motivate switching to explicit requests... so should
90 * the weaker cleanup after faults, compared to gpio_request().
92 * NOTE: the autorequest mechanism is going away; at this point it's
93 * only "legal" in the sense that (old) code using it won't break yet,
94 * but instead only triggers a WARN() stack dump.
96 static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
98 const struct gpio_chip *chip = desc->chip;
99 const int gpio = chip->base + offset;
101 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
102 "autorequest GPIO-%d\n", gpio)) {
103 if (!try_module_get(chip->owner)) {
104 pr_err("GPIO-%d: module can't be gotten \n", gpio);
105 clear_bit(FLAG_REQUESTED, &desc->flags);
109 desc_set_label(desc, "[auto]");
110 /* caller must chip->request() w/o spinlock */
117 /* caller holds gpio_lock *OR* gpio is marked as requested */
118 static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
120 return gpio_desc[gpio].chip;
123 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
124 static int gpiochip_find_base(int ngpio)
130 for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
131 struct gpio_desc *desc = &gpio_desc[i];
132 struct gpio_chip *chip = desc->chip;
134 if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) {
136 if (spare == ngpio) {
143 i -= chip->ngpio - 1;
147 if (gpio_is_valid(base))
148 pr_debug("%s: found new base at %d\n", __func__, base);
153 * gpiochip_reserve() - reserve range of gpios to use with platform code only
154 * @start: starting gpio number
155 * @ngpio: number of gpios to reserve
156 * Context: platform init, potentially before irqs or kmalloc will work
158 * Returns a negative errno if any gpio within the range is already reserved
159 * or registered, else returns zero as a success code. Use this function
160 * to mark a range of gpios as unavailable for dynamic gpio number allocation,
161 * for example because its driver support is not yet loaded.
163 int __init gpiochip_reserve(int start, int ngpio)
169 if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1))
172 spin_lock_irqsave(&gpio_lock, flags);
174 for (i = start; i < start + ngpio; i++) {
175 struct gpio_desc *desc = &gpio_desc[i];
177 if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) {
182 set_bit(FLAG_RESERVED, &desc->flags);
185 pr_debug("%s: reserved gpios from %d to %d\n",
186 __func__, start, start + ngpio - 1);
188 spin_unlock_irqrestore(&gpio_lock, flags);
193 #ifdef CONFIG_GPIO_SYSFS
195 /* lock protects against unexport_gpio() being called while
196 * sysfs files are active.
198 static DEFINE_MUTEX(sysfs_lock);
201 * /sys/class/gpio/gpioN... only for GPIOs that are exported
203 * * MAY BE OMITTED if kernel won't allow direction changes
204 * * is read/write as "in" or "out"
205 * * may also be written as "high" or "low", initializing
206 * output value as specified ("out" implies "low")
208 * * always readable, subject to hardware behavior
209 * * may be writable, as zero/nonzero
211 * * configures behavior of poll(2) on /value
212 * * available only if pin can generate IRQs on input
213 * * is read/write as "none", "falling", "rising", or "both"
215 * * configures polarity of /value
216 * * is read/write as zero/nonzero
217 * * also affects existing and subsequent "falling" and "rising"
218 * /edge configuration
221 static ssize_t gpio_direction_show(struct device *dev,
222 struct device_attribute *attr, char *buf)
224 const struct gpio_desc *desc = dev_get_drvdata(dev);
227 mutex_lock(&sysfs_lock);
229 if (!test_bit(FLAG_EXPORT, &desc->flags))
232 status = sprintf(buf, "%s\n",
233 test_bit(FLAG_IS_OUT, &desc->flags)
236 mutex_unlock(&sysfs_lock);
240 static ssize_t gpio_direction_store(struct device *dev,
241 struct device_attribute *attr, const char *buf, size_t size)
243 const struct gpio_desc *desc = dev_get_drvdata(dev);
244 unsigned gpio = desc - gpio_desc;
247 mutex_lock(&sysfs_lock);
249 if (!test_bit(FLAG_EXPORT, &desc->flags))
251 else if (sysfs_streq(buf, "high"))
252 status = gpio_direction_output(gpio, 1);
253 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
254 status = gpio_direction_output(gpio, 0);
255 else if (sysfs_streq(buf, "in"))
256 status = gpio_direction_input(gpio);
260 mutex_unlock(&sysfs_lock);
261 return status ? : size;
264 static /* const */ DEVICE_ATTR(direction, 0644,
265 gpio_direction_show, gpio_direction_store);
267 static ssize_t gpio_value_show(struct device *dev,
268 struct device_attribute *attr, char *buf)
270 const struct gpio_desc *desc = dev_get_drvdata(dev);
271 unsigned gpio = desc - gpio_desc;
274 mutex_lock(&sysfs_lock);
276 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
281 value = !!gpio_get_value_cansleep(gpio);
282 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
285 status = sprintf(buf, "%d\n", value);
288 mutex_unlock(&sysfs_lock);
292 static ssize_t gpio_value_store(struct device *dev,
293 struct device_attribute *attr, const char *buf, size_t size)
295 const struct gpio_desc *desc = dev_get_drvdata(dev);
296 unsigned gpio = desc - gpio_desc;
299 mutex_lock(&sysfs_lock);
301 if (!test_bit(FLAG_EXPORT, &desc->flags))
303 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
308 status = strict_strtol(buf, 0, &value);
310 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
312 gpio_set_value_cansleep(gpio, value != 0);
317 mutex_unlock(&sysfs_lock);
321 static DEVICE_ATTR(value, 0644,
322 gpio_value_show, gpio_value_store);
324 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
326 struct sysfs_dirent *value_sd = priv;
328 sysfs_notify_dirent(value_sd);
332 static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
333 unsigned long gpio_flags)
335 struct sysfs_dirent *value_sd;
336 unsigned long irq_flags;
339 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
342 irq = gpio_to_irq(desc - gpio_desc);
346 id = desc->flags >> ID_SHIFT;
347 value_sd = idr_find(&dirent_idr, id);
349 free_irq(irq, value_sd);
351 desc->flags &= ~GPIO_TRIGGER_MASK;
358 irq_flags = IRQF_SHARED;
359 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
360 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
361 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
362 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
363 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
364 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
367 value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
375 if (idr_pre_get(&dirent_idr, GFP_KERNEL))
376 ret = idr_get_new_above(&dirent_idr, value_sd,
378 } while (ret == -EAGAIN);
383 desc->flags &= GPIO_FLAGS_MASK;
384 desc->flags |= (unsigned long)id << ID_SHIFT;
386 if (desc->flags >> ID_SHIFT != id) {
392 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
393 "gpiolib", value_sd);
397 desc->flags |= gpio_flags;
401 idr_remove(&dirent_idr, id);
402 desc->flags &= GPIO_FLAGS_MASK;
410 static const struct {
413 } trigger_types[] = {
415 { "falling", BIT(FLAG_TRIG_FALL) },
416 { "rising", BIT(FLAG_TRIG_RISE) },
417 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
420 static ssize_t gpio_edge_show(struct device *dev,
421 struct device_attribute *attr, char *buf)
423 const struct gpio_desc *desc = dev_get_drvdata(dev);
426 mutex_lock(&sysfs_lock);
428 if (!test_bit(FLAG_EXPORT, &desc->flags))
434 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
435 if ((desc->flags & GPIO_TRIGGER_MASK)
436 == trigger_types[i].flags) {
437 status = sprintf(buf, "%s\n",
438 trigger_types[i].name);
443 mutex_unlock(&sysfs_lock);
447 static ssize_t gpio_edge_store(struct device *dev,
448 struct device_attribute *attr, const char *buf, size_t size)
450 struct gpio_desc *desc = dev_get_drvdata(dev);
454 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
455 if (sysfs_streq(trigger_types[i].name, buf))
460 mutex_lock(&sysfs_lock);
462 if (!test_bit(FLAG_EXPORT, &desc->flags))
465 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
470 mutex_unlock(&sysfs_lock);
475 static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
477 static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
482 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
486 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
488 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
490 /* reconfigure poll(2) support if enabled on one edge only */
491 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
492 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
493 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
495 gpio_setup_irq(desc, dev, 0);
496 status = gpio_setup_irq(desc, dev, trigger_flags);
502 static ssize_t gpio_active_low_show(struct device *dev,
503 struct device_attribute *attr, char *buf)
505 const struct gpio_desc *desc = dev_get_drvdata(dev);
508 mutex_lock(&sysfs_lock);
510 if (!test_bit(FLAG_EXPORT, &desc->flags))
513 status = sprintf(buf, "%d\n",
514 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
516 mutex_unlock(&sysfs_lock);
521 static ssize_t gpio_active_low_store(struct device *dev,
522 struct device_attribute *attr, const char *buf, size_t size)
524 struct gpio_desc *desc = dev_get_drvdata(dev);
527 mutex_lock(&sysfs_lock);
529 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
534 status = strict_strtol(buf, 0, &value);
536 status = sysfs_set_active_low(desc, dev, value != 0);
539 mutex_unlock(&sysfs_lock);
541 return status ? : size;
544 static DEVICE_ATTR(active_low, 0644,
545 gpio_active_low_show, gpio_active_low_store);
547 static mode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
550 struct device *dev = container_of(kobj, struct device, kobj);
551 struct gpio_desc *desc = dev_get_drvdata(dev);
552 unsigned gpio = desc - gpio_desc;
553 mode_t mode = attr->mode;
554 bool show_direction = test_bit(FLAG_SYSFS_DIR, &desc->flags);
556 if (attr == &dev_attr_direction.attr) {
559 } else if (attr == &dev_attr_edge.attr) {
560 if (gpio_to_irq(gpio) < 0)
562 if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
569 static struct attribute *gpio_attrs[] = {
570 &dev_attr_direction.attr,
572 &dev_attr_value.attr,
573 &dev_attr_active_low.attr,
577 static const struct attribute_group gpio_group = {
579 .is_visible = gpio_is_visible,
582 static const struct attribute_group *gpio_groups[] = {
588 * /sys/class/gpio/gpiochipN/
589 * /base ... matching gpio_chip.base (N)
590 * /label ... matching gpio_chip.label
591 * /ngpio ... matching gpio_chip.ngpio
594 static ssize_t chip_base_show(struct device *dev,
595 struct device_attribute *attr, char *buf)
597 const struct gpio_chip *chip = dev_get_drvdata(dev);
599 return sprintf(buf, "%d\n", chip->base);
601 static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
603 static ssize_t chip_label_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, "%s\n", chip->label ? : "");
610 static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
612 static ssize_t chip_ngpio_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, "%u\n", chip->ngpio);
619 static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
621 static struct attribute *gpiochip_attrs[] = {
623 &dev_attr_label.attr,
624 &dev_attr_ngpio.attr,
627 ATTRIBUTE_GROUPS(gpiochip);
630 * /sys/class/gpio/export ... write-only
631 * integer N ... number of GPIO to export (full access)
632 * /sys/class/gpio/unexport ... write-only
633 * integer N ... number of GPIO to unexport
635 static ssize_t export_store(struct class *class,
636 struct class_attribute *attr,
637 const char *buf, size_t len)
642 status = strict_strtol(buf, 0, &gpio);
646 /* No extra locking here; FLAG_SYSFS just signifies that the
647 * request and export were done by on behalf of userspace, so
648 * they may be undone on its behalf too.
651 status = gpio_request(gpio, "sysfs");
655 status = gpio_export(gpio, true);
659 set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
663 pr_debug("%s: status %d\n", __func__, status);
664 return status ? : len;
667 static ssize_t unexport_store(struct class *class,
668 struct class_attribute *attr,
669 const char *buf, size_t len)
674 status = strict_strtol(buf, 0, &gpio);
680 /* reject bogus commands (gpio_unexport ignores them) */
681 if (!gpio_is_valid(gpio))
684 /* No extra locking here; FLAG_SYSFS just signifies that the
685 * request and export were done by on behalf of userspace, so
686 * they may be undone on its behalf too.
688 if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
694 pr_debug("%s: status %d\n", __func__, status);
695 return status ? : len;
698 static struct class_attribute gpio_class_attrs[] = {
699 __ATTR(export, 0200, NULL, export_store),
700 __ATTR(unexport, 0200, NULL, unexport_store),
704 static struct class gpio_class = {
706 .owner = THIS_MODULE,
708 .class_attrs = gpio_class_attrs,
713 * gpio_export - export a GPIO through sysfs
714 * @gpio: gpio to make available, already requested
715 * @direction_may_change: true if userspace may change gpio direction
716 * Context: arch_initcall or later
718 * When drivers want to make a GPIO accessible to userspace after they
719 * have requested it -- perhaps while debugging, or as part of their
720 * public interface -- they may use this routine. If the GPIO can
721 * change direction (some can't) and the caller allows it, userspace
722 * will see "direction" sysfs attribute which may be used to change
723 * the gpio's direction. A "value" attribute will always be provided.
725 * Returns zero on success, else an error.
727 int gpio_export(unsigned gpio, bool direction_may_change)
730 struct gpio_desc *desc;
732 const char *ioname = NULL;
735 /* can't export until sysfs is available ... */
737 pr_debug("%s: called too early!\n", __func__);
741 if (!gpio_is_valid(gpio)) {
742 pr_debug("%s: gpio %d is not valid\n", __func__, gpio);
746 mutex_lock(&sysfs_lock);
748 spin_lock_irqsave(&gpio_lock, flags);
749 desc = &gpio_desc[gpio];
750 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
751 test_bit(FLAG_EXPORT, &desc->flags)) {
752 spin_unlock_irqrestore(&gpio_lock, flags);
753 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
755 test_bit(FLAG_REQUESTED, &desc->flags),
756 test_bit(FLAG_EXPORT, &desc->flags));
760 if (desc->chip->direction_input && desc->chip->direction_output &&
761 direction_may_change) {
762 set_bit(FLAG_SYSFS_DIR, &desc->flags);
765 spin_unlock_irqrestore(&gpio_lock, flags);
767 if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
768 ioname = desc->chip->names[gpio - desc->chip->base];
770 dev = device_create_with_groups(&gpio_class, desc->chip->dev,
771 MKDEV(0, 0), desc, gpio_groups,
772 ioname ? ioname : "gpio%u", gpio);
774 status = PTR_ERR(dev);
778 set_bit(FLAG_EXPORT, &desc->flags);
779 mutex_unlock(&sysfs_lock);
783 mutex_unlock(&sysfs_lock);
784 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
787 EXPORT_SYMBOL_GPL(gpio_export);
789 static int match_export(struct device *dev, void *data)
791 return dev_get_drvdata(dev) == data;
795 * gpio_export_link - create a sysfs link to an exported GPIO node
796 * @dev: device under which to create symlink
797 * @name: name of the symlink
798 * @gpio: gpio to create symlink to, already exported
800 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
801 * node. Caller is responsible for unlinking.
803 * Returns zero on success, else an error.
805 int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
807 struct gpio_desc *desc;
808 int status = -EINVAL;
810 if (!gpio_is_valid(gpio))
813 mutex_lock(&sysfs_lock);
815 desc = &gpio_desc[gpio];
817 if (test_bit(FLAG_EXPORT, &desc->flags)) {
820 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
822 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
830 mutex_unlock(&sysfs_lock);
834 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
838 EXPORT_SYMBOL_GPL(gpio_export_link);
842 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
843 * @gpio: gpio to change
844 * @value: non-zero to use active low, i.e. inverted values
846 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
847 * The GPIO does not have to be exported yet. If poll(2) support has
848 * been enabled for either rising or falling edge, it will be
849 * reconfigured to follow the new polarity.
851 * Returns zero on success, else an error.
853 int gpio_sysfs_set_active_low(unsigned gpio, int value)
855 struct gpio_desc *desc;
856 struct device *dev = NULL;
857 int status = -EINVAL;
859 if (!gpio_is_valid(gpio))
862 mutex_lock(&sysfs_lock);
864 desc = &gpio_desc[gpio];
866 if (test_bit(FLAG_EXPORT, &desc->flags)) {
867 dev = class_find_device(&gpio_class, NULL, desc, match_export);
874 status = sysfs_set_active_low(desc, dev, value);
877 mutex_unlock(&sysfs_lock);
881 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
885 EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
888 * gpio_unexport - reverse effect of gpio_export()
889 * @gpio: gpio to make unavailable
891 * This is implicit on gpio_free().
893 void gpio_unexport(unsigned gpio)
895 struct gpio_desc *desc;
897 struct device *dev = NULL;
899 if (!gpio_is_valid(gpio)) {
904 mutex_lock(&sysfs_lock);
906 desc = &gpio_desc[gpio];
908 if (test_bit(FLAG_EXPORT, &desc->flags)) {
910 dev = class_find_device(&gpio_class, NULL, desc, match_export);
912 gpio_setup_irq(desc, dev, 0);
913 clear_bit(FLAG_SYSFS_DIR, &desc->flags);
914 clear_bit(FLAG_EXPORT, &desc->flags);
919 mutex_unlock(&sysfs_lock);
921 device_unregister(dev);
926 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
928 EXPORT_SYMBOL_GPL(gpio_unexport);
930 static int gpiochip_export(struct gpio_chip *chip)
935 /* Many systems register gpio chips for SOC support very early,
936 * before driver model support is available. In those cases we
937 * export this later, in gpiolib_sysfs_init() ... here we just
938 * verify that _some_ field of gpio_class got initialized.
943 /* use chip->base for the ID; it's already known to be unique */
944 mutex_lock(&sysfs_lock);
945 dev = device_create_with_groups(&gpio_class, chip->dev, MKDEV(0, 0),
946 chip, gpiochip_groups,
947 "gpiochip%d", chip->base);
949 status = PTR_ERR(dev);
952 chip->exported = (status == 0);
953 mutex_unlock(&sysfs_lock);
959 spin_lock_irqsave(&gpio_lock, flags);
961 while (gpio_desc[gpio].chip == chip)
962 gpio_desc[gpio++].chip = NULL;
963 spin_unlock_irqrestore(&gpio_lock, flags);
965 pr_debug("%s: chip %s status %d\n", __func__,
966 chip->label, status);
972 static void gpiochip_unexport(struct gpio_chip *chip)
977 mutex_lock(&sysfs_lock);
978 dev = class_find_device(&gpio_class, NULL, chip, match_export);
981 device_unregister(dev);
986 mutex_unlock(&sysfs_lock);
989 pr_debug("%s: chip %s status %d\n", __func__,
990 chip->label, status);
993 static int __init gpiolib_sysfs_init(void)
999 status = class_register(&gpio_class);
1003 /* Scan and register the gpio_chips which registered very
1004 * early (e.g. before the class_register above was called).
1006 * We run before arch_initcall() so chip->dev nodes can have
1007 * registered, and so arch_initcall() can always gpio_export().
1009 spin_lock_irqsave(&gpio_lock, flags);
1010 for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
1011 struct gpio_chip *chip;
1013 chip = gpio_desc[gpio].chip;
1014 if (!chip || chip->exported)
1017 spin_unlock_irqrestore(&gpio_lock, flags);
1018 status = gpiochip_export(chip);
1019 spin_lock_irqsave(&gpio_lock, flags);
1021 spin_unlock_irqrestore(&gpio_lock, flags);
1026 postcore_initcall(gpiolib_sysfs_init);
1029 static inline int gpiochip_export(struct gpio_chip *chip)
1034 static inline void gpiochip_unexport(struct gpio_chip *chip)
1038 #endif /* CONFIG_GPIO_SYSFS */
1041 * gpiochip_add() - register a gpio_chip
1042 * @chip: the chip to register, with chip->base initialized
1043 * Context: potentially before irqs or kmalloc will work
1045 * Returns a negative errno if the chip can't be registered, such as
1046 * because the chip->base is invalid or already associated with a
1047 * different chip. Otherwise it returns zero as a success code.
1049 * When gpiochip_add() is called very early during boot, so that GPIOs
1050 * can be freely used, the chip->dev device must be registered before
1051 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1052 * for GPIOs will fail rudely.
1054 * If chip->base is negative, this requests dynamic assignment of
1055 * a range of valid GPIOs.
1057 int gpiochip_add(struct gpio_chip *chip)
1059 unsigned long flags;
1062 int base = chip->base;
1064 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
1070 spin_lock_irqsave(&gpio_lock, flags);
1073 base = gpiochip_find_base(chip->ngpio);
1081 /* these GPIO numbers must not be managed by another gpio_chip */
1082 for (id = base; id < base + chip->ngpio; id++) {
1083 if (gpio_desc[id].chip != NULL) {
1089 for (id = base; id < base + chip->ngpio; id++) {
1090 gpio_desc[id].chip = chip;
1092 /* REVISIT: most hardware initializes GPIOs as
1093 * inputs (often with pullups enabled) so power
1094 * usage is minimized. Linux code should set the
1095 * gpio direction first thing; but until it does,
1096 * we may expose the wrong direction in sysfs.
1098 gpio_desc[id].flags = !chip->direction_input
1099 ? (1 << FLAG_IS_OUT)
1103 of_gpiochip_add(chip);
1107 spin_unlock_irqrestore(&gpio_lock, flags);
1112 status = gpiochip_export(chip);
1114 of_gpiochip_remove(chip);
1120 /* failures here can mean systems won't boot... */
1121 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1122 chip->base, chip->base + chip->ngpio - 1,
1123 chip->label ? : "generic");
1126 EXPORT_SYMBOL_GPL(gpiochip_add);
1129 * gpiochip_remove() - unregister a gpio_chip
1130 * @chip: the chip to unregister
1132 * A gpio_chip with any GPIOs still requested may not be removed.
1134 int gpiochip_remove(struct gpio_chip *chip)
1136 unsigned long flags;
1140 spin_lock_irqsave(&gpio_lock, flags);
1142 of_gpiochip_remove(chip);
1144 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
1145 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
1151 for (id = chip->base; id < chip->base + chip->ngpio; id++)
1152 gpio_desc[id].chip = NULL;
1155 spin_unlock_irqrestore(&gpio_lock, flags);
1158 gpiochip_unexport(chip);
1162 EXPORT_SYMBOL_GPL(gpiochip_remove);
1165 * gpiochip_find() - iterator for locating a specific gpio_chip
1166 * @data: data to pass to match function
1167 * @callback: Callback function to check gpio_chip
1169 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1170 * determined by a user supplied @match callback. The callback should return
1171 * 0 if the device doesn't match and non-zero if it does. If the callback is
1172 * non-zero, this function will return to the caller and not iterate over any
1175 struct gpio_chip *gpiochip_find(void *data,
1176 int (*match)(struct gpio_chip *chip, void *data))
1178 struct gpio_chip *chip = NULL;
1179 unsigned long flags;
1182 spin_lock_irqsave(&gpio_lock, flags);
1183 for (i = 0; i < ARCH_NR_GPIOS; i++) {
1184 if (!gpio_desc[i].chip)
1187 if (match(gpio_desc[i].chip, data)) {
1188 chip = gpio_desc[i].chip;
1192 spin_unlock_irqrestore(&gpio_lock, flags);
1196 EXPORT_SYMBOL_GPL(gpiochip_find);
1198 /* These "optional" allocation calls help prevent drivers from stomping
1199 * on each other, and help provide better diagnostics in debugfs.
1200 * They're called even less than the "set direction" calls.
1202 int gpio_request(unsigned gpio, const char *label)
1204 struct gpio_desc *desc;
1205 struct gpio_chip *chip;
1206 int status = -EINVAL;
1207 unsigned long flags;
1209 spin_lock_irqsave(&gpio_lock, flags);
1211 if (!gpio_is_valid(gpio))
1213 desc = &gpio_desc[gpio];
1218 if (!try_module_get(chip->owner))
1221 /* NOTE: gpio_request() can be called in early boot,
1222 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1225 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1226 desc_set_label(desc, label ? : "?");
1230 module_put(chip->owner);
1234 if (chip->request) {
1235 /* chip->request may sleep */
1236 spin_unlock_irqrestore(&gpio_lock, flags);
1237 status = chip->request(chip, gpio - chip->base);
1238 spin_lock_irqsave(&gpio_lock, flags);
1241 desc_set_label(desc, NULL);
1242 module_put(chip->owner);
1243 clear_bit(FLAG_REQUESTED, &desc->flags);
1249 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
1250 gpio, label ? : "?", status);
1251 spin_unlock_irqrestore(&gpio_lock, flags);
1254 EXPORT_SYMBOL_GPL(gpio_request);
1256 void gpio_free(unsigned gpio)
1258 unsigned long flags;
1259 struct gpio_desc *desc;
1260 struct gpio_chip *chip;
1264 if (!gpio_is_valid(gpio)) {
1265 WARN_ON(extra_checks);
1269 gpio_unexport(gpio);
1271 spin_lock_irqsave(&gpio_lock, flags);
1273 desc = &gpio_desc[gpio];
1275 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1277 spin_unlock_irqrestore(&gpio_lock, flags);
1278 might_sleep_if(chip->can_sleep);
1279 chip->free(chip, gpio - chip->base);
1280 spin_lock_irqsave(&gpio_lock, flags);
1282 desc_set_label(desc, NULL);
1283 module_put(desc->chip->owner);
1284 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1285 clear_bit(FLAG_REQUESTED, &desc->flags);
1287 WARN_ON(extra_checks);
1289 spin_unlock_irqrestore(&gpio_lock, flags);
1291 EXPORT_SYMBOL_GPL(gpio_free);
1294 * gpio_request_one - request a single GPIO with initial configuration
1295 * @gpio: the GPIO number
1296 * @flags: GPIO configuration as specified by GPIOF_*
1297 * @label: a literal description string of this GPIO
1299 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1303 err = gpio_request(gpio, label);
1307 if (flags & GPIOF_DIR_IN)
1308 err = gpio_direction_input(gpio);
1310 err = gpio_direction_output(gpio,
1311 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1318 EXPORT_SYMBOL_GPL(gpio_request_one);
1321 * gpio_request_array - request multiple GPIOs in a single call
1322 * @array: array of the 'struct gpio'
1323 * @num: how many GPIOs in the array
1325 int gpio_request_array(const struct gpio *array, size_t num)
1329 for (i = 0; i < num; i++, array++) {
1330 err = gpio_request_one(array->gpio, array->flags, array->label);
1338 gpio_free((--array)->gpio);
1341 EXPORT_SYMBOL_GPL(gpio_request_array);
1344 * gpio_free_array - release multiple GPIOs in a single call
1345 * @array: array of the 'struct gpio'
1346 * @num: how many GPIOs in the array
1348 void gpio_free_array(const struct gpio *array, size_t num)
1351 gpio_free((array++)->gpio);
1353 EXPORT_SYMBOL_GPL(gpio_free_array);
1356 * gpiochip_is_requested - return string iff signal was requested
1357 * @chip: controller managing the signal
1358 * @offset: of signal within controller's 0..(ngpio - 1) range
1360 * Returns NULL if the GPIO is not currently requested, else a string.
1361 * If debugfs support is enabled, the string returned is the label passed
1362 * to gpio_request(); otherwise it is a meaningless constant.
1364 * This function is for use by GPIO controller drivers. The label can
1365 * help with diagnostics, and knowing that the signal is used as a GPIO
1366 * can help avoid accidentally multiplexing it to another controller.
1368 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1370 unsigned gpio = chip->base + offset;
1372 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
1374 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
1376 #ifdef CONFIG_DEBUG_FS
1377 return gpio_desc[gpio].label;
1382 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1385 /* Drivers MUST set GPIO direction before making get/set calls. In
1386 * some cases this is done in early boot, before IRQs are enabled.
1388 * As a rule these aren't called more than once (except for drivers
1389 * using the open-drain emulation idiom) so these are natural places
1390 * to accumulate extra debugging checks. Note that we can't (yet)
1391 * rely on gpio_request() having been called beforehand.
1394 int gpio_direction_input(unsigned gpio)
1396 unsigned long flags;
1397 struct gpio_chip *chip;
1398 struct gpio_desc *desc = &gpio_desc[gpio];
1399 int status = -EINVAL;
1401 spin_lock_irqsave(&gpio_lock, flags);
1403 if (!gpio_is_valid(gpio))
1406 if (!chip || !chip->get || !chip->direction_input)
1409 if (gpio >= chip->ngpio)
1411 status = gpio_ensure_requested(desc, gpio);
1415 /* now we know the gpio is valid and chip won't vanish */
1417 spin_unlock_irqrestore(&gpio_lock, flags);
1419 might_sleep_if(chip->can_sleep);
1422 status = chip->request(chip, gpio);
1424 pr_debug("GPIO-%d: chip request fail, %d\n",
1425 chip->base + gpio, status);
1426 /* and it's not available to anyone else ...
1427 * gpio_request() is the fully clean solution.
1433 status = chip->direction_input(chip, gpio);
1435 clear_bit(FLAG_IS_OUT, &desc->flags);
1437 trace_gpio_direction(chip->base + gpio, 1, status);
1441 spin_unlock_irqrestore(&gpio_lock, flags);
1443 pr_debug("%s: gpio-%d status %d\n",
1444 __func__, gpio, status);
1447 EXPORT_SYMBOL_GPL(gpio_direction_input);
1449 int gpio_direction_output(unsigned gpio, int value)
1451 unsigned long flags;
1452 struct gpio_chip *chip;
1453 struct gpio_desc *desc = &gpio_desc[gpio];
1454 int status = -EINVAL;
1456 spin_lock_irqsave(&gpio_lock, flags);
1458 if (!gpio_is_valid(gpio))
1461 if (!chip || !chip->set || !chip->direction_output)
1464 if (gpio >= chip->ngpio)
1466 status = gpio_ensure_requested(desc, gpio);
1470 /* now we know the gpio is valid and chip won't vanish */
1472 spin_unlock_irqrestore(&gpio_lock, flags);
1474 might_sleep_if(chip->can_sleep);
1477 status = chip->request(chip, gpio);
1479 pr_debug("GPIO-%d: chip request fail, %d\n",
1480 chip->base + gpio, status);
1481 /* and it's not available to anyone else ...
1482 * gpio_request() is the fully clean solution.
1488 status = chip->direction_output(chip, gpio, value);
1490 set_bit(FLAG_IS_OUT, &desc->flags);
1491 trace_gpio_value(chip->base + gpio, 0, value);
1492 trace_gpio_direction(chip->base + gpio, 0, status);
1496 spin_unlock_irqrestore(&gpio_lock, flags);
1498 pr_debug("%s: gpio-%d status %d\n",
1499 __func__, gpio, status);
1502 EXPORT_SYMBOL_GPL(gpio_direction_output);
1505 * gpio_set_debounce - sets @debounce time for a @gpio
1506 * @gpio: the gpio to set debounce time
1507 * @debounce: debounce time is microseconds
1509 int gpio_set_debounce(unsigned gpio, unsigned debounce)
1511 unsigned long flags;
1512 struct gpio_chip *chip;
1513 struct gpio_desc *desc = &gpio_desc[gpio];
1514 int status = -EINVAL;
1516 spin_lock_irqsave(&gpio_lock, flags);
1518 if (!gpio_is_valid(gpio))
1521 if (!chip || !chip->set || !chip->set_debounce)
1524 if (gpio >= chip->ngpio)
1526 status = gpio_ensure_requested(desc, gpio);
1530 /* now we know the gpio is valid and chip won't vanish */
1532 spin_unlock_irqrestore(&gpio_lock, flags);
1534 might_sleep_if(chip->can_sleep);
1536 return chip->set_debounce(chip, gpio, debounce);
1539 spin_unlock_irqrestore(&gpio_lock, flags);
1541 pr_debug("%s: gpio-%d status %d\n",
1542 __func__, gpio, status);
1546 EXPORT_SYMBOL_GPL(gpio_set_debounce);
1548 /* I/O calls are only valid after configuration completed; the relevant
1549 * "is this a valid GPIO" error checks should already have been done.
1551 * "Get" operations are often inlinable as reading a pin value register,
1552 * and masking the relevant bit in that register.
1554 * When "set" operations are inlinable, they involve writing that mask to
1555 * one register to set a low value, or a different register to set it high.
1556 * Otherwise locking is needed, so there may be little value to inlining.
1558 *------------------------------------------------------------------------
1560 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1561 * have requested the GPIO. That can include implicit requesting by
1562 * a direction setting call. Marking a gpio as requested locks its chip
1563 * in memory, guaranteeing that these table lookups need no more locking
1564 * and that gpiochip_remove() will fail.
1566 * REVISIT when debugging, consider adding some instrumentation to ensure
1567 * that the GPIO was actually requested.
1571 * __gpio_get_value() - return a gpio's value
1572 * @gpio: gpio whose value will be returned
1575 * This is used directly or indirectly to implement gpio_get_value().
1576 * It returns the zero or nonzero value provided by the associated
1577 * gpio_chip.get() method; or zero if no such method is provided.
1579 int __gpio_get_value(unsigned gpio)
1581 struct gpio_chip *chip;
1584 chip = gpio_to_chip(gpio);
1585 WARN_ON(chip->can_sleep);
1586 value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
1587 trace_gpio_value(gpio, 1, value);
1590 EXPORT_SYMBOL_GPL(__gpio_get_value);
1593 * __gpio_set_value() - assign a gpio's value
1594 * @gpio: gpio whose value will be assigned
1595 * @value: value to assign
1598 * This is used directly or indirectly to implement gpio_set_value().
1599 * It invokes the associated gpio_chip.set() method.
1601 void __gpio_set_value(unsigned gpio, int value)
1603 struct gpio_chip *chip;
1605 chip = gpio_to_chip(gpio);
1606 WARN_ON(chip->can_sleep);
1607 trace_gpio_value(gpio, 0, value);
1608 chip->set(chip, gpio - chip->base, value);
1610 EXPORT_SYMBOL_GPL(__gpio_set_value);
1613 * __gpio_cansleep() - report whether gpio value access will sleep
1614 * @gpio: gpio in question
1617 * This is used directly or indirectly to implement gpio_cansleep(). It
1618 * returns nonzero if access reading or writing the GPIO value can sleep.
1620 int __gpio_cansleep(unsigned gpio)
1622 struct gpio_chip *chip;
1624 /* only call this on GPIOs that are valid! */
1625 chip = gpio_to_chip(gpio);
1627 return chip->can_sleep;
1629 EXPORT_SYMBOL_GPL(__gpio_cansleep);
1632 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1633 * @gpio: gpio whose IRQ will be returned (already requested)
1636 * This is used directly or indirectly to implement gpio_to_irq().
1637 * It returns the number of the IRQ signaled by this (input) GPIO,
1638 * or a negative errno.
1640 int __gpio_to_irq(unsigned gpio)
1642 struct gpio_chip *chip;
1644 chip = gpio_to_chip(gpio);
1645 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1647 EXPORT_SYMBOL_GPL(__gpio_to_irq);
1651 /* There's no value in making it easy to inline GPIO calls that may sleep.
1652 * Common examples include ones connected to I2C or SPI chips.
1655 int gpio_get_value_cansleep(unsigned gpio)
1657 struct gpio_chip *chip;
1660 might_sleep_if(extra_checks);
1661 chip = gpio_to_chip(gpio);
1662 value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
1663 trace_gpio_value(gpio, 1, value);
1666 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1668 void gpio_set_value_cansleep(unsigned gpio, int value)
1670 struct gpio_chip *chip;
1672 might_sleep_if(extra_checks);
1673 chip = gpio_to_chip(gpio);
1674 trace_gpio_value(gpio, 0, value);
1675 chip->set(chip, gpio - chip->base, value);
1677 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1680 #ifdef CONFIG_DEBUG_FS
1682 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1685 unsigned gpio = chip->base;
1686 struct gpio_desc *gdesc = &gpio_desc[gpio];
1689 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1690 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1693 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
1694 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
1696 is_out ? "out" : "in ",
1698 ? (chip->get(chip, i) ? "hi" : "lo")
1700 seq_printf(s, "\n");
1704 static int gpiolib_show(struct seq_file *s, void *unused)
1706 struct gpio_chip *chip = NULL;
1710 /* REVISIT this isn't locked against gpio_chip removal ... */
1712 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
1715 if (chip == gpio_desc[gpio].chip)
1717 chip = gpio_desc[gpio].chip;
1721 seq_printf(s, "%sGPIOs %d-%d",
1722 started ? "\n" : "",
1723 chip->base, chip->base + chip->ngpio - 1);
1726 seq_printf(s, ", %s/%s",
1727 dev->bus ? dev->bus->name : "no-bus",
1730 seq_printf(s, ", %s", chip->label);
1731 if (chip->can_sleep)
1732 seq_printf(s, ", can sleep");
1733 seq_printf(s, ":\n");
1737 chip->dbg_show(s, chip);
1739 gpiolib_dbg_show(s, chip);
1744 static int gpiolib_open(struct inode *inode, struct file *file)
1746 return single_open(file, gpiolib_show, NULL);
1749 static const struct file_operations gpiolib_operations = {
1750 .open = gpiolib_open,
1752 .llseek = seq_lseek,
1753 .release = single_release,
1756 static int __init gpiolib_debugfs_init(void)
1758 /* /sys/kernel/debug/gpio */
1759 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1760 NULL, NULL, &gpiolib_operations);
1763 subsys_initcall(gpiolib_debugfs_init);
1765 #endif /* DEBUG_FS */