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>
16 /* Optional implementation infrastructure for GPIO interfaces.
18 * Platforms may want to use this if they tend to use very many GPIOs
19 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
21 * When kernel footprint or instruction count is an issue, simpler
22 * implementations may be preferred. The GPIO programming interface
23 * allows for inlining speed-critical get/set operations for common
24 * cases, so that access to SOC-integrated GPIOs can sometimes cost
25 * only an instruction or two per bit.
29 /* When debugging, extend minimal trust to callers and platform code.
30 * Also emit diagnostic messages that may help initial bringup, when
31 * board setup or driver bugs are most common.
33 * Otherwise, minimize overhead in what may be bitbanging codepaths.
36 #define extra_checks 1
38 #define extra_checks 0
41 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
42 * While any GPIO is requested, its gpio_chip is not removable;
43 * each GPIO's "requested" flag serves as a lock and refcount.
45 static DEFINE_SPINLOCK(gpio_lock);
48 struct gpio_chip *chip;
50 /* flag symbols are bit numbers */
51 #define FLAG_REQUESTED 0
53 #define FLAG_RESERVED 2
54 #define FLAG_EXPORT 3 /* protected by sysfs_lock */
55 #define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */
56 #define FLAG_TRIG_FALL 5 /* trigger on falling edge */
57 #define FLAG_TRIG_RISE 6 /* trigger on rising edge */
58 #define FLAG_ACTIVE_LOW 7 /* sysfs value has active low */
60 #define PDESC_ID_SHIFT 16 /* add new flags before this one */
62 #define GPIO_FLAGS_MASK ((1 << PDESC_ID_SHIFT) - 1)
63 #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
65 #ifdef CONFIG_DEBUG_FS
69 static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
71 #ifdef CONFIG_GPIO_SYSFS
73 struct work_struct work;
74 struct sysfs_dirent *value_sd;
77 static struct idr pdesc_idr;
80 static inline void desc_set_label(struct gpio_desc *d, const char *label)
82 #ifdef CONFIG_DEBUG_FS
87 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
88 * when setting direction, and otherwise illegal. Until board setup code
89 * and drivers use explicit requests everywhere (which won't happen when
90 * those calls have no teeth) we can't avoid autorequesting. This nag
91 * message should motivate switching to explicit requests... so should
92 * the weaker cleanup after faults, compared to gpio_request().
94 * NOTE: the autorequest mechanism is going away; at this point it's
95 * only "legal" in the sense that (old) code using it won't break yet,
96 * but instead only triggers a WARN() stack dump.
98 static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
100 const struct gpio_chip *chip = desc->chip;
101 const int gpio = chip->base + offset;
103 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
104 "autorequest GPIO-%d\n", gpio)) {
105 if (!try_module_get(chip->owner)) {
106 pr_err("GPIO-%d: module can't be gotten \n", gpio);
107 clear_bit(FLAG_REQUESTED, &desc->flags);
111 desc_set_label(desc, "[auto]");
112 /* caller must chip->request() w/o spinlock */
119 /* caller holds gpio_lock *OR* gpio is marked as requested */
120 static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
122 return gpio_desc[gpio].chip;
125 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
126 static int gpiochip_find_base(int ngpio)
132 for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
133 struct gpio_desc *desc = &gpio_desc[i];
134 struct gpio_chip *chip = desc->chip;
136 if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) {
138 if (spare == ngpio) {
145 i -= chip->ngpio - 1;
149 if (gpio_is_valid(base))
150 pr_debug("%s: found new base at %d\n", __func__, base);
155 * gpiochip_reserve() - reserve range of gpios to use with platform code only
156 * @start: starting gpio number
157 * @ngpio: number of gpios to reserve
158 * Context: platform init, potentially before irqs or kmalloc will work
160 * Returns a negative errno if any gpio within the range is already reserved
161 * or registered, else returns zero as a success code. Use this function
162 * to mark a range of gpios as unavailable for dynamic gpio number allocation,
163 * for example because its driver support is not yet loaded.
165 int __init gpiochip_reserve(int start, int ngpio)
171 if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1))
174 spin_lock_irqsave(&gpio_lock, flags);
176 for (i = start; i < start + ngpio; i++) {
177 struct gpio_desc *desc = &gpio_desc[i];
179 if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) {
184 set_bit(FLAG_RESERVED, &desc->flags);
187 pr_debug("%s: reserved gpios from %d to %d\n",
188 __func__, start, start + ngpio - 1);
190 spin_unlock_irqrestore(&gpio_lock, flags);
195 #ifdef CONFIG_GPIO_SYSFS
197 /* lock protects against unexport_gpio() being called while
198 * sysfs files are active.
200 static DEFINE_MUTEX(sysfs_lock);
203 * /sys/class/gpio/gpioN... only for GPIOs that are exported
205 * * MAY BE OMITTED if kernel won't allow direction changes
206 * * is read/write as "in" or "out"
207 * * may also be written as "high" or "low", initializing
208 * output value as specified ("out" implies "low")
210 * * always readable, subject to hardware behavior
211 * * may be writable, as zero/nonzero
213 * * configures behavior of poll(2) on /value
214 * * available only if pin can generate IRQs on input
215 * * is read/write as "none", "falling", "rising", or "both"
217 * * configures polarity of /value
218 * * is read/write as zero/nonzero
219 * * also affects existing and subsequent "falling" and "rising"
220 * /edge configuration
223 static ssize_t gpio_direction_show(struct device *dev,
224 struct device_attribute *attr, char *buf)
226 const struct gpio_desc *desc = dev_get_drvdata(dev);
229 mutex_lock(&sysfs_lock);
231 if (!test_bit(FLAG_EXPORT, &desc->flags))
234 status = sprintf(buf, "%s\n",
235 test_bit(FLAG_IS_OUT, &desc->flags)
238 mutex_unlock(&sysfs_lock);
242 static ssize_t gpio_direction_store(struct device *dev,
243 struct device_attribute *attr, const char *buf, size_t size)
245 const struct gpio_desc *desc = dev_get_drvdata(dev);
246 unsigned gpio = desc - gpio_desc;
249 mutex_lock(&sysfs_lock);
251 if (!test_bit(FLAG_EXPORT, &desc->flags))
253 else if (sysfs_streq(buf, "high"))
254 status = gpio_direction_output(gpio, 1);
255 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
256 status = gpio_direction_output(gpio, 0);
257 else if (sysfs_streq(buf, "in"))
258 status = gpio_direction_input(gpio);
262 mutex_unlock(&sysfs_lock);
263 return status ? : size;
266 static /* const */ DEVICE_ATTR(direction, 0644,
267 gpio_direction_show, gpio_direction_store);
269 static ssize_t gpio_value_show(struct device *dev,
270 struct device_attribute *attr, char *buf)
272 const struct gpio_desc *desc = dev_get_drvdata(dev);
273 unsigned gpio = desc - gpio_desc;
276 mutex_lock(&sysfs_lock);
278 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
283 value = !!gpio_get_value_cansleep(gpio);
284 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
287 status = sprintf(buf, "%d\n", value);
290 mutex_unlock(&sysfs_lock);
294 static ssize_t gpio_value_store(struct device *dev,
295 struct device_attribute *attr, const char *buf, size_t size)
297 const struct gpio_desc *desc = dev_get_drvdata(dev);
298 unsigned gpio = desc - gpio_desc;
301 mutex_lock(&sysfs_lock);
303 if (!test_bit(FLAG_EXPORT, &desc->flags))
305 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
310 status = strict_strtol(buf, 0, &value);
312 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
314 gpio_set_value_cansleep(gpio, value != 0);
319 mutex_unlock(&sysfs_lock);
323 static const DEVICE_ATTR(value, 0644,
324 gpio_value_show, gpio_value_store);
326 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
328 struct work_struct *work = priv;
334 static void gpio_notify_sysfs(struct work_struct *work)
336 struct poll_desc *pdesc;
338 pdesc = container_of(work, struct poll_desc, work);
339 sysfs_notify_dirent(pdesc->value_sd);
342 static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
343 unsigned long gpio_flags)
345 struct poll_desc *pdesc;
346 unsigned long irq_flags;
349 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
352 irq = gpio_to_irq(desc - gpio_desc);
356 id = desc->flags >> PDESC_ID_SHIFT;
357 pdesc = idr_find(&pdesc_idr, id);
359 free_irq(irq, &pdesc->work);
360 cancel_work_sync(&pdesc->work);
363 desc->flags &= ~GPIO_TRIGGER_MASK;
370 irq_flags = IRQF_SHARED;
371 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
372 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
373 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
374 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
375 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
376 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
379 pdesc = kmalloc(sizeof(*pdesc), GFP_KERNEL);
387 if (idr_pre_get(&pdesc_idr, GFP_KERNEL))
388 ret = idr_get_new_above(&pdesc_idr,
390 } while (ret == -EAGAIN);
395 desc->flags &= GPIO_FLAGS_MASK;
396 desc->flags |= (unsigned long)id << PDESC_ID_SHIFT;
398 if (desc->flags >> PDESC_ID_SHIFT != id) {
403 pdesc->value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
404 if (!pdesc->value_sd) {
408 INIT_WORK(&pdesc->work, gpio_notify_sysfs);
411 ret = request_irq(irq, gpio_sysfs_irq, irq_flags,
412 "gpiolib", &pdesc->work);
416 desc->flags |= gpio_flags;
421 sysfs_put(pdesc->value_sd);
423 idr_remove(&pdesc_idr, id);
424 desc->flags &= GPIO_FLAGS_MASK;
431 static const struct {
434 } trigger_types[] = {
436 { "falling", BIT(FLAG_TRIG_FALL) },
437 { "rising", BIT(FLAG_TRIG_RISE) },
438 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
441 static ssize_t gpio_edge_show(struct device *dev,
442 struct device_attribute *attr, char *buf)
444 const struct gpio_desc *desc = dev_get_drvdata(dev);
447 mutex_lock(&sysfs_lock);
449 if (!test_bit(FLAG_EXPORT, &desc->flags))
455 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
456 if ((desc->flags & GPIO_TRIGGER_MASK)
457 == trigger_types[i].flags) {
458 status = sprintf(buf, "%s\n",
459 trigger_types[i].name);
464 mutex_unlock(&sysfs_lock);
468 static ssize_t gpio_edge_store(struct device *dev,
469 struct device_attribute *attr, const char *buf, size_t size)
471 struct gpio_desc *desc = dev_get_drvdata(dev);
475 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
476 if (sysfs_streq(trigger_types[i].name, buf))
481 mutex_lock(&sysfs_lock);
483 if (!test_bit(FLAG_EXPORT, &desc->flags))
486 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
491 mutex_unlock(&sysfs_lock);
496 static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
498 static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
503 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
507 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
509 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
511 /* reconfigure poll(2) support if enabled on one edge only */
512 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
513 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
514 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
516 gpio_setup_irq(desc, dev, 0);
517 status = gpio_setup_irq(desc, dev, trigger_flags);
523 static ssize_t gpio_active_low_show(struct device *dev,
524 struct device_attribute *attr, char *buf)
526 const struct gpio_desc *desc = dev_get_drvdata(dev);
529 mutex_lock(&sysfs_lock);
531 if (!test_bit(FLAG_EXPORT, &desc->flags))
534 status = sprintf(buf, "%d\n",
535 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
537 mutex_unlock(&sysfs_lock);
542 static ssize_t gpio_active_low_store(struct device *dev,
543 struct device_attribute *attr, const char *buf, size_t size)
545 struct gpio_desc *desc = dev_get_drvdata(dev);
548 mutex_lock(&sysfs_lock);
550 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
555 status = strict_strtol(buf, 0, &value);
557 status = sysfs_set_active_low(desc, dev, value != 0);
560 mutex_unlock(&sysfs_lock);
562 return status ? : size;
565 static const DEVICE_ATTR(active_low, 0644,
566 gpio_active_low_show, gpio_active_low_store);
568 static const struct attribute *gpio_attrs[] = {
569 &dev_attr_value.attr,
570 &dev_attr_active_low.attr,
574 static const struct attribute_group gpio_attr_group = {
575 .attrs = (struct attribute **) gpio_attrs,
579 * /sys/class/gpio/gpiochipN/
580 * /base ... matching gpio_chip.base (N)
581 * /label ... matching gpio_chip.label
582 * /ngpio ... matching gpio_chip.ngpio
585 static ssize_t chip_base_show(struct device *dev,
586 struct device_attribute *attr, char *buf)
588 const struct gpio_chip *chip = dev_get_drvdata(dev);
590 return sprintf(buf, "%d\n", chip->base);
592 static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
594 static ssize_t chip_label_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, "%s\n", chip->label ? : "");
601 static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
603 static ssize_t chip_ngpio_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, "%u\n", chip->ngpio);
610 static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
612 static const struct attribute *gpiochip_attrs[] = {
614 &dev_attr_label.attr,
615 &dev_attr_ngpio.attr,
619 static const struct attribute_group gpiochip_attr_group = {
620 .attrs = (struct attribute **) gpiochip_attrs,
624 * /sys/class/gpio/export ... write-only
625 * integer N ... number of GPIO to export (full access)
626 * /sys/class/gpio/unexport ... write-only
627 * integer N ... number of GPIO to unexport
629 static ssize_t export_store(struct class *class,
630 struct class_attribute *attr,
631 const char *buf, size_t len)
636 status = strict_strtol(buf, 0, &gpio);
640 /* No extra locking here; FLAG_SYSFS just signifies that the
641 * request and export were done by on behalf of userspace, so
642 * they may be undone on its behalf too.
645 status = gpio_request(gpio, "sysfs");
649 status = gpio_export(gpio, true);
653 set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
657 pr_debug("%s: status %d\n", __func__, status);
658 return status ? : len;
661 static ssize_t unexport_store(struct class *class,
662 struct class_attribute *attr,
663 const char *buf, size_t len)
668 status = strict_strtol(buf, 0, &gpio);
674 /* reject bogus commands (gpio_unexport ignores them) */
675 if (!gpio_is_valid(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.
682 if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
688 pr_debug("%s: status %d\n", __func__, status);
689 return status ? : len;
692 static struct class_attribute gpio_class_attrs[] = {
693 __ATTR(export, 0200, NULL, export_store),
694 __ATTR(unexport, 0200, NULL, unexport_store),
698 static struct class gpio_class = {
700 .owner = THIS_MODULE,
702 .class_attrs = gpio_class_attrs,
707 * gpio_export - export a GPIO through sysfs
708 * @gpio: gpio to make available, already requested
709 * @direction_may_change: true if userspace may change gpio direction
710 * Context: arch_initcall or later
712 * When drivers want to make a GPIO accessible to userspace after they
713 * have requested it -- perhaps while debugging, or as part of their
714 * public interface -- they may use this routine. If the GPIO can
715 * change direction (some can't) and the caller allows it, userspace
716 * will see "direction" sysfs attribute which may be used to change
717 * the gpio's direction. A "value" attribute will always be provided.
719 * Returns zero on success, else an error.
721 int gpio_export(unsigned gpio, bool direction_may_change)
724 struct gpio_desc *desc;
725 int status = -EINVAL;
726 const char *ioname = NULL;
728 /* can't export until sysfs is available ... */
730 pr_debug("%s: called too early!\n", __func__);
734 if (!gpio_is_valid(gpio))
737 mutex_lock(&sysfs_lock);
739 spin_lock_irqsave(&gpio_lock, flags);
740 desc = &gpio_desc[gpio];
741 if (test_bit(FLAG_REQUESTED, &desc->flags)
742 && !test_bit(FLAG_EXPORT, &desc->flags)) {
744 if (!desc->chip->direction_input
745 || !desc->chip->direction_output)
746 direction_may_change = false;
748 spin_unlock_irqrestore(&gpio_lock, flags);
750 if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
751 ioname = desc->chip->names[gpio - desc->chip->base];
756 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
757 desc, ioname ? ioname : "gpio%u", gpio);
759 status = sysfs_create_group(&dev->kobj,
762 if (!status && direction_may_change)
763 status = device_create_file(dev,
764 &dev_attr_direction);
766 if (!status && gpio_to_irq(gpio) >= 0
767 && (direction_may_change
768 || !test_bit(FLAG_IS_OUT,
770 status = device_create_file(dev,
774 device_unregister(dev);
776 status = PTR_ERR(dev);
778 set_bit(FLAG_EXPORT, &desc->flags);
781 mutex_unlock(&sysfs_lock);
785 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
789 EXPORT_SYMBOL_GPL(gpio_export);
791 static int match_export(struct device *dev, void *data)
793 return dev_get_drvdata(dev) == data;
797 * gpio_export_link - create a sysfs link to an exported GPIO node
798 * @dev: device under which to create symlink
799 * @name: name of the symlink
800 * @gpio: gpio to create symlink to, already exported
802 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
803 * node. Caller is responsible for unlinking.
805 * Returns zero on success, else an error.
807 int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
809 struct gpio_desc *desc;
810 int status = -EINVAL;
812 if (!gpio_is_valid(gpio))
815 mutex_lock(&sysfs_lock);
817 desc = &gpio_desc[gpio];
819 if (test_bit(FLAG_EXPORT, &desc->flags)) {
822 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
824 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
831 mutex_unlock(&sysfs_lock);
835 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
839 EXPORT_SYMBOL_GPL(gpio_export_link);
843 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
844 * @gpio: gpio to change
845 * @value: non-zero to use active low, i.e. inverted values
847 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
848 * The GPIO does not have to be exported yet. If poll(2) support has
849 * been enabled for either rising or falling edge, it will be
850 * reconfigured to follow the new polarity.
852 * Returns zero on success, else an error.
854 int gpio_sysfs_set_active_low(unsigned gpio, int value)
856 struct gpio_desc *desc;
857 struct device *dev = NULL;
858 int status = -EINVAL;
860 if (!gpio_is_valid(gpio))
863 mutex_lock(&sysfs_lock);
865 desc = &gpio_desc[gpio];
867 if (test_bit(FLAG_EXPORT, &desc->flags)) {
868 dev = class_find_device(&gpio_class, NULL, desc, match_export);
875 status = sysfs_set_active_low(desc, dev, value);
878 mutex_unlock(&sysfs_lock);
882 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
886 EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
889 * gpio_unexport - reverse effect of gpio_export()
890 * @gpio: gpio to make unavailable
892 * This is implicit on gpio_free().
894 void gpio_unexport(unsigned gpio)
896 struct gpio_desc *desc;
899 if (!gpio_is_valid(gpio)) {
904 mutex_lock(&sysfs_lock);
906 desc = &gpio_desc[gpio];
908 if (test_bit(FLAG_EXPORT, &desc->flags)) {
909 struct device *dev = NULL;
911 dev = class_find_device(&gpio_class, NULL, desc, match_export);
913 gpio_setup_irq(desc, dev, 0);
914 clear_bit(FLAG_EXPORT, &desc->flags);
916 device_unregister(dev);
921 mutex_unlock(&sysfs_lock);
924 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
926 EXPORT_SYMBOL_GPL(gpio_unexport);
928 static int gpiochip_export(struct gpio_chip *chip)
933 /* Many systems register gpio chips for SOC support very early,
934 * before driver model support is available. In those cases we
935 * export this later, in gpiolib_sysfs_init() ... here we just
936 * verify that _some_ field of gpio_class got initialized.
941 /* use chip->base for the ID; it's already known to be unique */
942 mutex_lock(&sysfs_lock);
943 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
944 "gpiochip%d", chip->base);
946 status = sysfs_create_group(&dev->kobj,
947 &gpiochip_attr_group);
949 status = PTR_ERR(dev);
950 chip->exported = (status == 0);
951 mutex_unlock(&sysfs_lock);
957 spin_lock_irqsave(&gpio_lock, flags);
959 while (gpio_desc[gpio].chip == chip)
960 gpio_desc[gpio++].chip = NULL;
961 spin_unlock_irqrestore(&gpio_lock, flags);
963 pr_debug("%s: chip %s status %d\n", __func__,
964 chip->label, status);
970 static void gpiochip_unexport(struct gpio_chip *chip)
975 mutex_lock(&sysfs_lock);
976 dev = class_find_device(&gpio_class, NULL, chip, match_export);
979 device_unregister(dev);
984 mutex_unlock(&sysfs_lock);
987 pr_debug("%s: chip %s status %d\n", __func__,
988 chip->label, status);
991 static int __init gpiolib_sysfs_init(void)
997 idr_init(&pdesc_idr);
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)
1104 of_gpiochip_add(chip);
1107 spin_unlock_irqrestore(&gpio_lock, flags);
1112 status = gpiochip_export(chip);
1118 /* failures here can mean systems won't boot... */
1119 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1120 chip->base, chip->base + chip->ngpio - 1,
1121 chip->label ? : "generic");
1124 EXPORT_SYMBOL_GPL(gpiochip_add);
1127 * gpiochip_remove() - unregister a gpio_chip
1128 * @chip: the chip to unregister
1130 * A gpio_chip with any GPIOs still requested may not be removed.
1132 int gpiochip_remove(struct gpio_chip *chip)
1134 unsigned long flags;
1138 spin_lock_irqsave(&gpio_lock, flags);
1140 of_gpiochip_remove(chip);
1142 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
1143 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
1149 for (id = chip->base; id < chip->base + chip->ngpio; id++)
1150 gpio_desc[id].chip = NULL;
1153 spin_unlock_irqrestore(&gpio_lock, flags);
1156 gpiochip_unexport(chip);
1160 EXPORT_SYMBOL_GPL(gpiochip_remove);
1163 * gpiochip_find() - iterator for locating a specific gpio_chip
1164 * @data: data to pass to match function
1165 * @callback: Callback function to check gpio_chip
1167 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1168 * determined by a user supplied @match callback. The callback should return
1169 * 0 if the device doesn't match and non-zero if it does. If the callback is
1170 * non-zero, this function will return to the caller and not iterate over any
1173 struct gpio_chip *gpiochip_find(void *data,
1174 int (*match)(struct gpio_chip *chip, void *data))
1176 struct gpio_chip *chip = NULL;
1177 unsigned long flags;
1180 spin_lock_irqsave(&gpio_lock, flags);
1181 for (i = 0; i < ARCH_NR_GPIOS; i++) {
1182 if (!gpio_desc[i].chip)
1185 if (match(gpio_desc[i].chip, data)) {
1186 chip = gpio_desc[i].chip;
1190 spin_unlock_irqrestore(&gpio_lock, flags);
1195 /* These "optional" allocation calls help prevent drivers from stomping
1196 * on each other, and help provide better diagnostics in debugfs.
1197 * They're called even less than the "set direction" calls.
1199 int gpio_request(unsigned gpio, const char *label)
1201 struct gpio_desc *desc;
1202 struct gpio_chip *chip;
1203 int status = -EINVAL;
1204 unsigned long flags;
1206 spin_lock_irqsave(&gpio_lock, flags);
1208 if (!gpio_is_valid(gpio))
1210 desc = &gpio_desc[gpio];
1215 if (!try_module_get(chip->owner))
1218 /* NOTE: gpio_request() can be called in early boot,
1219 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1222 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1223 desc_set_label(desc, label ? : "?");
1227 module_put(chip->owner);
1231 if (chip->request) {
1232 /* chip->request may sleep */
1233 spin_unlock_irqrestore(&gpio_lock, flags);
1234 status = chip->request(chip, gpio - chip->base);
1235 spin_lock_irqsave(&gpio_lock, flags);
1238 desc_set_label(desc, NULL);
1239 module_put(chip->owner);
1240 clear_bit(FLAG_REQUESTED, &desc->flags);
1246 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
1247 gpio, label ? : "?", status);
1248 spin_unlock_irqrestore(&gpio_lock, flags);
1251 EXPORT_SYMBOL_GPL(gpio_request);
1253 void gpio_free(unsigned gpio)
1255 unsigned long flags;
1256 struct gpio_desc *desc;
1257 struct gpio_chip *chip;
1261 if (!gpio_is_valid(gpio)) {
1262 WARN_ON(extra_checks);
1266 gpio_unexport(gpio);
1268 spin_lock_irqsave(&gpio_lock, flags);
1270 desc = &gpio_desc[gpio];
1272 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1274 spin_unlock_irqrestore(&gpio_lock, flags);
1275 might_sleep_if(extra_checks && chip->can_sleep);
1276 chip->free(chip, gpio - chip->base);
1277 spin_lock_irqsave(&gpio_lock, flags);
1279 desc_set_label(desc, NULL);
1280 module_put(desc->chip->owner);
1281 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1282 clear_bit(FLAG_REQUESTED, &desc->flags);
1284 WARN_ON(extra_checks);
1286 spin_unlock_irqrestore(&gpio_lock, flags);
1288 EXPORT_SYMBOL_GPL(gpio_free);
1291 * gpio_request_one - request a single GPIO with initial configuration
1292 * @gpio: the GPIO number
1293 * @flags: GPIO configuration as specified by GPIOF_*
1294 * @label: a literal description string of this GPIO
1296 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1300 err = gpio_request(gpio, label);
1304 if (flags & GPIOF_DIR_IN)
1305 err = gpio_direction_input(gpio);
1307 err = gpio_direction_output(gpio,
1308 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1312 EXPORT_SYMBOL_GPL(gpio_request_one);
1315 * gpio_request_array - request multiple GPIOs in a single call
1316 * @array: array of the 'struct gpio'
1317 * @num: how many GPIOs in the array
1319 int gpio_request_array(struct gpio *array, size_t num)
1323 for (i = 0; i < num; i++, array++) {
1324 err = gpio_request_one(array->gpio, array->flags, array->label);
1332 gpio_free((--array)->gpio);
1335 EXPORT_SYMBOL_GPL(gpio_request_array);
1338 * gpio_free_array - release multiple GPIOs in a single call
1339 * @array: array of the 'struct gpio'
1340 * @num: how many GPIOs in the array
1342 void gpio_free_array(struct gpio *array, size_t num)
1345 gpio_free((array++)->gpio);
1347 EXPORT_SYMBOL_GPL(gpio_free_array);
1350 * gpiochip_is_requested - return string iff signal was requested
1351 * @chip: controller managing the signal
1352 * @offset: of signal within controller's 0..(ngpio - 1) range
1354 * Returns NULL if the GPIO is not currently requested, else a string.
1355 * If debugfs support is enabled, the string returned is the label passed
1356 * to gpio_request(); otherwise it is a meaningless constant.
1358 * This function is for use by GPIO controller drivers. The label can
1359 * help with diagnostics, and knowing that the signal is used as a GPIO
1360 * can help avoid accidentally multiplexing it to another controller.
1362 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1364 unsigned gpio = chip->base + offset;
1366 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
1368 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
1370 #ifdef CONFIG_DEBUG_FS
1371 return gpio_desc[gpio].label;
1376 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1379 /* Drivers MUST set GPIO direction before making get/set calls. In
1380 * some cases this is done in early boot, before IRQs are enabled.
1382 * As a rule these aren't called more than once (except for drivers
1383 * using the open-drain emulation idiom) so these are natural places
1384 * to accumulate extra debugging checks. Note that we can't (yet)
1385 * rely on gpio_request() having been called beforehand.
1388 int gpio_direction_input(unsigned gpio)
1390 unsigned long flags;
1391 struct gpio_chip *chip;
1392 struct gpio_desc *desc = &gpio_desc[gpio];
1393 int status = -EINVAL;
1395 spin_lock_irqsave(&gpio_lock, flags);
1397 if (!gpio_is_valid(gpio))
1400 if (!chip || !chip->get || !chip->direction_input)
1403 if (gpio >= chip->ngpio)
1405 status = gpio_ensure_requested(desc, gpio);
1409 /* now we know the gpio is valid and chip won't vanish */
1411 spin_unlock_irqrestore(&gpio_lock, flags);
1413 might_sleep_if(extra_checks && chip->can_sleep);
1416 status = chip->request(chip, gpio);
1418 pr_debug("GPIO-%d: chip request fail, %d\n",
1419 chip->base + gpio, status);
1420 /* and it's not available to anyone else ...
1421 * gpio_request() is the fully clean solution.
1427 status = chip->direction_input(chip, gpio);
1429 clear_bit(FLAG_IS_OUT, &desc->flags);
1433 spin_unlock_irqrestore(&gpio_lock, flags);
1435 pr_debug("%s: gpio-%d status %d\n",
1436 __func__, gpio, status);
1439 EXPORT_SYMBOL_GPL(gpio_direction_input);
1441 int gpio_direction_output(unsigned gpio, int value)
1443 unsigned long flags;
1444 struct gpio_chip *chip;
1445 struct gpio_desc *desc = &gpio_desc[gpio];
1446 int status = -EINVAL;
1448 spin_lock_irqsave(&gpio_lock, flags);
1450 if (!gpio_is_valid(gpio))
1453 if (!chip || !chip->set || !chip->direction_output)
1456 if (gpio >= chip->ngpio)
1458 status = gpio_ensure_requested(desc, gpio);
1462 /* now we know the gpio is valid and chip won't vanish */
1464 spin_unlock_irqrestore(&gpio_lock, flags);
1466 might_sleep_if(extra_checks && chip->can_sleep);
1469 status = chip->request(chip, gpio);
1471 pr_debug("GPIO-%d: chip request fail, %d\n",
1472 chip->base + gpio, status);
1473 /* and it's not available to anyone else ...
1474 * gpio_request() is the fully clean solution.
1480 status = chip->direction_output(chip, gpio, value);
1482 set_bit(FLAG_IS_OUT, &desc->flags);
1486 spin_unlock_irqrestore(&gpio_lock, flags);
1488 pr_debug("%s: gpio-%d status %d\n",
1489 __func__, gpio, status);
1492 EXPORT_SYMBOL_GPL(gpio_direction_output);
1495 * gpio_set_debounce - sets @debounce time for a @gpio
1496 * @gpio: the gpio to set debounce time
1497 * @debounce: debounce time is microseconds
1499 int gpio_set_debounce(unsigned gpio, unsigned debounce)
1501 unsigned long flags;
1502 struct gpio_chip *chip;
1503 struct gpio_desc *desc = &gpio_desc[gpio];
1504 int status = -EINVAL;
1506 spin_lock_irqsave(&gpio_lock, flags);
1508 if (!gpio_is_valid(gpio))
1511 if (!chip || !chip->set || !chip->set_debounce)
1514 if (gpio >= chip->ngpio)
1516 status = gpio_ensure_requested(desc, gpio);
1520 /* now we know the gpio is valid and chip won't vanish */
1522 spin_unlock_irqrestore(&gpio_lock, flags);
1524 might_sleep_if(extra_checks && chip->can_sleep);
1526 return chip->set_debounce(chip, gpio, debounce);
1529 spin_unlock_irqrestore(&gpio_lock, flags);
1531 pr_debug("%s: gpio-%d status %d\n",
1532 __func__, gpio, status);
1536 EXPORT_SYMBOL_GPL(gpio_set_debounce);
1538 /* I/O calls are only valid after configuration completed; the relevant
1539 * "is this a valid GPIO" error checks should already have been done.
1541 * "Get" operations are often inlinable as reading a pin value register,
1542 * and masking the relevant bit in that register.
1544 * When "set" operations are inlinable, they involve writing that mask to
1545 * one register to set a low value, or a different register to set it high.
1546 * Otherwise locking is needed, so there may be little value to inlining.
1548 *------------------------------------------------------------------------
1550 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1551 * have requested the GPIO. That can include implicit requesting by
1552 * a direction setting call. Marking a gpio as requested locks its chip
1553 * in memory, guaranteeing that these table lookups need no more locking
1554 * and that gpiochip_remove() will fail.
1556 * REVISIT when debugging, consider adding some instrumentation to ensure
1557 * that the GPIO was actually requested.
1561 * __gpio_get_value() - return a gpio's value
1562 * @gpio: gpio whose value will be returned
1565 * This is used directly or indirectly to implement gpio_get_value().
1566 * It returns the zero or nonzero value provided by the associated
1567 * gpio_chip.get() method; or zero if no such method is provided.
1569 int __gpio_get_value(unsigned gpio)
1571 struct gpio_chip *chip;
1573 chip = gpio_to_chip(gpio);
1574 WARN_ON(extra_checks && chip->can_sleep);
1575 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1577 EXPORT_SYMBOL_GPL(__gpio_get_value);
1580 * __gpio_set_value() - assign a gpio's value
1581 * @gpio: gpio whose value will be assigned
1582 * @value: value to assign
1585 * This is used directly or indirectly to implement gpio_set_value().
1586 * It invokes the associated gpio_chip.set() method.
1588 void __gpio_set_value(unsigned gpio, int value)
1590 struct gpio_chip *chip;
1592 chip = gpio_to_chip(gpio);
1593 WARN_ON(extra_checks && chip->can_sleep);
1594 chip->set(chip, gpio - chip->base, value);
1596 EXPORT_SYMBOL_GPL(__gpio_set_value);
1599 * __gpio_cansleep() - report whether gpio value access will sleep
1600 * @gpio: gpio in question
1603 * This is used directly or indirectly to implement gpio_cansleep(). It
1604 * returns nonzero if access reading or writing the GPIO value can sleep.
1606 int __gpio_cansleep(unsigned gpio)
1608 struct gpio_chip *chip;
1610 /* only call this on GPIOs that are valid! */
1611 chip = gpio_to_chip(gpio);
1613 return chip->can_sleep;
1615 EXPORT_SYMBOL_GPL(__gpio_cansleep);
1618 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1619 * @gpio: gpio whose IRQ will be returned (already requested)
1622 * This is used directly or indirectly to implement gpio_to_irq().
1623 * It returns the number of the IRQ signaled by this (input) GPIO,
1624 * or a negative errno.
1626 int __gpio_to_irq(unsigned gpio)
1628 struct gpio_chip *chip;
1630 chip = gpio_to_chip(gpio);
1631 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1633 EXPORT_SYMBOL_GPL(__gpio_to_irq);
1637 /* There's no value in making it easy to inline GPIO calls that may sleep.
1638 * Common examples include ones connected to I2C or SPI chips.
1641 int gpio_get_value_cansleep(unsigned gpio)
1643 struct gpio_chip *chip;
1645 might_sleep_if(extra_checks);
1646 chip = gpio_to_chip(gpio);
1647 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1649 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1651 void gpio_set_value_cansleep(unsigned gpio, int value)
1653 struct gpio_chip *chip;
1655 might_sleep_if(extra_checks);
1656 chip = gpio_to_chip(gpio);
1657 chip->set(chip, gpio - chip->base, value);
1659 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1662 #ifdef CONFIG_DEBUG_FS
1664 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1667 unsigned gpio = chip->base;
1668 struct gpio_desc *gdesc = &gpio_desc[gpio];
1671 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1672 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1675 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
1676 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
1678 is_out ? "out" : "in ",
1680 ? (chip->get(chip, i) ? "hi" : "lo")
1684 int irq = gpio_to_irq(gpio);
1685 struct irq_desc *desc = irq_to_desc(irq);
1687 /* This races with request_irq(), set_irq_type(),
1688 * and set_irq_wake() ... but those are "rare".
1690 * More significantly, trigger type flags aren't
1691 * currently maintained by genirq.
1693 if (irq >= 0 && desc->action) {
1696 switch (desc->status & IRQ_TYPE_SENSE_MASK) {
1698 trigger = "(default)";
1700 case IRQ_TYPE_EDGE_FALLING:
1701 trigger = "edge-falling";
1703 case IRQ_TYPE_EDGE_RISING:
1704 trigger = "edge-rising";
1706 case IRQ_TYPE_EDGE_BOTH:
1707 trigger = "edge-both";
1709 case IRQ_TYPE_LEVEL_HIGH:
1710 trigger = "level-high";
1712 case IRQ_TYPE_LEVEL_LOW:
1713 trigger = "level-low";
1716 trigger = "?trigger?";
1720 seq_printf(s, " irq-%d %s%s",
1722 (desc->status & IRQ_WAKEUP)
1727 seq_printf(s, "\n");
1731 static int gpiolib_show(struct seq_file *s, void *unused)
1733 struct gpio_chip *chip = NULL;
1737 /* REVISIT this isn't locked against gpio_chip removal ... */
1739 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
1742 if (chip == gpio_desc[gpio].chip)
1744 chip = gpio_desc[gpio].chip;
1748 seq_printf(s, "%sGPIOs %d-%d",
1749 started ? "\n" : "",
1750 chip->base, chip->base + chip->ngpio - 1);
1753 seq_printf(s, ", %s/%s",
1754 dev->bus ? dev->bus->name : "no-bus",
1757 seq_printf(s, ", %s", chip->label);
1758 if (chip->can_sleep)
1759 seq_printf(s, ", can sleep");
1760 seq_printf(s, ":\n");
1764 chip->dbg_show(s, chip);
1766 gpiolib_dbg_show(s, chip);
1771 static int gpiolib_open(struct inode *inode, struct file *file)
1773 return single_open(file, gpiolib_show, NULL);
1776 static const struct file_operations gpiolib_operations = {
1777 .open = gpiolib_open,
1779 .llseek = seq_lseek,
1780 .release = single_release,
1783 static int __init gpiolib_debugfs_init(void)
1785 /* /sys/kernel/debug/gpio */
1786 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1787 NULL, NULL, &gpiolib_operations);
1790 subsys_initcall(gpiolib_debugfs_init);
1792 #endif /* DEBUG_FS */