x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / gpio / gpiolib.c
blob0dee0e0c247ae5fa2f121df234d09979dc00df74
1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/interrupt.h>
4 #include <linux/irq.h>
5 #include <linux/spinlock.h>
6 #include <linux/list.h>
7 #include <linux/device.h>
8 #include <linux/err.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.
38 #ifdef DEBUG
39 #define extra_checks 1
40 #else
41 #define extra_checks 0
42 #endif
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);
50 struct gpio_desc {
51 struct gpio_chip *chip;
52 unsigned long flags;
53 /* flag symbols are bit numbers */
54 #define FLAG_REQUESTED 0
55 #define FLAG_IS_OUT 1
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
70 const char *label;
71 #endif
73 static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
75 #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
77 static LIST_HEAD(gpio_chips);
79 #ifdef CONFIG_GPIO_SYSFS
80 static DEFINE_IDR(dirent_idr);
81 #endif
84 * Internal gpiod_* API using descriptors instead of the integer namespace.
85 * Most of this should eventually go public.
87 static int gpiod_request(struct gpio_desc *desc, const char *label);
88 static void gpiod_free(struct gpio_desc *desc);
89 static int gpiod_direction_input(struct gpio_desc *desc);
90 static int gpiod_direction_output(struct gpio_desc *desc, int value);
91 static int gpiod_get_direction(const struct gpio_desc *desc);
92 static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
93 static int gpiod_get_value_cansleep(const struct gpio_desc *desc);
94 static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
95 static int gpiod_get_value(const struct gpio_desc *desc);
96 static void gpiod_set_value(struct gpio_desc *desc, int value);
97 static int gpiod_cansleep(const struct gpio_desc *desc);
98 static int gpiod_to_irq(const struct gpio_desc *desc);
99 static int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
100 static int gpiod_export_link(struct device *dev, const char *name,
101 struct gpio_desc *desc);
102 static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
103 static void gpiod_unexport(struct gpio_desc *desc);
106 static inline void desc_set_label(struct gpio_desc *d, const char *label)
108 #ifdef CONFIG_DEBUG_FS
109 d->label = label;
110 #endif
114 * Return the GPIO number of the passed descriptor relative to its chip
116 static int gpio_chip_hwgpio(const struct gpio_desc *desc)
118 return desc - &desc->chip->desc[0];
122 * Convert a GPIO number to its descriptor
124 static struct gpio_desc *gpio_to_desc(unsigned gpio)
126 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
127 return NULL;
128 else
129 return &gpio_desc[gpio];
133 * Convert a GPIO descriptor to the integer namespace.
134 * This should disappear in the future but is needed since we still
135 * use GPIO numbers for error messages and sysfs nodes
137 static int desc_to_gpio(const struct gpio_desc *desc)
139 return desc - &gpio_desc[0];
143 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
144 * when setting direction, and otherwise illegal. Until board setup code
145 * and drivers use explicit requests everywhere (which won't happen when
146 * those calls have no teeth) we can't avoid autorequesting. This nag
147 * message should motivate switching to explicit requests... so should
148 * the weaker cleanup after faults, compared to gpio_request().
150 * NOTE: the autorequest mechanism is going away; at this point it's
151 * only "legal" in the sense that (old) code using it won't break yet,
152 * but instead only triggers a WARN() stack dump.
154 static int gpio_ensure_requested(struct gpio_desc *desc)
156 const struct gpio_chip *chip = desc->chip;
157 const int gpio = desc_to_gpio(desc);
159 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
160 "autorequest GPIO-%d\n", gpio)) {
161 if (!try_module_get(chip->owner)) {
162 pr_err("GPIO-%d: module can't be gotten \n", gpio);
163 clear_bit(FLAG_REQUESTED, &desc->flags);
164 /* lose */
165 return -EIO;
167 desc_set_label(desc, "[auto]");
168 /* caller must chip->request() w/o spinlock */
169 if (chip->request)
170 return 1;
172 return 0;
175 static struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
177 return desc ? desc->chip : NULL;
180 /* caller holds gpio_lock *OR* gpio is marked as requested */
181 struct gpio_chip *gpio_to_chip(unsigned gpio)
183 return gpiod_to_chip(gpio_to_desc(gpio));
186 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
187 static int gpiochip_find_base(int ngpio)
189 struct gpio_chip *chip;
190 int base = ARCH_NR_GPIOS - ngpio;
192 list_for_each_entry_reverse(chip, &gpio_chips, list) {
193 /* found a free space? */
194 if (chip->base + chip->ngpio <= base)
195 break;
196 else
197 /* nope, check the space right before the chip */
198 base = chip->base - ngpio;
201 if (gpio_is_valid(base)) {
202 pr_debug("%s: found new base at %d\n", __func__, base);
203 return base;
204 } else {
205 pr_err("%s: cannot find free range\n", __func__);
206 return -ENOSPC;
210 /* caller ensures gpio is valid and requested, chip->get_direction may sleep */
211 static int gpiod_get_direction(const struct gpio_desc *desc)
213 struct gpio_chip *chip;
214 unsigned offset;
215 int status = -EINVAL;
217 chip = gpiod_to_chip(desc);
218 offset = gpio_chip_hwgpio(desc);
220 if (!chip->get_direction)
221 return status;
223 status = chip->get_direction(chip, offset);
224 if (status > 0) {
225 /* GPIOF_DIR_IN, or other positive */
226 status = 1;
227 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
228 * so it does not affect constness per se */
229 clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
231 if (status == 0) {
232 /* GPIOF_DIR_OUT */
233 set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
235 return status;
238 #ifdef CONFIG_GPIO_SYSFS
240 /* lock protects against unexport_gpio() being called while
241 * sysfs files are active.
243 static DEFINE_MUTEX(sysfs_lock);
246 * /sys/class/gpio/gpioN... only for GPIOs that are exported
247 * /direction
248 * * MAY BE OMITTED if kernel won't allow direction changes
249 * * is read/write as "in" or "out"
250 * * may also be written as "high" or "low", initializing
251 * output value as specified ("out" implies "low")
252 * /value
253 * * always readable, subject to hardware behavior
254 * * may be writable, as zero/nonzero
255 * /edge
256 * * configures behavior of poll(2) on /value
257 * * available only if pin can generate IRQs on input
258 * * is read/write as "none", "falling", "rising", or "both"
259 * /active_low
260 * * configures polarity of /value
261 * * is read/write as zero/nonzero
262 * * also affects existing and subsequent "falling" and "rising"
263 * /edge configuration
266 static ssize_t gpio_direction_show(struct device *dev,
267 struct device_attribute *attr, char *buf)
269 const struct gpio_desc *desc = dev_get_drvdata(dev);
270 ssize_t status;
272 mutex_lock(&sysfs_lock);
274 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
275 status = -EIO;
276 } else {
277 gpiod_get_direction(desc);
278 status = sprintf(buf, "%s\n",
279 test_bit(FLAG_IS_OUT, &desc->flags)
280 ? "out" : "in");
283 mutex_unlock(&sysfs_lock);
284 return status;
287 static ssize_t gpio_direction_store(struct device *dev,
288 struct device_attribute *attr, const char *buf, size_t size)
290 struct gpio_desc *desc = dev_get_drvdata(dev);
291 ssize_t status;
293 mutex_lock(&sysfs_lock);
295 if (!test_bit(FLAG_EXPORT, &desc->flags))
296 status = -EIO;
297 else if (sysfs_streq(buf, "high"))
298 status = gpiod_direction_output(desc, 1);
299 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
300 status = gpiod_direction_output(desc, 0);
301 else if (sysfs_streq(buf, "in"))
302 status = gpiod_direction_input(desc);
303 else
304 status = -EINVAL;
306 mutex_unlock(&sysfs_lock);
307 return status ? : size;
310 static /* const */ DEVICE_ATTR(direction, 0644,
311 gpio_direction_show, gpio_direction_store);
313 static ssize_t gpio_value_show(struct device *dev,
314 struct device_attribute *attr, char *buf)
316 struct gpio_desc *desc = dev_get_drvdata(dev);
317 ssize_t status;
319 mutex_lock(&sysfs_lock);
321 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
322 status = -EIO;
323 } else {
324 int value;
326 value = !!gpiod_get_value_cansleep(desc);
327 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
328 value = !value;
330 status = sprintf(buf, "%d\n", value);
333 mutex_unlock(&sysfs_lock);
334 return status;
337 static ssize_t gpio_value_store(struct device *dev,
338 struct device_attribute *attr, const char *buf, size_t size)
340 struct gpio_desc *desc = dev_get_drvdata(dev);
341 ssize_t status;
343 mutex_lock(&sysfs_lock);
345 if (!test_bit(FLAG_EXPORT, &desc->flags))
346 status = -EIO;
347 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
348 status = -EPERM;
349 else {
350 long value;
352 status = kstrtol(buf, 0, &value);
353 if (status == 0) {
354 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
355 value = !value;
356 gpiod_set_value_cansleep(desc, value != 0);
357 status = size;
361 mutex_unlock(&sysfs_lock);
362 return status;
365 static const DEVICE_ATTR(value, 0644,
366 gpio_value_show, gpio_value_store);
368 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
370 struct sysfs_dirent *value_sd = priv;
372 sysfs_notify_dirent(value_sd);
373 return IRQ_HANDLED;
376 static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
377 unsigned long gpio_flags)
379 struct sysfs_dirent *value_sd;
380 unsigned long irq_flags;
381 int ret, irq, id;
383 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
384 return 0;
386 irq = gpiod_to_irq(desc);
387 if (irq < 0)
388 return -EIO;
390 id = desc->flags >> ID_SHIFT;
391 value_sd = idr_find(&dirent_idr, id);
392 if (value_sd)
393 free_irq(irq, value_sd);
395 desc->flags &= ~GPIO_TRIGGER_MASK;
397 if (!gpio_flags) {
398 ret = 0;
399 goto free_id;
402 irq_flags = IRQF_SHARED;
403 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
404 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
405 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
406 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
407 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
408 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
410 if (!value_sd) {
411 value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
412 if (!value_sd) {
413 ret = -ENODEV;
414 goto err_out;
417 ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
418 if (ret < 0)
419 goto free_sd;
420 id = ret;
422 desc->flags &= GPIO_FLAGS_MASK;
423 desc->flags |= (unsigned long)id << ID_SHIFT;
425 if (desc->flags >> ID_SHIFT != id) {
426 ret = -ERANGE;
427 goto free_id;
431 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
432 "gpiolib", value_sd);
433 if (ret < 0)
434 goto free_id;
436 desc->flags |= gpio_flags;
437 return 0;
439 free_id:
440 idr_remove(&dirent_idr, id);
441 desc->flags &= GPIO_FLAGS_MASK;
442 free_sd:
443 if (value_sd)
444 sysfs_put(value_sd);
445 err_out:
446 return ret;
449 static const struct {
450 const char *name;
451 unsigned long flags;
452 } trigger_types[] = {
453 { "none", 0 },
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);
463 ssize_t status;
465 mutex_lock(&sysfs_lock);
467 if (!test_bit(FLAG_EXPORT, &desc->flags))
468 status = -EIO;
469 else {
470 int i;
472 status = 0;
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);
478 break;
482 mutex_unlock(&sysfs_lock);
483 return status;
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);
490 ssize_t status;
491 int i;
493 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
494 if (sysfs_streq(trigger_types[i].name, buf))
495 goto found;
496 return -EINVAL;
498 found:
499 mutex_lock(&sysfs_lock);
501 if (!test_bit(FLAG_EXPORT, &desc->flags))
502 status = -EIO;
503 else {
504 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
505 if (!status)
506 status = size;
509 mutex_unlock(&sysfs_lock);
511 return status;
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,
517 int value)
519 int status = 0;
521 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
522 return 0;
524 if (value)
525 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
526 else
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);
538 return status;
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);
545 ssize_t status;
547 mutex_lock(&sysfs_lock);
549 if (!test_bit(FLAG_EXPORT, &desc->flags))
550 status = -EIO;
551 else
552 status = sprintf(buf, "%d\n",
553 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
555 mutex_unlock(&sysfs_lock);
557 return status;
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);
564 ssize_t status;
566 mutex_lock(&sysfs_lock);
568 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
569 status = -EIO;
570 } else {
571 long value;
573 status = kstrtol(buf, 0, &value);
574 if (status == 0)
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,
589 NULL,
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[] = {
631 &dev_attr_base.attr,
632 &dev_attr_label.attr,
633 &dev_attr_ngpio.attr,
634 NULL,
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)
651 long gpio;
652 struct gpio_desc *desc;
653 int status;
655 status = kstrtol(buf, 0, &gpio);
656 if (status < 0)
657 goto done;
659 desc = gpio_to_desc(gpio);
660 /* reject invalid GPIOs */
661 if (!desc) {
662 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
663 return -EINVAL;
666 /* No extra locking here; FLAG_SYSFS just signifies that the
667 * request and export were done by on behalf of userspace, so
668 * they may be undone on its behalf too.
671 status = gpiod_request(desc, "sysfs");
672 if (status < 0) {
673 if (status == -EPROBE_DEFER)
674 status = -ENODEV;
675 goto done;
677 status = gpiod_export(desc, true);
678 if (status < 0)
679 gpiod_free(desc);
680 else
681 set_bit(FLAG_SYSFS, &desc->flags);
683 done:
684 if (status)
685 pr_debug("%s: status %d\n", __func__, status);
686 return status ? : len;
689 static ssize_t unexport_store(struct class *class,
690 struct class_attribute *attr,
691 const char *buf, size_t len)
693 long gpio;
694 struct gpio_desc *desc;
695 int status;
697 status = kstrtol(buf, 0, &gpio);
698 if (status < 0)
699 goto done;
701 desc = gpio_to_desc(gpio);
702 /* reject bogus commands (gpio_unexport ignores them) */
703 if (!desc) {
704 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
705 return -EINVAL;
708 status = -EINVAL;
710 /* No extra locking here; FLAG_SYSFS just signifies that the
711 * request and export were done by on behalf of userspace, so
712 * they may be undone on its behalf too.
714 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
715 status = 0;
716 gpiod_free(desc);
718 done:
719 if (status)
720 pr_debug("%s: status %d\n", __func__, status);
721 return status ? : len;
724 static struct class_attribute gpio_class_attrs[] = {
725 __ATTR(export, 0200, NULL, export_store),
726 __ATTR(unexport, 0200, NULL, unexport_store),
727 __ATTR_NULL,
730 static struct class gpio_class = {
731 .name = "gpio",
732 .owner = THIS_MODULE,
734 .class_attrs = gpio_class_attrs,
739 * gpio_export - export a GPIO through sysfs
740 * @gpio: gpio to make available, already requested
741 * @direction_may_change: true if userspace may change gpio direction
742 * Context: arch_initcall or later
744 * When drivers want to make a GPIO accessible to userspace after they
745 * have requested it -- perhaps while debugging, or as part of their
746 * public interface -- they may use this routine. If the GPIO can
747 * change direction (some can't) and the caller allows it, userspace
748 * will see "direction" sysfs attribute which may be used to change
749 * the gpio's direction. A "value" attribute will always be provided.
751 * Returns zero on success, else an error.
753 static int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
755 unsigned long flags;
756 int status;
757 const char *ioname = NULL;
758 struct device *dev;
759 int offset;
761 /* can't export until sysfs is available ... */
762 if (!gpio_class.p) {
763 pr_debug("%s: called too early!\n", __func__);
764 return -ENOENT;
767 if (!desc) {
768 pr_debug("%s: invalid gpio descriptor\n", __func__);
769 return -EINVAL;
772 mutex_lock(&sysfs_lock);
774 spin_lock_irqsave(&gpio_lock, flags);
775 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
776 test_bit(FLAG_EXPORT, &desc->flags)) {
777 spin_unlock_irqrestore(&gpio_lock, flags);
778 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
779 __func__, desc_to_gpio(desc),
780 test_bit(FLAG_REQUESTED, &desc->flags),
781 test_bit(FLAG_EXPORT, &desc->flags));
782 status = -EPERM;
783 goto fail_unlock;
786 if (!desc->chip->direction_input || !desc->chip->direction_output)
787 direction_may_change = false;
788 spin_unlock_irqrestore(&gpio_lock, flags);
790 offset = gpio_chip_hwgpio(desc);
791 if (desc->chip->names && desc->chip->names[offset])
792 ioname = desc->chip->names[offset];
794 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
795 desc, ioname ? ioname : "gpio%u",
796 desc_to_gpio(desc));
797 if (IS_ERR(dev)) {
798 status = PTR_ERR(dev);
799 goto fail_unlock;
802 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
803 if (status)
804 goto fail_unregister_device;
806 if (direction_may_change) {
807 status = device_create_file(dev, &dev_attr_direction);
808 if (status)
809 goto fail_unregister_device;
812 if (gpiod_to_irq(desc) >= 0 && (direction_may_change ||
813 !test_bit(FLAG_IS_OUT, &desc->flags))) {
814 status = device_create_file(dev, &dev_attr_edge);
815 if (status)
816 goto fail_unregister_device;
819 set_bit(FLAG_EXPORT, &desc->flags);
820 mutex_unlock(&sysfs_lock);
821 return 0;
823 fail_unregister_device:
824 device_unregister(dev);
825 fail_unlock:
826 mutex_unlock(&sysfs_lock);
827 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
828 status);
829 return status;
832 int gpio_export(unsigned gpio, bool direction_may_change)
834 return gpiod_export(gpio_to_desc(gpio), direction_may_change);
836 EXPORT_SYMBOL_GPL(gpio_export);
838 static int match_export(struct device *dev, const void *data)
840 return dev_get_drvdata(dev) == data;
844 * gpio_export_link - create a sysfs link to an exported GPIO node
845 * @dev: device under which to create symlink
846 * @name: name of the symlink
847 * @gpio: gpio to create symlink to, already exported
849 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
850 * node. Caller is responsible for unlinking.
852 * Returns zero on success, else an error.
854 static int gpiod_export_link(struct device *dev, const char *name,
855 struct gpio_desc *desc)
857 int status = -EINVAL;
859 if (!desc) {
860 pr_warn("%s: invalid GPIO\n", __func__);
861 return -EINVAL;
864 mutex_lock(&sysfs_lock);
866 if (test_bit(FLAG_EXPORT, &desc->flags)) {
867 struct device *tdev;
869 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
870 if (tdev != NULL) {
871 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
872 name);
873 } else {
874 status = -ENODEV;
878 mutex_unlock(&sysfs_lock);
880 if (status)
881 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
882 status);
884 return status;
887 int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
889 return gpiod_export_link(dev, name, gpio_to_desc(gpio));
891 EXPORT_SYMBOL_GPL(gpio_export_link);
894 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
895 * @gpio: gpio to change
896 * @value: non-zero to use active low, i.e. inverted values
898 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
899 * The GPIO does not have to be exported yet. If poll(2) support has
900 * been enabled for either rising or falling edge, it will be
901 * reconfigured to follow the new polarity.
903 * Returns zero on success, else an error.
905 static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
907 struct device *dev = NULL;
908 int status = -EINVAL;
910 if (!desc) {
911 pr_warn("%s: invalid GPIO\n", __func__);
912 return -EINVAL;
915 mutex_lock(&sysfs_lock);
917 if (test_bit(FLAG_EXPORT, &desc->flags)) {
918 dev = class_find_device(&gpio_class, NULL, desc, match_export);
919 if (dev == NULL) {
920 status = -ENODEV;
921 goto unlock;
925 status = sysfs_set_active_low(desc, dev, value);
927 unlock:
928 mutex_unlock(&sysfs_lock);
930 if (status)
931 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
932 status);
934 return status;
937 int gpio_sysfs_set_active_low(unsigned gpio, int value)
939 return gpiod_sysfs_set_active_low(gpio_to_desc(gpio), value);
941 EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
944 * gpio_unexport - reverse effect of gpio_export()
945 * @gpio: gpio to make unavailable
947 * This is implicit on gpio_free().
949 static void gpiod_unexport(struct gpio_desc *desc)
951 int status = 0;
952 struct device *dev = NULL;
954 if (!desc) {
955 pr_warn("%s: invalid GPIO\n", __func__);
956 return;
959 mutex_lock(&sysfs_lock);
961 if (test_bit(FLAG_EXPORT, &desc->flags)) {
963 dev = class_find_device(&gpio_class, NULL, desc, match_export);
964 if (dev) {
965 gpio_setup_irq(desc, dev, 0);
966 clear_bit(FLAG_EXPORT, &desc->flags);
967 } else
968 status = -ENODEV;
971 mutex_unlock(&sysfs_lock);
973 if (dev) {
974 device_unregister(dev);
975 put_device(dev);
978 if (status)
979 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
980 status);
983 void gpio_unexport(unsigned gpio)
985 gpiod_unexport(gpio_to_desc(gpio));
987 EXPORT_SYMBOL_GPL(gpio_unexport);
989 static int gpiochip_export(struct gpio_chip *chip)
991 int status;
992 struct device *dev;
994 /* Many systems register gpio chips for SOC support very early,
995 * before driver model support is available. In those cases we
996 * export this later, in gpiolib_sysfs_init() ... here we just
997 * verify that _some_ field of gpio_class got initialized.
999 if (!gpio_class.p)
1000 return 0;
1002 /* use chip->base for the ID; it's already known to be unique */
1003 mutex_lock(&sysfs_lock);
1004 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
1005 "gpiochip%d", chip->base);
1006 if (!IS_ERR(dev)) {
1007 status = sysfs_create_group(&dev->kobj,
1008 &gpiochip_attr_group);
1009 } else
1010 status = PTR_ERR(dev);
1011 chip->exported = (status == 0);
1012 mutex_unlock(&sysfs_lock);
1014 if (status) {
1015 unsigned long flags;
1016 unsigned gpio;
1018 spin_lock_irqsave(&gpio_lock, flags);
1019 gpio = 0;
1020 while (gpio < chip->ngpio)
1021 chip->desc[gpio++].chip = NULL;
1022 spin_unlock_irqrestore(&gpio_lock, flags);
1024 pr_debug("%s: chip %s status %d\n", __func__,
1025 chip->label, status);
1028 return status;
1031 static void gpiochip_unexport(struct gpio_chip *chip)
1033 int status;
1034 struct device *dev;
1036 mutex_lock(&sysfs_lock);
1037 dev = class_find_device(&gpio_class, NULL, chip, match_export);
1038 if (dev) {
1039 put_device(dev);
1040 device_unregister(dev);
1041 chip->exported = 0;
1042 status = 0;
1043 } else
1044 status = -ENODEV;
1045 mutex_unlock(&sysfs_lock);
1047 if (status)
1048 pr_debug("%s: chip %s status %d\n", __func__,
1049 chip->label, status);
1052 static int __init gpiolib_sysfs_init(void)
1054 int status;
1055 unsigned long flags;
1056 struct gpio_chip *chip;
1058 status = class_register(&gpio_class);
1059 if (status < 0)
1060 return status;
1062 /* Scan and register the gpio_chips which registered very
1063 * early (e.g. before the class_register above was called).
1065 * We run before arch_initcall() so chip->dev nodes can have
1066 * registered, and so arch_initcall() can always gpio_export().
1068 spin_lock_irqsave(&gpio_lock, flags);
1069 list_for_each_entry(chip, &gpio_chips, list) {
1070 if (!chip || chip->exported)
1071 continue;
1073 spin_unlock_irqrestore(&gpio_lock, flags);
1074 status = gpiochip_export(chip);
1075 spin_lock_irqsave(&gpio_lock, flags);
1077 spin_unlock_irqrestore(&gpio_lock, flags);
1080 return status;
1082 postcore_initcall(gpiolib_sysfs_init);
1084 #else
1085 static inline int gpiochip_export(struct gpio_chip *chip)
1087 return 0;
1090 static inline void gpiochip_unexport(struct gpio_chip *chip)
1094 static inline int gpiod_export(struct gpio_desc *desc,
1095 bool direction_may_change)
1097 return -ENOSYS;
1100 static inline int gpiod_export_link(struct device *dev, const char *name,
1101 struct gpio_desc *desc)
1103 return -ENOSYS;
1106 static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
1108 return -ENOSYS;
1111 static inline void gpiod_unexport(struct gpio_desc *desc)
1115 #endif /* CONFIG_GPIO_SYSFS */
1118 * Add a new chip to the global chips list, keeping the list of chips sorted
1119 * by base order.
1121 * Return -EBUSY if the new chip overlaps with some other chip's integer
1122 * space.
1124 static int gpiochip_add_to_list(struct gpio_chip *chip)
1126 struct list_head *pos = &gpio_chips;
1127 struct gpio_chip *_chip;
1128 int err = 0;
1130 /* find where to insert our chip */
1131 list_for_each(pos, &gpio_chips) {
1132 _chip = list_entry(pos, struct gpio_chip, list);
1133 /* shall we insert before _chip? */
1134 if (_chip->base >= chip->base + chip->ngpio)
1135 break;
1138 /* are we stepping on the chip right before? */
1139 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
1140 _chip = list_entry(pos->prev, struct gpio_chip, list);
1141 if (_chip->base + _chip->ngpio > chip->base) {
1142 dev_err(chip->dev,
1143 "GPIO integer space overlap, cannot add chip\n");
1144 err = -EBUSY;
1148 if (!err)
1149 list_add_tail(&chip->list, pos);
1151 return err;
1155 * gpiochip_add() - register a gpio_chip
1156 * @chip: the chip to register, with chip->base initialized
1157 * Context: potentially before irqs or kmalloc will work
1159 * Returns a negative errno if the chip can't be registered, such as
1160 * because the chip->base is invalid or already associated with a
1161 * different chip. Otherwise it returns zero as a success code.
1163 * When gpiochip_add() is called very early during boot, so that GPIOs
1164 * can be freely used, the chip->dev device must be registered before
1165 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1166 * for GPIOs will fail rudely.
1168 * If chip->base is negative, this requests dynamic assignment of
1169 * a range of valid GPIOs.
1171 int gpiochip_add(struct gpio_chip *chip)
1173 unsigned long flags;
1174 int status = 0;
1175 unsigned id;
1176 int base = chip->base;
1178 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
1179 && base >= 0) {
1180 status = -EINVAL;
1181 goto fail;
1184 spin_lock_irqsave(&gpio_lock, flags);
1186 if (base < 0) {
1187 base = gpiochip_find_base(chip->ngpio);
1188 if (base < 0) {
1189 status = base;
1190 goto unlock;
1192 chip->base = base;
1195 status = gpiochip_add_to_list(chip);
1197 if (status == 0) {
1198 chip->desc = &gpio_desc[chip->base];
1200 for (id = 0; id < chip->ngpio; id++) {
1201 struct gpio_desc *desc = &chip->desc[id];
1202 desc->chip = chip;
1204 /* REVISIT: most hardware initializes GPIOs as
1205 * inputs (often with pullups enabled) so power
1206 * usage is minimized. Linux code should set the
1207 * gpio direction first thing; but until it does,
1208 * and in case chip->get_direction is not set,
1209 * we may expose the wrong direction in sysfs.
1211 desc->flags = !chip->direction_input
1212 ? (1 << FLAG_IS_OUT)
1213 : 0;
1217 spin_unlock_irqrestore(&gpio_lock, flags);
1219 #ifdef CONFIG_PINCTRL
1220 INIT_LIST_HEAD(&chip->pin_ranges);
1221 #endif
1223 of_gpiochip_add(chip);
1225 if (status)
1226 goto fail;
1228 status = gpiochip_export(chip);
1229 if (status)
1230 goto fail;
1232 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
1233 chip->base, chip->base + chip->ngpio - 1,
1234 chip->label ? : "generic");
1236 return 0;
1238 unlock:
1239 spin_unlock_irqrestore(&gpio_lock, flags);
1240 fail:
1241 /* failures here can mean systems won't boot... */
1242 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1243 chip->base, chip->base + chip->ngpio - 1,
1244 chip->label ? : "generic");
1245 return status;
1247 EXPORT_SYMBOL_GPL(gpiochip_add);
1250 * gpiochip_remove() - unregister a gpio_chip
1251 * @chip: the chip to unregister
1253 * A gpio_chip with any GPIOs still requested may not be removed.
1255 int gpiochip_remove(struct gpio_chip *chip)
1257 unsigned long flags;
1258 int status = 0;
1259 unsigned id;
1261 spin_lock_irqsave(&gpio_lock, flags);
1263 gpiochip_remove_pin_ranges(chip);
1264 of_gpiochip_remove(chip);
1266 for (id = 0; id < chip->ngpio; id++) {
1267 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
1268 status = -EBUSY;
1269 break;
1272 if (status == 0) {
1273 for (id = 0; id < chip->ngpio; id++)
1274 chip->desc[id].chip = NULL;
1276 list_del(&chip->list);
1279 spin_unlock_irqrestore(&gpio_lock, flags);
1281 if (status == 0)
1282 gpiochip_unexport(chip);
1284 return status;
1286 EXPORT_SYMBOL_GPL(gpiochip_remove);
1289 * gpiochip_find() - iterator for locating a specific gpio_chip
1290 * @data: data to pass to match function
1291 * @callback: Callback function to check gpio_chip
1293 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1294 * determined by a user supplied @match callback. The callback should return
1295 * 0 if the device doesn't match and non-zero if it does. If the callback is
1296 * non-zero, this function will return to the caller and not iterate over any
1297 * more gpio_chips.
1299 struct gpio_chip *gpiochip_find(void *data,
1300 int (*match)(struct gpio_chip *chip,
1301 void *data))
1303 struct gpio_chip *chip;
1304 unsigned long flags;
1306 spin_lock_irqsave(&gpio_lock, flags);
1307 list_for_each_entry(chip, &gpio_chips, list)
1308 if (match(chip, data))
1309 break;
1311 /* No match? */
1312 if (&chip->list == &gpio_chips)
1313 chip = NULL;
1314 spin_unlock_irqrestore(&gpio_lock, flags);
1316 return chip;
1318 EXPORT_SYMBOL_GPL(gpiochip_find);
1320 #ifdef CONFIG_PINCTRL
1323 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1324 * @chip: the gpiochip to add the range for
1325 * @pinctrl_name: the dev_name() of the pin controller to map to
1326 * @gpio_offset: the start offset in the current gpio_chip number space
1327 * @pin_offset: the start offset in the pin controller number space
1328 * @npins: the number of pins from the offset of each pin space (GPIO and
1329 * pin controller) to accumulate in this range
1331 int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
1332 unsigned int gpio_offset, unsigned int pin_offset,
1333 unsigned int npins)
1335 struct gpio_pin_range *pin_range;
1336 int ret;
1338 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1339 if (!pin_range) {
1340 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1341 chip->label);
1342 return -ENOMEM;
1345 /* Use local offset as range ID */
1346 pin_range->range.id = gpio_offset;
1347 pin_range->range.gc = chip;
1348 pin_range->range.name = chip->label;
1349 pin_range->range.base = chip->base + gpio_offset;
1350 pin_range->range.pin_base = pin_offset;
1351 pin_range->range.npins = npins;
1352 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
1353 &pin_range->range);
1354 if (IS_ERR(pin_range->pctldev)) {
1355 ret = PTR_ERR(pin_range->pctldev);
1356 pr_err("%s: GPIO chip: could not create pin range\n",
1357 chip->label);
1358 kfree(pin_range);
1359 return ret;
1361 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1362 chip->label, gpio_offset, gpio_offset + npins - 1,
1363 pinctl_name,
1364 pin_offset, pin_offset + npins - 1);
1366 list_add_tail(&pin_range->node, &chip->pin_ranges);
1368 return 0;
1370 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
1373 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1374 * @chip: the chip to remove all the mappings for
1376 void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1378 struct gpio_pin_range *pin_range, *tmp;
1380 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1381 list_del(&pin_range->node);
1382 pinctrl_remove_gpio_range(pin_range->pctldev,
1383 &pin_range->range);
1384 kfree(pin_range);
1387 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1389 #endif /* CONFIG_PINCTRL */
1391 /* These "optional" allocation calls help prevent drivers from stomping
1392 * on each other, and help provide better diagnostics in debugfs.
1393 * They're called even less than the "set direction" calls.
1395 static int gpiod_request(struct gpio_desc *desc, const char *label)
1397 struct gpio_chip *chip;
1398 int status = -EPROBE_DEFER;
1399 unsigned long flags;
1401 if (!desc) {
1402 pr_warn("%s: invalid GPIO\n", __func__);
1403 return -EINVAL;
1406 spin_lock_irqsave(&gpio_lock, flags);
1408 chip = desc->chip;
1409 if (chip == NULL)
1410 goto done;
1412 if (!try_module_get(chip->owner))
1413 goto done;
1415 /* NOTE: gpio_request() can be called in early boot,
1416 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1419 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1420 desc_set_label(desc, label ? : "?");
1421 status = 0;
1422 } else {
1423 status = -EBUSY;
1424 module_put(chip->owner);
1425 goto done;
1428 if (chip->request) {
1429 /* chip->request may sleep */
1430 spin_unlock_irqrestore(&gpio_lock, flags);
1431 status = chip->request(chip, gpio_chip_hwgpio(desc));
1432 spin_lock_irqsave(&gpio_lock, flags);
1434 if (status < 0) {
1435 desc_set_label(desc, NULL);
1436 module_put(chip->owner);
1437 clear_bit(FLAG_REQUESTED, &desc->flags);
1438 goto done;
1441 if (chip->get_direction) {
1442 /* chip->get_direction may sleep */
1443 spin_unlock_irqrestore(&gpio_lock, flags);
1444 gpiod_get_direction(desc);
1445 spin_lock_irqsave(&gpio_lock, flags);
1447 done:
1448 if (status)
1449 pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
1450 desc_to_gpio(desc), label ? : "?", status);
1451 spin_unlock_irqrestore(&gpio_lock, flags);
1452 return status;
1455 int gpio_request(unsigned gpio, const char *label)
1457 return gpiod_request(gpio_to_desc(gpio), label);
1459 EXPORT_SYMBOL_GPL(gpio_request);
1461 static void gpiod_free(struct gpio_desc *desc)
1463 unsigned long flags;
1464 struct gpio_chip *chip;
1466 might_sleep();
1468 if (!desc) {
1469 WARN_ON(extra_checks);
1470 return;
1473 gpiod_unexport(desc);
1475 spin_lock_irqsave(&gpio_lock, flags);
1477 chip = desc->chip;
1478 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1479 if (chip->free) {
1480 spin_unlock_irqrestore(&gpio_lock, flags);
1481 might_sleep_if(chip->can_sleep);
1482 chip->free(chip, gpio_chip_hwgpio(desc));
1483 spin_lock_irqsave(&gpio_lock, flags);
1485 desc_set_label(desc, NULL);
1486 module_put(desc->chip->owner);
1487 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1488 clear_bit(FLAG_REQUESTED, &desc->flags);
1489 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1490 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1491 } else
1492 WARN_ON(extra_checks);
1494 spin_unlock_irqrestore(&gpio_lock, flags);
1497 void gpio_free(unsigned gpio)
1499 gpiod_free(gpio_to_desc(gpio));
1501 EXPORT_SYMBOL_GPL(gpio_free);
1504 * gpio_request_one - request a single GPIO with initial configuration
1505 * @gpio: the GPIO number
1506 * @flags: GPIO configuration as specified by GPIOF_*
1507 * @label: a literal description string of this GPIO
1509 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1511 struct gpio_desc *desc;
1512 int err;
1514 desc = gpio_to_desc(gpio);
1516 err = gpiod_request(desc, label);
1517 if (err)
1518 return err;
1520 if (flags & GPIOF_OPEN_DRAIN)
1521 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
1523 if (flags & GPIOF_OPEN_SOURCE)
1524 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
1526 if (flags & GPIOF_DIR_IN)
1527 err = gpiod_direction_input(desc);
1528 else
1529 err = gpiod_direction_output(desc,
1530 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1532 if (err)
1533 goto free_gpio;
1535 if (flags & GPIOF_EXPORT) {
1536 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
1537 if (err)
1538 goto free_gpio;
1541 return 0;
1543 free_gpio:
1544 gpiod_free(desc);
1545 return err;
1547 EXPORT_SYMBOL_GPL(gpio_request_one);
1550 * gpio_request_array - request multiple GPIOs in a single call
1551 * @array: array of the 'struct gpio'
1552 * @num: how many GPIOs in the array
1554 int gpio_request_array(const struct gpio *array, size_t num)
1556 int i, err;
1558 for (i = 0; i < num; i++, array++) {
1559 err = gpio_request_one(array->gpio, array->flags, array->label);
1560 if (err)
1561 goto err_free;
1563 return 0;
1565 err_free:
1566 while (i--)
1567 gpio_free((--array)->gpio);
1568 return err;
1570 EXPORT_SYMBOL_GPL(gpio_request_array);
1573 * gpio_free_array - release multiple GPIOs in a single call
1574 * @array: array of the 'struct gpio'
1575 * @num: how many GPIOs in the array
1577 void gpio_free_array(const struct gpio *array, size_t num)
1579 while (num--)
1580 gpio_free((array++)->gpio);
1582 EXPORT_SYMBOL_GPL(gpio_free_array);
1585 * gpiochip_is_requested - return string iff signal was requested
1586 * @chip: controller managing the signal
1587 * @offset: of signal within controller's 0..(ngpio - 1) range
1589 * Returns NULL if the GPIO is not currently requested, else a string.
1590 * If debugfs support is enabled, the string returned is the label passed
1591 * to gpio_request(); otherwise it is a meaningless constant.
1593 * This function is for use by GPIO controller drivers. The label can
1594 * help with diagnostics, and knowing that the signal is used as a GPIO
1595 * can help avoid accidentally multiplexing it to another controller.
1597 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1599 struct gpio_desc *desc;
1601 if (!GPIO_OFFSET_VALID(chip, offset))
1602 return NULL;
1604 desc = &chip->desc[offset];
1606 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
1607 return NULL;
1608 #ifdef CONFIG_DEBUG_FS
1609 return desc->label;
1610 #else
1611 return "?";
1612 #endif
1614 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1617 /* Drivers MUST set GPIO direction before making get/set calls. In
1618 * some cases this is done in early boot, before IRQs are enabled.
1620 * As a rule these aren't called more than once (except for drivers
1621 * using the open-drain emulation idiom) so these are natural places
1622 * to accumulate extra debugging checks. Note that we can't (yet)
1623 * rely on gpio_request() having been called beforehand.
1626 static int gpiod_direction_input(struct gpio_desc *desc)
1628 unsigned long flags;
1629 struct gpio_chip *chip;
1630 int status = -EINVAL;
1631 int offset;
1633 if (!desc || !desc->chip) {
1634 pr_warn("%s: invalid GPIO\n", __func__);
1635 return -EINVAL;
1638 chip = desc->chip;
1639 if (!chip->get || !chip->direction_input) {
1640 pr_warn("%s: missing get() or direction_input() operations\n",
1641 __func__);
1642 return -EIO;
1645 spin_lock_irqsave(&gpio_lock, flags);
1647 status = gpio_ensure_requested(desc);
1648 if (status < 0)
1649 goto fail;
1651 /* now we know the gpio is valid and chip won't vanish */
1653 spin_unlock_irqrestore(&gpio_lock, flags);
1655 might_sleep_if(chip->can_sleep);
1657 offset = gpio_chip_hwgpio(desc);
1658 if (status) {
1659 status = chip->request(chip, offset);
1660 if (status < 0) {
1661 pr_debug("GPIO-%d: chip request fail, %d\n",
1662 desc_to_gpio(desc), status);
1663 /* and it's not available to anyone else ...
1664 * gpio_request() is the fully clean solution.
1666 goto lose;
1670 status = chip->direction_input(chip, offset);
1671 if (status == 0)
1672 clear_bit(FLAG_IS_OUT, &desc->flags);
1674 trace_gpio_direction(desc_to_gpio(desc), 1, status);
1675 lose:
1676 return status;
1677 fail:
1678 spin_unlock_irqrestore(&gpio_lock, flags);
1679 if (status)
1680 pr_debug("%s: gpio-%d status %d\n", __func__,
1681 desc_to_gpio(desc), status);
1682 return status;
1685 int gpio_direction_input(unsigned gpio)
1687 return gpiod_direction_input(gpio_to_desc(gpio));
1689 EXPORT_SYMBOL_GPL(gpio_direction_input);
1691 static int gpiod_direction_output(struct gpio_desc *desc, int value)
1693 unsigned long flags;
1694 struct gpio_chip *chip;
1695 int status = -EINVAL;
1696 int offset;
1698 if (!desc || !desc->chip) {
1699 pr_warn("%s: invalid GPIO\n", __func__);
1700 return -EINVAL;
1703 /* Open drain pin should not be driven to 1 */
1704 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1705 return gpiod_direction_input(desc);
1707 /* Open source pin should not be driven to 0 */
1708 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1709 return gpiod_direction_input(desc);
1711 chip = desc->chip;
1712 if (!chip->set || !chip->direction_output) {
1713 pr_warn("%s: missing set() or direction_output() operations\n",
1714 __func__);
1715 return -EIO;
1718 spin_lock_irqsave(&gpio_lock, flags);
1720 status = gpio_ensure_requested(desc);
1721 if (status < 0)
1722 goto fail;
1724 /* now we know the gpio is valid and chip won't vanish */
1726 spin_unlock_irqrestore(&gpio_lock, flags);
1728 might_sleep_if(chip->can_sleep);
1730 offset = gpio_chip_hwgpio(desc);
1731 if (status) {
1732 status = chip->request(chip, offset);
1733 if (status < 0) {
1734 pr_debug("GPIO-%d: chip request fail, %d\n",
1735 desc_to_gpio(desc), status);
1736 /* and it's not available to anyone else ...
1737 * gpio_request() is the fully clean solution.
1739 goto lose;
1743 status = chip->direction_output(chip, offset, value);
1744 if (status == 0)
1745 set_bit(FLAG_IS_OUT, &desc->flags);
1746 trace_gpio_value(desc_to_gpio(desc), 0, value);
1747 trace_gpio_direction(desc_to_gpio(desc), 0, status);
1748 lose:
1749 return status;
1750 fail:
1751 spin_unlock_irqrestore(&gpio_lock, flags);
1752 if (status)
1753 pr_debug("%s: gpio-%d status %d\n", __func__,
1754 desc_to_gpio(desc), status);
1755 return status;
1758 int gpio_direction_output(unsigned gpio, int value)
1760 return gpiod_direction_output(gpio_to_desc(gpio), value);
1762 EXPORT_SYMBOL_GPL(gpio_direction_output);
1765 * gpio_set_debounce - sets @debounce time for a @gpio
1766 * @gpio: the gpio to set debounce time
1767 * @debounce: debounce time is microseconds
1769 * returns -ENOTSUPP if the controller does not support setting
1770 * debounce.
1772 static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
1774 unsigned long flags;
1775 struct gpio_chip *chip;
1776 int status = -EINVAL;
1777 int offset;
1779 if (!desc || !desc->chip) {
1780 pr_warn("%s: invalid GPIO\n", __func__);
1781 return -EINVAL;
1784 chip = desc->chip;
1785 if (!chip->set || !chip->set_debounce) {
1786 pr_debug("%s: missing set() or set_debounce() operations\n",
1787 __func__);
1788 return -ENOTSUPP;
1791 spin_lock_irqsave(&gpio_lock, flags);
1793 status = gpio_ensure_requested(desc);
1794 if (status < 0)
1795 goto fail;
1797 /* now we know the gpio is valid and chip won't vanish */
1799 spin_unlock_irqrestore(&gpio_lock, flags);
1801 might_sleep_if(chip->can_sleep);
1803 offset = gpio_chip_hwgpio(desc);
1804 return chip->set_debounce(chip, offset, debounce);
1806 fail:
1807 spin_unlock_irqrestore(&gpio_lock, flags);
1808 if (status)
1809 pr_debug("%s: gpio-%d status %d\n", __func__,
1810 desc_to_gpio(desc), status);
1812 return status;
1815 int gpio_set_debounce(unsigned gpio, unsigned debounce)
1817 return gpiod_set_debounce(gpio_to_desc(gpio), debounce);
1819 EXPORT_SYMBOL_GPL(gpio_set_debounce);
1821 /* I/O calls are only valid after configuration completed; the relevant
1822 * "is this a valid GPIO" error checks should already have been done.
1824 * "Get" operations are often inlinable as reading a pin value register,
1825 * and masking the relevant bit in that register.
1827 * When "set" operations are inlinable, they involve writing that mask to
1828 * one register to set a low value, or a different register to set it high.
1829 * Otherwise locking is needed, so there may be little value to inlining.
1831 *------------------------------------------------------------------------
1833 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1834 * have requested the GPIO. That can include implicit requesting by
1835 * a direction setting call. Marking a gpio as requested locks its chip
1836 * in memory, guaranteeing that these table lookups need no more locking
1837 * and that gpiochip_remove() will fail.
1839 * REVISIT when debugging, consider adding some instrumentation to ensure
1840 * that the GPIO was actually requested.
1844 * __gpio_get_value() - return a gpio's value
1845 * @gpio: gpio whose value will be returned
1846 * Context: any
1848 * This is used directly or indirectly to implement gpio_get_value().
1849 * It returns the zero or nonzero value provided by the associated
1850 * gpio_chip.get() method; or zero if no such method is provided.
1852 static int gpiod_get_value(const struct gpio_desc *desc)
1854 struct gpio_chip *chip;
1855 int value;
1856 int offset;
1858 if (!desc)
1859 return 0;
1860 chip = desc->chip;
1861 offset = gpio_chip_hwgpio(desc);
1862 /* Should be using gpio_get_value_cansleep() */
1863 WARN_ON(chip->can_sleep);
1864 value = chip->get ? chip->get(chip, offset) : 0;
1865 trace_gpio_value(desc_to_gpio(desc), 1, value);
1866 return value;
1869 int __gpio_get_value(unsigned gpio)
1871 return gpiod_get_value(gpio_to_desc(gpio));
1873 EXPORT_SYMBOL_GPL(__gpio_get_value);
1876 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1877 * @gpio: Gpio whose state need to be set.
1878 * @chip: Gpio chip.
1879 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1881 static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
1883 int err = 0;
1884 struct gpio_chip *chip = desc->chip;
1885 int offset = gpio_chip_hwgpio(desc);
1887 if (value) {
1888 err = chip->direction_input(chip, offset);
1889 if (!err)
1890 clear_bit(FLAG_IS_OUT, &desc->flags);
1891 } else {
1892 err = chip->direction_output(chip, offset, 0);
1893 if (!err)
1894 set_bit(FLAG_IS_OUT, &desc->flags);
1896 trace_gpio_direction(desc_to_gpio(desc), value, err);
1897 if (err < 0)
1898 pr_err("%s: Error in set_value for open drain gpio%d err %d\n",
1899 __func__, desc_to_gpio(desc), err);
1903 * _gpio_set_open_source() - Set the open source gpio's value.
1904 * @gpio: Gpio whose state need to be set.
1905 * @chip: Gpio chip.
1906 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1908 static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
1910 int err = 0;
1911 struct gpio_chip *chip = desc->chip;
1912 int offset = gpio_chip_hwgpio(desc);
1914 if (value) {
1915 err = chip->direction_output(chip, offset, 1);
1916 if (!err)
1917 set_bit(FLAG_IS_OUT, &desc->flags);
1918 } else {
1919 err = chip->direction_input(chip, offset);
1920 if (!err)
1921 clear_bit(FLAG_IS_OUT, &desc->flags);
1923 trace_gpio_direction(desc_to_gpio(desc), !value, err);
1924 if (err < 0)
1925 pr_err("%s: Error in set_value for open source gpio%d err %d\n",
1926 __func__, desc_to_gpio(desc), err);
1930 * __gpio_set_value() - assign a gpio's value
1931 * @gpio: gpio whose value will be assigned
1932 * @value: value to assign
1933 * Context: any
1935 * This is used directly or indirectly to implement gpio_set_value().
1936 * It invokes the associated gpio_chip.set() method.
1938 static void gpiod_set_value(struct gpio_desc *desc, int value)
1940 struct gpio_chip *chip;
1942 if (!desc)
1943 return;
1944 chip = desc->chip;
1945 /* Should be using gpio_set_value_cansleep() */
1946 WARN_ON(chip->can_sleep);
1947 trace_gpio_value(desc_to_gpio(desc), 0, value);
1948 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1949 _gpio_set_open_drain_value(desc, value);
1950 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1951 _gpio_set_open_source_value(desc, value);
1952 else
1953 chip->set(chip, gpio_chip_hwgpio(desc), value);
1956 void __gpio_set_value(unsigned gpio, int value)
1958 return gpiod_set_value(gpio_to_desc(gpio), value);
1960 EXPORT_SYMBOL_GPL(__gpio_set_value);
1963 * __gpio_cansleep() - report whether gpio value access will sleep
1964 * @gpio: gpio in question
1965 * Context: any
1967 * This is used directly or indirectly to implement gpio_cansleep(). It
1968 * returns nonzero if access reading or writing the GPIO value can sleep.
1970 static int gpiod_cansleep(const struct gpio_desc *desc)
1972 if (!desc)
1973 return 0;
1974 /* only call this on GPIOs that are valid! */
1975 return desc->chip->can_sleep;
1978 int __gpio_cansleep(unsigned gpio)
1980 return gpiod_cansleep(gpio_to_desc(gpio));
1982 EXPORT_SYMBOL_GPL(__gpio_cansleep);
1985 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1986 * @gpio: gpio whose IRQ will be returned (already requested)
1987 * Context: any
1989 * This is used directly or indirectly to implement gpio_to_irq().
1990 * It returns the number of the IRQ signaled by this (input) GPIO,
1991 * or a negative errno.
1993 static int gpiod_to_irq(const struct gpio_desc *desc)
1995 struct gpio_chip *chip;
1996 int offset;
1998 if (!desc)
1999 return -EINVAL;
2000 chip = desc->chip;
2001 offset = gpio_chip_hwgpio(desc);
2002 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
2005 int __gpio_to_irq(unsigned gpio)
2007 return gpiod_to_irq(gpio_to_desc(gpio));
2009 EXPORT_SYMBOL_GPL(__gpio_to_irq);
2012 /* There's no value in making it easy to inline GPIO calls that may sleep.
2013 * Common examples include ones connected to I2C or SPI chips.
2016 static int gpiod_get_value_cansleep(const struct gpio_desc *desc)
2018 struct gpio_chip *chip;
2019 int value;
2020 int offset;
2022 might_sleep_if(extra_checks);
2023 if (!desc)
2024 return 0;
2025 chip = desc->chip;
2026 offset = gpio_chip_hwgpio(desc);
2027 value = chip->get ? chip->get(chip, offset) : 0;
2028 trace_gpio_value(desc_to_gpio(desc), 1, value);
2029 return value;
2032 int gpio_get_value_cansleep(unsigned gpio)
2034 return gpiod_get_value_cansleep(gpio_to_desc(gpio));
2036 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
2038 static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
2040 struct gpio_chip *chip;
2042 might_sleep_if(extra_checks);
2043 if (!desc)
2044 return;
2045 chip = desc->chip;
2046 trace_gpio_value(desc_to_gpio(desc), 0, value);
2047 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2048 _gpio_set_open_drain_value(desc, value);
2049 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2050 _gpio_set_open_source_value(desc, value);
2051 else
2052 chip->set(chip, gpio_chip_hwgpio(desc), value);
2055 void gpio_set_value_cansleep(unsigned gpio, int value)
2057 return gpiod_set_value_cansleep(gpio_to_desc(gpio), value);
2059 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
2061 #ifdef CONFIG_DEBUG_FS
2063 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2065 unsigned i;
2066 unsigned gpio = chip->base;
2067 struct gpio_desc *gdesc = &chip->desc[0];
2068 int is_out;
2070 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2071 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2072 continue;
2074 gpiod_get_direction(gdesc);
2075 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
2076 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
2077 gpio, gdesc->label,
2078 is_out ? "out" : "in ",
2079 chip->get
2080 ? (chip->get(chip, i) ? "hi" : "lo")
2081 : "? ");
2082 seq_printf(s, "\n");
2086 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
2088 unsigned long flags;
2089 struct gpio_chip *chip = NULL;
2090 loff_t index = *pos;
2092 s->private = "";
2094 spin_lock_irqsave(&gpio_lock, flags);
2095 list_for_each_entry(chip, &gpio_chips, list)
2096 if (index-- == 0) {
2097 spin_unlock_irqrestore(&gpio_lock, flags);
2098 return chip;
2100 spin_unlock_irqrestore(&gpio_lock, flags);
2102 return NULL;
2105 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2107 unsigned long flags;
2108 struct gpio_chip *chip = v;
2109 void *ret = NULL;
2111 spin_lock_irqsave(&gpio_lock, flags);
2112 if (list_is_last(&chip->list, &gpio_chips))
2113 ret = NULL;
2114 else
2115 ret = list_entry(chip->list.next, struct gpio_chip, list);
2116 spin_unlock_irqrestore(&gpio_lock, flags);
2118 s->private = "\n";
2119 ++*pos;
2121 return ret;
2124 static void gpiolib_seq_stop(struct seq_file *s, void *v)
2128 static int gpiolib_seq_show(struct seq_file *s, void *v)
2130 struct gpio_chip *chip = v;
2131 struct device *dev;
2133 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2134 chip->base, chip->base + chip->ngpio - 1);
2135 dev = chip->dev;
2136 if (dev)
2137 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2138 dev_name(dev));
2139 if (chip->label)
2140 seq_printf(s, ", %s", chip->label);
2141 if (chip->can_sleep)
2142 seq_printf(s, ", can sleep");
2143 seq_printf(s, ":\n");
2145 if (chip->dbg_show)
2146 chip->dbg_show(s, chip);
2147 else
2148 gpiolib_dbg_show(s, chip);
2150 return 0;
2153 static const struct seq_operations gpiolib_seq_ops = {
2154 .start = gpiolib_seq_start,
2155 .next = gpiolib_seq_next,
2156 .stop = gpiolib_seq_stop,
2157 .show = gpiolib_seq_show,
2160 static int gpiolib_open(struct inode *inode, struct file *file)
2162 return seq_open(file, &gpiolib_seq_ops);
2165 static const struct file_operations gpiolib_operations = {
2166 .owner = THIS_MODULE,
2167 .open = gpiolib_open,
2168 .read = seq_read,
2169 .llseek = seq_lseek,
2170 .release = seq_release,
2173 static int __init gpiolib_debugfs_init(void)
2175 /* /sys/kernel/debug/gpio */
2176 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2177 NULL, NULL, &gpiolib_operations);
2178 return 0;
2180 subsys_initcall(gpiolib_debugfs_init);
2182 #endif /* DEBUG_FS */