1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/interrupt.h>
5 #include <linux/spinlock.h>
6 #include <linux/device.h>
8 #include <linux/debugfs.h>
9 #include <linux/seq_file.h>
10 #include <linux/gpio.h>
11 #include <linux/of_gpio.h>
12 #include <linux/idr.h>
13 #include <linux/slab.h>
15 #define CREATE_TRACE_POINTS
16 #include <trace/events/gpio.h>
18 /* Optional implementation infrastructure for GPIO interfaces.
20 * Platforms may want to use this if they tend to use very many GPIOs
21 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
23 * When kernel footprint or instruction count is an issue, simpler
24 * implementations may be preferred. The GPIO programming interface
25 * allows for inlining speed-critical get/set operations for common
26 * cases, so that access to SOC-integrated GPIOs can sometimes cost
27 * only an instruction or two per bit.
31 /* When debugging, extend minimal trust to callers and platform code.
32 * Also emit diagnostic messages that may help initial bringup, when
33 * board setup or driver bugs are most common.
35 * Otherwise, minimize overhead in what may be bitbanging codepaths.
38 #define extra_checks 1
40 #define extra_checks 0
43 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
44 * While any GPIO is requested, its gpio_chip is not removable;
45 * each GPIO's "requested" flag serves as a lock and refcount.
47 static DEFINE_SPINLOCK(gpio_lock
);
50 struct gpio_chip
*chip
;
52 /* flag symbols are bit numbers */
53 #define FLAG_REQUESTED 0
55 #define FLAG_RESERVED 2
56 #define FLAG_EXPORT 3 /* protected by sysfs_lock */
57 #define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */
58 #define FLAG_TRIG_FALL 5 /* trigger on falling edge */
59 #define FLAG_TRIG_RISE 6 /* trigger on rising edge */
60 #define FLAG_ACTIVE_LOW 7 /* sysfs value has active low */
61 #define FLAG_OPEN_DRAIN 8 /* Gpio is open drain type */
62 #define FLAG_OPEN_SOURCE 9 /* Gpio is open source type */
64 #define ID_SHIFT 16 /* add new flags before this one */
66 #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
67 #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
69 #ifdef CONFIG_DEBUG_FS
73 static struct gpio_desc gpio_desc
[ARCH_NR_GPIOS
];
75 #ifdef CONFIG_GPIO_SYSFS
76 static DEFINE_IDR(dirent_idr
);
79 static inline void desc_set_label(struct gpio_desc
*d
, const char *label
)
81 #ifdef CONFIG_DEBUG_FS
86 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
87 * when setting direction, and otherwise illegal. Until board setup code
88 * and drivers use explicit requests everywhere (which won't happen when
89 * those calls have no teeth) we can't avoid autorequesting. This nag
90 * message should motivate switching to explicit requests... so should
91 * the weaker cleanup after faults, compared to gpio_request().
93 * NOTE: the autorequest mechanism is going away; at this point it's
94 * only "legal" in the sense that (old) code using it won't break yet,
95 * but instead only triggers a WARN() stack dump.
97 static int gpio_ensure_requested(struct gpio_desc
*desc
, unsigned offset
)
99 const struct gpio_chip
*chip
= desc
->chip
;
100 const int gpio
= chip
->base
+ offset
;
102 if (WARN(test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0,
103 "autorequest GPIO-%d\n", gpio
)) {
104 if (!try_module_get(chip
->owner
)) {
105 pr_err("GPIO-%d: module can't be gotten \n", gpio
);
106 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
110 desc_set_label(desc
, "[auto]");
111 /* caller must chip->request() w/o spinlock */
118 /* caller holds gpio_lock *OR* gpio is marked as requested */
119 struct gpio_chip
*gpio_to_chip(unsigned gpio
)
121 return gpio_desc
[gpio
].chip
;
124 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
125 static int gpiochip_find_base(int ngpio
)
131 for (i
= ARCH_NR_GPIOS
- 1; i
>= 0 ; i
--) {
132 struct gpio_desc
*desc
= &gpio_desc
[i
];
133 struct gpio_chip
*chip
= desc
->chip
;
135 if (!chip
&& !test_bit(FLAG_RESERVED
, &desc
->flags
)) {
137 if (spare
== ngpio
) {
144 i
-= chip
->ngpio
- 1;
148 if (gpio_is_valid(base
))
149 pr_debug("%s: found new base at %d\n", __func__
, base
);
154 * gpiochip_reserve() - reserve range of gpios to use with platform code only
155 * @start: starting gpio number
156 * @ngpio: number of gpios to reserve
157 * Context: platform init, potentially before irqs or kmalloc will work
159 * Returns a negative errno if any gpio within the range is already reserved
160 * or registered, else returns zero as a success code. Use this function
161 * to mark a range of gpios as unavailable for dynamic gpio number allocation,
162 * for example because its driver support is not yet loaded.
164 int __init
gpiochip_reserve(int start
, int ngpio
)
170 if (!gpio_is_valid(start
) || !gpio_is_valid(start
+ ngpio
- 1))
173 spin_lock_irqsave(&gpio_lock
, flags
);
175 for (i
= start
; i
< start
+ ngpio
; i
++) {
176 struct gpio_desc
*desc
= &gpio_desc
[i
];
178 if (desc
->chip
|| test_bit(FLAG_RESERVED
, &desc
->flags
)) {
183 set_bit(FLAG_RESERVED
, &desc
->flags
);
186 pr_debug("%s: reserved gpios from %d to %d\n",
187 __func__
, start
, start
+ ngpio
- 1);
189 spin_unlock_irqrestore(&gpio_lock
, flags
);
194 #ifdef CONFIG_GPIO_SYSFS
196 /* lock protects against unexport_gpio() being called while
197 * sysfs files are active.
199 static DEFINE_MUTEX(sysfs_lock
);
202 * /sys/class/gpio/gpioN... only for GPIOs that are exported
204 * * MAY BE OMITTED if kernel won't allow direction changes
205 * * is read/write as "in" or "out"
206 * * may also be written as "high" or "low", initializing
207 * output value as specified ("out" implies "low")
209 * * always readable, subject to hardware behavior
210 * * may be writable, as zero/nonzero
212 * * configures behavior of poll(2) on /value
213 * * available only if pin can generate IRQs on input
214 * * is read/write as "none", "falling", "rising", or "both"
216 * * configures polarity of /value
217 * * is read/write as zero/nonzero
218 * * also affects existing and subsequent "falling" and "rising"
219 * /edge configuration
222 static ssize_t
gpio_direction_show(struct device
*dev
,
223 struct device_attribute
*attr
, char *buf
)
225 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
228 mutex_lock(&sysfs_lock
);
230 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
233 status
= sprintf(buf
, "%s\n",
234 test_bit(FLAG_IS_OUT
, &desc
->flags
)
237 mutex_unlock(&sysfs_lock
);
241 static ssize_t
gpio_direction_store(struct device
*dev
,
242 struct device_attribute
*attr
, const char *buf
, size_t size
)
244 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
245 unsigned gpio
= desc
- gpio_desc
;
248 mutex_lock(&sysfs_lock
);
250 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
252 else if (sysfs_streq(buf
, "high"))
253 status
= gpio_direction_output(gpio
, 1);
254 else if (sysfs_streq(buf
, "out") || sysfs_streq(buf
, "low"))
255 status
= gpio_direction_output(gpio
, 0);
256 else if (sysfs_streq(buf
, "in"))
257 status
= gpio_direction_input(gpio
);
261 mutex_unlock(&sysfs_lock
);
262 return status
? : size
;
265 static /* const */ DEVICE_ATTR(direction
, 0644,
266 gpio_direction_show
, gpio_direction_store
);
268 static ssize_t
gpio_value_show(struct device
*dev
,
269 struct device_attribute
*attr
, char *buf
)
271 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
272 unsigned gpio
= desc
- gpio_desc
;
275 mutex_lock(&sysfs_lock
);
277 if (!test_bit(FLAG_EXPORT
, &desc
->flags
)) {
282 value
= !!gpio_get_value_cansleep(gpio
);
283 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
286 status
= sprintf(buf
, "%d\n", value
);
289 mutex_unlock(&sysfs_lock
);
293 static ssize_t
gpio_value_store(struct device
*dev
,
294 struct device_attribute
*attr
, const char *buf
, size_t size
)
296 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
297 unsigned gpio
= desc
- gpio_desc
;
300 mutex_lock(&sysfs_lock
);
302 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
304 else if (!test_bit(FLAG_IS_OUT
, &desc
->flags
))
309 status
= strict_strtol(buf
, 0, &value
);
311 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
313 gpio_set_value_cansleep(gpio
, value
!= 0);
318 mutex_unlock(&sysfs_lock
);
322 static const DEVICE_ATTR(value
, 0644,
323 gpio_value_show
, gpio_value_store
);
325 static irqreturn_t
gpio_sysfs_irq(int irq
, void *priv
)
327 struct sysfs_dirent
*value_sd
= priv
;
329 sysfs_notify_dirent(value_sd
);
333 static int gpio_setup_irq(struct gpio_desc
*desc
, struct device
*dev
,
334 unsigned long gpio_flags
)
336 struct sysfs_dirent
*value_sd
;
337 unsigned long irq_flags
;
340 if ((desc
->flags
& GPIO_TRIGGER_MASK
) == gpio_flags
)
343 irq
= gpio_to_irq(desc
- gpio_desc
);
347 id
= desc
->flags
>> ID_SHIFT
;
348 value_sd
= idr_find(&dirent_idr
, id
);
350 free_irq(irq
, value_sd
);
352 desc
->flags
&= ~GPIO_TRIGGER_MASK
;
359 irq_flags
= IRQF_SHARED
;
360 if (test_bit(FLAG_TRIG_FALL
, &gpio_flags
))
361 irq_flags
|= test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
) ?
362 IRQF_TRIGGER_RISING
: IRQF_TRIGGER_FALLING
;
363 if (test_bit(FLAG_TRIG_RISE
, &gpio_flags
))
364 irq_flags
|= test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
) ?
365 IRQF_TRIGGER_FALLING
: IRQF_TRIGGER_RISING
;
368 value_sd
= sysfs_get_dirent(dev
->kobj
.sd
, NULL
, "value");
376 if (idr_pre_get(&dirent_idr
, GFP_KERNEL
))
377 ret
= idr_get_new_above(&dirent_idr
, value_sd
,
379 } while (ret
== -EAGAIN
);
384 desc
->flags
&= GPIO_FLAGS_MASK
;
385 desc
->flags
|= (unsigned long)id
<< ID_SHIFT
;
387 if (desc
->flags
>> ID_SHIFT
!= id
) {
393 ret
= request_any_context_irq(irq
, gpio_sysfs_irq
, irq_flags
,
394 "gpiolib", value_sd
);
398 desc
->flags
|= gpio_flags
;
402 idr_remove(&dirent_idr
, id
);
403 desc
->flags
&= GPIO_FLAGS_MASK
;
411 static const struct {
414 } trigger_types
[] = {
416 { "falling", BIT(FLAG_TRIG_FALL
) },
417 { "rising", BIT(FLAG_TRIG_RISE
) },
418 { "both", BIT(FLAG_TRIG_FALL
) | BIT(FLAG_TRIG_RISE
) },
421 static ssize_t
gpio_edge_show(struct device
*dev
,
422 struct device_attribute
*attr
, char *buf
)
424 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
427 mutex_lock(&sysfs_lock
);
429 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
435 for (i
= 0; i
< ARRAY_SIZE(trigger_types
); i
++)
436 if ((desc
->flags
& GPIO_TRIGGER_MASK
)
437 == trigger_types
[i
].flags
) {
438 status
= sprintf(buf
, "%s\n",
439 trigger_types
[i
].name
);
444 mutex_unlock(&sysfs_lock
);
448 static ssize_t
gpio_edge_store(struct device
*dev
,
449 struct device_attribute
*attr
, const char *buf
, size_t size
)
451 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
455 for (i
= 0; i
< ARRAY_SIZE(trigger_types
); i
++)
456 if (sysfs_streq(trigger_types
[i
].name
, buf
))
461 mutex_lock(&sysfs_lock
);
463 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
466 status
= gpio_setup_irq(desc
, dev
, trigger_types
[i
].flags
);
471 mutex_unlock(&sysfs_lock
);
476 static DEVICE_ATTR(edge
, 0644, gpio_edge_show
, gpio_edge_store
);
478 static int sysfs_set_active_low(struct gpio_desc
*desc
, struct device
*dev
,
483 if (!!test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
) == !!value
)
487 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
489 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
491 /* reconfigure poll(2) support if enabled on one edge only */
492 if (dev
!= NULL
&& (!!test_bit(FLAG_TRIG_RISE
, &desc
->flags
) ^
493 !!test_bit(FLAG_TRIG_FALL
, &desc
->flags
))) {
494 unsigned long trigger_flags
= desc
->flags
& GPIO_TRIGGER_MASK
;
496 gpio_setup_irq(desc
, dev
, 0);
497 status
= gpio_setup_irq(desc
, dev
, trigger_flags
);
503 static ssize_t
gpio_active_low_show(struct device
*dev
,
504 struct device_attribute
*attr
, char *buf
)
506 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
509 mutex_lock(&sysfs_lock
);
511 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
514 status
= sprintf(buf
, "%d\n",
515 !!test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
));
517 mutex_unlock(&sysfs_lock
);
522 static ssize_t
gpio_active_low_store(struct device
*dev
,
523 struct device_attribute
*attr
, const char *buf
, size_t size
)
525 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
528 mutex_lock(&sysfs_lock
);
530 if (!test_bit(FLAG_EXPORT
, &desc
->flags
)) {
535 status
= strict_strtol(buf
, 0, &value
);
537 status
= sysfs_set_active_low(desc
, dev
, value
!= 0);
540 mutex_unlock(&sysfs_lock
);
542 return status
? : size
;
545 static const DEVICE_ATTR(active_low
, 0644,
546 gpio_active_low_show
, gpio_active_low_store
);
548 static const struct attribute
*gpio_attrs
[] = {
549 &dev_attr_value
.attr
,
550 &dev_attr_active_low
.attr
,
554 static const struct attribute_group gpio_attr_group
= {
555 .attrs
= (struct attribute
**) gpio_attrs
,
559 * /sys/class/gpio/gpiochipN/
560 * /base ... matching gpio_chip.base (N)
561 * /label ... matching gpio_chip.label
562 * /ngpio ... matching gpio_chip.ngpio
565 static ssize_t
chip_base_show(struct device
*dev
,
566 struct device_attribute
*attr
, char *buf
)
568 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
570 return sprintf(buf
, "%d\n", chip
->base
);
572 static DEVICE_ATTR(base
, 0444, chip_base_show
, NULL
);
574 static ssize_t
chip_label_show(struct device
*dev
,
575 struct device_attribute
*attr
, char *buf
)
577 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
579 return sprintf(buf
, "%s\n", chip
->label
? : "");
581 static DEVICE_ATTR(label
, 0444, chip_label_show
, NULL
);
583 static ssize_t
chip_ngpio_show(struct device
*dev
,
584 struct device_attribute
*attr
, char *buf
)
586 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
588 return sprintf(buf
, "%u\n", chip
->ngpio
);
590 static DEVICE_ATTR(ngpio
, 0444, chip_ngpio_show
, NULL
);
592 static const struct attribute
*gpiochip_attrs
[] = {
594 &dev_attr_label
.attr
,
595 &dev_attr_ngpio
.attr
,
599 static const struct attribute_group gpiochip_attr_group
= {
600 .attrs
= (struct attribute
**) gpiochip_attrs
,
604 * /sys/class/gpio/export ... write-only
605 * integer N ... number of GPIO to export (full access)
606 * /sys/class/gpio/unexport ... write-only
607 * integer N ... number of GPIO to unexport
609 static ssize_t
export_store(struct class *class,
610 struct class_attribute
*attr
,
611 const char *buf
, size_t len
)
616 status
= strict_strtol(buf
, 0, &gpio
);
620 /* No extra locking here; FLAG_SYSFS just signifies that the
621 * request and export were done by on behalf of userspace, so
622 * they may be undone on its behalf too.
625 status
= gpio_request(gpio
, "sysfs");
627 if (status
== -EPROBE_DEFER
)
631 status
= gpio_export(gpio
, true);
635 set_bit(FLAG_SYSFS
, &gpio_desc
[gpio
].flags
);
639 pr_debug("%s: status %d\n", __func__
, status
);
640 return status
? : len
;
643 static ssize_t
unexport_store(struct class *class,
644 struct class_attribute
*attr
,
645 const char *buf
, size_t len
)
650 status
= strict_strtol(buf
, 0, &gpio
);
656 /* reject bogus commands (gpio_unexport ignores them) */
657 if (!gpio_is_valid(gpio
))
660 /* No extra locking here; FLAG_SYSFS just signifies that the
661 * request and export were done by on behalf of userspace, so
662 * they may be undone on its behalf too.
664 if (test_and_clear_bit(FLAG_SYSFS
, &gpio_desc
[gpio
].flags
)) {
670 pr_debug("%s: status %d\n", __func__
, status
);
671 return status
? : len
;
674 static struct class_attribute gpio_class_attrs
[] = {
675 __ATTR(export
, 0200, NULL
, export_store
),
676 __ATTR(unexport
, 0200, NULL
, unexport_store
),
680 static struct class gpio_class
= {
682 .owner
= THIS_MODULE
,
684 .class_attrs
= gpio_class_attrs
,
689 * gpio_export - export a GPIO through sysfs
690 * @gpio: gpio to make available, already requested
691 * @direction_may_change: true if userspace may change gpio direction
692 * Context: arch_initcall or later
694 * When drivers want to make a GPIO accessible to userspace after they
695 * have requested it -- perhaps while debugging, or as part of their
696 * public interface -- they may use this routine. If the GPIO can
697 * change direction (some can't) and the caller allows it, userspace
698 * will see "direction" sysfs attribute which may be used to change
699 * the gpio's direction. A "value" attribute will always be provided.
701 * Returns zero on success, else an error.
703 int gpio_export(unsigned gpio
, bool direction_may_change
)
706 struct gpio_desc
*desc
;
707 int status
= -EINVAL
;
708 const char *ioname
= NULL
;
710 /* can't export until sysfs is available ... */
712 pr_debug("%s: called too early!\n", __func__
);
716 if (!gpio_is_valid(gpio
))
719 mutex_lock(&sysfs_lock
);
721 spin_lock_irqsave(&gpio_lock
, flags
);
722 desc
= &gpio_desc
[gpio
];
723 if (test_bit(FLAG_REQUESTED
, &desc
->flags
)
724 && !test_bit(FLAG_EXPORT
, &desc
->flags
)) {
726 if (!desc
->chip
->direction_input
727 || !desc
->chip
->direction_output
)
728 direction_may_change
= false;
730 spin_unlock_irqrestore(&gpio_lock
, flags
);
732 if (desc
->chip
->names
&& desc
->chip
->names
[gpio
- desc
->chip
->base
])
733 ioname
= desc
->chip
->names
[gpio
- desc
->chip
->base
];
738 dev
= device_create(&gpio_class
, desc
->chip
->dev
, MKDEV(0, 0),
739 desc
, ioname
? ioname
: "gpio%u", gpio
);
741 status
= sysfs_create_group(&dev
->kobj
,
744 if (!status
&& direction_may_change
)
745 status
= device_create_file(dev
,
746 &dev_attr_direction
);
748 if (!status
&& gpio_to_irq(gpio
) >= 0
749 && (direction_may_change
750 || !test_bit(FLAG_IS_OUT
,
752 status
= device_create_file(dev
,
756 device_unregister(dev
);
758 status
= PTR_ERR(dev
);
760 set_bit(FLAG_EXPORT
, &desc
->flags
);
763 mutex_unlock(&sysfs_lock
);
767 pr_debug("%s: gpio%d status %d\n", __func__
, gpio
, status
);
771 EXPORT_SYMBOL_GPL(gpio_export
);
773 static int match_export(struct device
*dev
, void *data
)
775 return dev_get_drvdata(dev
) == data
;
779 * gpio_export_link - create a sysfs link to an exported GPIO node
780 * @dev: device under which to create symlink
781 * @name: name of the symlink
782 * @gpio: gpio to create symlink to, already exported
784 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
785 * node. Caller is responsible for unlinking.
787 * Returns zero on success, else an error.
789 int gpio_export_link(struct device
*dev
, const char *name
, unsigned gpio
)
791 struct gpio_desc
*desc
;
792 int status
= -EINVAL
;
794 if (!gpio_is_valid(gpio
))
797 mutex_lock(&sysfs_lock
);
799 desc
= &gpio_desc
[gpio
];
801 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
804 tdev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
806 status
= sysfs_create_link(&dev
->kobj
, &tdev
->kobj
,
813 mutex_unlock(&sysfs_lock
);
817 pr_debug("%s: gpio%d status %d\n", __func__
, gpio
, status
);
821 EXPORT_SYMBOL_GPL(gpio_export_link
);
825 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
826 * @gpio: gpio to change
827 * @value: non-zero to use active low, i.e. inverted values
829 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
830 * The GPIO does not have to be exported yet. If poll(2) support has
831 * been enabled for either rising or falling edge, it will be
832 * reconfigured to follow the new polarity.
834 * Returns zero on success, else an error.
836 int gpio_sysfs_set_active_low(unsigned gpio
, int value
)
838 struct gpio_desc
*desc
;
839 struct device
*dev
= NULL
;
840 int status
= -EINVAL
;
842 if (!gpio_is_valid(gpio
))
845 mutex_lock(&sysfs_lock
);
847 desc
= &gpio_desc
[gpio
];
849 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
850 dev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
857 status
= sysfs_set_active_low(desc
, dev
, value
);
860 mutex_unlock(&sysfs_lock
);
864 pr_debug("%s: gpio%d status %d\n", __func__
, gpio
, status
);
868 EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low
);
871 * gpio_unexport - reverse effect of gpio_export()
872 * @gpio: gpio to make unavailable
874 * This is implicit on gpio_free().
876 void gpio_unexport(unsigned gpio
)
878 struct gpio_desc
*desc
;
880 struct device
*dev
= NULL
;
882 if (!gpio_is_valid(gpio
)) {
887 mutex_lock(&sysfs_lock
);
889 desc
= &gpio_desc
[gpio
];
891 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
893 dev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
895 gpio_setup_irq(desc
, dev
, 0);
896 clear_bit(FLAG_EXPORT
, &desc
->flags
);
901 mutex_unlock(&sysfs_lock
);
903 device_unregister(dev
);
908 pr_debug("%s: gpio%d status %d\n", __func__
, gpio
, status
);
910 EXPORT_SYMBOL_GPL(gpio_unexport
);
912 static int gpiochip_export(struct gpio_chip
*chip
)
917 /* Many systems register gpio chips for SOC support very early,
918 * before driver model support is available. In those cases we
919 * export this later, in gpiolib_sysfs_init() ... here we just
920 * verify that _some_ field of gpio_class got initialized.
925 /* use chip->base for the ID; it's already known to be unique */
926 mutex_lock(&sysfs_lock
);
927 dev
= device_create(&gpio_class
, chip
->dev
, MKDEV(0, 0), chip
,
928 "gpiochip%d", chip
->base
);
930 status
= sysfs_create_group(&dev
->kobj
,
931 &gpiochip_attr_group
);
933 status
= PTR_ERR(dev
);
934 chip
->exported
= (status
== 0);
935 mutex_unlock(&sysfs_lock
);
941 spin_lock_irqsave(&gpio_lock
, flags
);
943 while (gpio_desc
[gpio
].chip
== chip
)
944 gpio_desc
[gpio
++].chip
= NULL
;
945 spin_unlock_irqrestore(&gpio_lock
, flags
);
947 pr_debug("%s: chip %s status %d\n", __func__
,
948 chip
->label
, status
);
954 static void gpiochip_unexport(struct gpio_chip
*chip
)
959 mutex_lock(&sysfs_lock
);
960 dev
= class_find_device(&gpio_class
, NULL
, chip
, match_export
);
963 device_unregister(dev
);
968 mutex_unlock(&sysfs_lock
);
971 pr_debug("%s: chip %s status %d\n", __func__
,
972 chip
->label
, status
);
975 static int __init
gpiolib_sysfs_init(void)
981 status
= class_register(&gpio_class
);
985 /* Scan and register the gpio_chips which registered very
986 * early (e.g. before the class_register above was called).
988 * We run before arch_initcall() so chip->dev nodes can have
989 * registered, and so arch_initcall() can always gpio_export().
991 spin_lock_irqsave(&gpio_lock
, flags
);
992 for (gpio
= 0; gpio
< ARCH_NR_GPIOS
; gpio
++) {
993 struct gpio_chip
*chip
;
995 chip
= gpio_desc
[gpio
].chip
;
996 if (!chip
|| chip
->exported
)
999 spin_unlock_irqrestore(&gpio_lock
, flags
);
1000 status
= gpiochip_export(chip
);
1001 spin_lock_irqsave(&gpio_lock
, flags
);
1003 spin_unlock_irqrestore(&gpio_lock
, flags
);
1008 postcore_initcall(gpiolib_sysfs_init
);
1011 static inline int gpiochip_export(struct gpio_chip
*chip
)
1016 static inline void gpiochip_unexport(struct gpio_chip
*chip
)
1020 #endif /* CONFIG_GPIO_SYSFS */
1023 * gpiochip_add() - register a gpio_chip
1024 * @chip: the chip to register, with chip->base initialized
1025 * Context: potentially before irqs or kmalloc will work
1027 * Returns a negative errno if the chip can't be registered, such as
1028 * because the chip->base is invalid or already associated with a
1029 * different chip. Otherwise it returns zero as a success code.
1031 * When gpiochip_add() is called very early during boot, so that GPIOs
1032 * can be freely used, the chip->dev device must be registered before
1033 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1034 * for GPIOs will fail rudely.
1036 * If chip->base is negative, this requests dynamic assignment of
1037 * a range of valid GPIOs.
1039 int gpiochip_add(struct gpio_chip
*chip
)
1041 unsigned long flags
;
1044 int base
= chip
->base
;
1046 if ((!gpio_is_valid(base
) || !gpio_is_valid(base
+ chip
->ngpio
- 1))
1052 spin_lock_irqsave(&gpio_lock
, flags
);
1055 base
= gpiochip_find_base(chip
->ngpio
);
1063 /* these GPIO numbers must not be managed by another gpio_chip */
1064 for (id
= base
; id
< base
+ chip
->ngpio
; id
++) {
1065 if (gpio_desc
[id
].chip
!= NULL
) {
1071 for (id
= base
; id
< base
+ chip
->ngpio
; id
++) {
1072 gpio_desc
[id
].chip
= chip
;
1074 /* REVISIT: most hardware initializes GPIOs as
1075 * inputs (often with pullups enabled) so power
1076 * usage is minimized. Linux code should set the
1077 * gpio direction first thing; but until it does,
1078 * we may expose the wrong direction in sysfs.
1080 gpio_desc
[id
].flags
= !chip
->direction_input
1081 ? (1 << FLAG_IS_OUT
)
1086 #ifdef CONFIG_PINCTRL
1087 INIT_LIST_HEAD(&chip
->pin_ranges
);
1090 of_gpiochip_add(chip
);
1093 spin_unlock_irqrestore(&gpio_lock
, flags
);
1098 status
= gpiochip_export(chip
);
1102 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
1103 chip
->base
, chip
->base
+ chip
->ngpio
- 1,
1104 chip
->label
? : "generic");
1108 /* failures here can mean systems won't boot... */
1109 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1110 chip
->base
, chip
->base
+ chip
->ngpio
- 1,
1111 chip
->label
? : "generic");
1114 EXPORT_SYMBOL_GPL(gpiochip_add
);
1117 * gpiochip_remove() - unregister a gpio_chip
1118 * @chip: the chip to unregister
1120 * A gpio_chip with any GPIOs still requested may not be removed.
1122 int gpiochip_remove(struct gpio_chip
*chip
)
1124 unsigned long flags
;
1128 spin_lock_irqsave(&gpio_lock
, flags
);
1130 gpiochip_remove_pin_ranges(chip
);
1131 of_gpiochip_remove(chip
);
1133 for (id
= chip
->base
; id
< chip
->base
+ chip
->ngpio
; id
++) {
1134 if (test_bit(FLAG_REQUESTED
, &gpio_desc
[id
].flags
)) {
1140 for (id
= chip
->base
; id
< chip
->base
+ chip
->ngpio
; id
++)
1141 gpio_desc
[id
].chip
= NULL
;
1144 spin_unlock_irqrestore(&gpio_lock
, flags
);
1147 gpiochip_unexport(chip
);
1151 EXPORT_SYMBOL_GPL(gpiochip_remove
);
1154 * gpiochip_find() - iterator for locating a specific gpio_chip
1155 * @data: data to pass to match function
1156 * @callback: Callback function to check gpio_chip
1158 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1159 * determined by a user supplied @match callback. The callback should return
1160 * 0 if the device doesn't match and non-zero if it does. If the callback is
1161 * non-zero, this function will return to the caller and not iterate over any
1164 struct gpio_chip
*gpiochip_find(void *data
,
1165 int (*match
)(struct gpio_chip
*chip
,
1168 struct gpio_chip
*chip
= NULL
;
1169 unsigned long flags
;
1172 spin_lock_irqsave(&gpio_lock
, flags
);
1173 for (i
= 0; i
< ARCH_NR_GPIOS
; i
++) {
1174 if (!gpio_desc
[i
].chip
)
1177 if (match(gpio_desc
[i
].chip
, data
)) {
1178 chip
= gpio_desc
[i
].chip
;
1182 spin_unlock_irqrestore(&gpio_lock
, flags
);
1186 EXPORT_SYMBOL_GPL(gpiochip_find
);
1188 #ifdef CONFIG_PINCTRL
1190 void gpiochip_add_pin_range(struct gpio_chip
*chip
, const char *pinctl_name
,
1191 unsigned int pin_base
, unsigned int npins
)
1193 struct gpio_pin_range
*pin_range
;
1195 pin_range
= devm_kzalloc(chip
->dev
, sizeof(*pin_range
), GFP_KERNEL
);
1197 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1202 pin_range
->range
.name
= chip
->label
;
1203 pin_range
->range
.base
= chip
->base
;
1204 pin_range
->range
.pin_base
= pin_base
;
1205 pin_range
->range
.npins
= npins
;
1206 pin_range
->pctldev
= find_pinctrl_and_add_gpio_range(pinctl_name
,
1209 list_add_tail(&pin_range
->node
, &chip
->pin_ranges
);
1211 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range
);
1213 void gpiochip_remove_pin_ranges(struct gpio_chip
*chip
)
1215 struct gpio_pin_range
*pin_range
, *tmp
;
1217 list_for_each_entry_safe(pin_range
, tmp
, &chip
->pin_ranges
, node
) {
1218 list_del(&pin_range
->node
);
1219 pinctrl_remove_gpio_range(pin_range
->pctldev
,
1223 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges
);
1225 #endif /* CONFIG_PINCTRL */
1227 /* These "optional" allocation calls help prevent drivers from stomping
1228 * on each other, and help provide better diagnostics in debugfs.
1229 * They're called even less than the "set direction" calls.
1231 int gpio_request(unsigned gpio
, const char *label
)
1233 struct gpio_desc
*desc
;
1234 struct gpio_chip
*chip
;
1235 int status
= -EPROBE_DEFER
;
1236 unsigned long flags
;
1238 spin_lock_irqsave(&gpio_lock
, flags
);
1240 if (!gpio_is_valid(gpio
)) {
1244 desc
= &gpio_desc
[gpio
];
1249 if (!try_module_get(chip
->owner
))
1252 /* NOTE: gpio_request() can be called in early boot,
1253 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1256 if (test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0) {
1257 desc_set_label(desc
, label
? : "?");
1261 module_put(chip
->owner
);
1265 if (chip
->request
) {
1266 /* chip->request may sleep */
1267 spin_unlock_irqrestore(&gpio_lock
, flags
);
1268 status
= chip
->request(chip
, gpio
- chip
->base
);
1269 spin_lock_irqsave(&gpio_lock
, flags
);
1272 desc_set_label(desc
, NULL
);
1273 module_put(chip
->owner
);
1274 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
1280 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
1281 gpio
, label
? : "?", status
);
1282 spin_unlock_irqrestore(&gpio_lock
, flags
);
1285 EXPORT_SYMBOL_GPL(gpio_request
);
1287 void gpio_free(unsigned gpio
)
1289 unsigned long flags
;
1290 struct gpio_desc
*desc
;
1291 struct gpio_chip
*chip
;
1295 if (!gpio_is_valid(gpio
)) {
1296 WARN_ON(extra_checks
);
1300 gpio_unexport(gpio
);
1302 spin_lock_irqsave(&gpio_lock
, flags
);
1304 desc
= &gpio_desc
[gpio
];
1306 if (chip
&& test_bit(FLAG_REQUESTED
, &desc
->flags
)) {
1308 spin_unlock_irqrestore(&gpio_lock
, flags
);
1309 might_sleep_if(chip
->can_sleep
);
1310 chip
->free(chip
, gpio
- chip
->base
);
1311 spin_lock_irqsave(&gpio_lock
, flags
);
1313 desc_set_label(desc
, NULL
);
1314 module_put(desc
->chip
->owner
);
1315 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
1316 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
1317 clear_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
1318 clear_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
1320 WARN_ON(extra_checks
);
1322 spin_unlock_irqrestore(&gpio_lock
, flags
);
1324 EXPORT_SYMBOL_GPL(gpio_free
);
1327 * gpio_request_one - request a single GPIO with initial configuration
1328 * @gpio: the GPIO number
1329 * @flags: GPIO configuration as specified by GPIOF_*
1330 * @label: a literal description string of this GPIO
1332 int gpio_request_one(unsigned gpio
, unsigned long flags
, const char *label
)
1336 err
= gpio_request(gpio
, label
);
1340 if (flags
& GPIOF_OPEN_DRAIN
)
1341 set_bit(FLAG_OPEN_DRAIN
, &gpio_desc
[gpio
].flags
);
1343 if (flags
& GPIOF_OPEN_SOURCE
)
1344 set_bit(FLAG_OPEN_SOURCE
, &gpio_desc
[gpio
].flags
);
1346 if (flags
& GPIOF_DIR_IN
)
1347 err
= gpio_direction_input(gpio
);
1349 err
= gpio_direction_output(gpio
,
1350 (flags
& GPIOF_INIT_HIGH
) ? 1 : 0);
1355 if (flags
& GPIOF_EXPORT
) {
1356 err
= gpio_export(gpio
, flags
& GPIOF_EXPORT_CHANGEABLE
);
1367 EXPORT_SYMBOL_GPL(gpio_request_one
);
1370 * gpio_request_array - request multiple GPIOs in a single call
1371 * @array: array of the 'struct gpio'
1372 * @num: how many GPIOs in the array
1374 int gpio_request_array(const struct gpio
*array
, size_t num
)
1378 for (i
= 0; i
< num
; i
++, array
++) {
1379 err
= gpio_request_one(array
->gpio
, array
->flags
, array
->label
);
1387 gpio_free((--array
)->gpio
);
1390 EXPORT_SYMBOL_GPL(gpio_request_array
);
1393 * gpio_free_array - release multiple GPIOs in a single call
1394 * @array: array of the 'struct gpio'
1395 * @num: how many GPIOs in the array
1397 void gpio_free_array(const struct gpio
*array
, size_t num
)
1400 gpio_free((array
++)->gpio
);
1402 EXPORT_SYMBOL_GPL(gpio_free_array
);
1405 * gpiochip_is_requested - return string iff signal was requested
1406 * @chip: controller managing the signal
1407 * @offset: of signal within controller's 0..(ngpio - 1) range
1409 * Returns NULL if the GPIO is not currently requested, else a string.
1410 * If debugfs support is enabled, the string returned is the label passed
1411 * to gpio_request(); otherwise it is a meaningless constant.
1413 * This function is for use by GPIO controller drivers. The label can
1414 * help with diagnostics, and knowing that the signal is used as a GPIO
1415 * can help avoid accidentally multiplexing it to another controller.
1417 const char *gpiochip_is_requested(struct gpio_chip
*chip
, unsigned offset
)
1419 unsigned gpio
= chip
->base
+ offset
;
1421 if (!gpio_is_valid(gpio
) || gpio_desc
[gpio
].chip
!= chip
)
1423 if (test_bit(FLAG_REQUESTED
, &gpio_desc
[gpio
].flags
) == 0)
1425 #ifdef CONFIG_DEBUG_FS
1426 return gpio_desc
[gpio
].label
;
1431 EXPORT_SYMBOL_GPL(gpiochip_is_requested
);
1434 /* Drivers MUST set GPIO direction before making get/set calls. In
1435 * some cases this is done in early boot, before IRQs are enabled.
1437 * As a rule these aren't called more than once (except for drivers
1438 * using the open-drain emulation idiom) so these are natural places
1439 * to accumulate extra debugging checks. Note that we can't (yet)
1440 * rely on gpio_request() having been called beforehand.
1443 int gpio_direction_input(unsigned gpio
)
1445 unsigned long flags
;
1446 struct gpio_chip
*chip
;
1447 struct gpio_desc
*desc
= &gpio_desc
[gpio
];
1448 int status
= -EINVAL
;
1450 spin_lock_irqsave(&gpio_lock
, flags
);
1452 if (!gpio_is_valid(gpio
))
1455 if (!chip
|| !chip
->get
|| !chip
->direction_input
)
1458 if (gpio
>= chip
->ngpio
)
1460 status
= gpio_ensure_requested(desc
, gpio
);
1464 /* now we know the gpio is valid and chip won't vanish */
1466 spin_unlock_irqrestore(&gpio_lock
, flags
);
1468 might_sleep_if(chip
->can_sleep
);
1471 status
= chip
->request(chip
, gpio
);
1473 pr_debug("GPIO-%d: chip request fail, %d\n",
1474 chip
->base
+ gpio
, status
);
1475 /* and it's not available to anyone else ...
1476 * gpio_request() is the fully clean solution.
1482 status
= chip
->direction_input(chip
, gpio
);
1484 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1486 trace_gpio_direction(chip
->base
+ gpio
, 1, status
);
1490 spin_unlock_irqrestore(&gpio_lock
, flags
);
1492 pr_debug("%s: gpio-%d status %d\n",
1493 __func__
, gpio
, status
);
1496 EXPORT_SYMBOL_GPL(gpio_direction_input
);
1498 int gpio_direction_output(unsigned gpio
, int value
)
1500 unsigned long flags
;
1501 struct gpio_chip
*chip
;
1502 struct gpio_desc
*desc
= &gpio_desc
[gpio
];
1503 int status
= -EINVAL
;
1505 /* Open drain pin should not be driven to 1 */
1506 if (value
&& test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
1507 return gpio_direction_input(gpio
);
1509 /* Open source pin should not be driven to 0 */
1510 if (!value
&& test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
1511 return gpio_direction_input(gpio
);
1513 spin_lock_irqsave(&gpio_lock
, flags
);
1515 if (!gpio_is_valid(gpio
))
1518 if (!chip
|| !chip
->set
|| !chip
->direction_output
)
1521 if (gpio
>= chip
->ngpio
)
1523 status
= gpio_ensure_requested(desc
, gpio
);
1527 /* now we know the gpio is valid and chip won't vanish */
1529 spin_unlock_irqrestore(&gpio_lock
, flags
);
1531 might_sleep_if(chip
->can_sleep
);
1534 status
= chip
->request(chip
, gpio
);
1536 pr_debug("GPIO-%d: chip request fail, %d\n",
1537 chip
->base
+ gpio
, status
);
1538 /* and it's not available to anyone else ...
1539 * gpio_request() is the fully clean solution.
1545 status
= chip
->direction_output(chip
, gpio
, value
);
1547 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1548 trace_gpio_value(chip
->base
+ gpio
, 0, value
);
1549 trace_gpio_direction(chip
->base
+ gpio
, 0, status
);
1553 spin_unlock_irqrestore(&gpio_lock
, flags
);
1555 pr_debug("%s: gpio-%d status %d\n",
1556 __func__
, gpio
, status
);
1559 EXPORT_SYMBOL_GPL(gpio_direction_output
);
1562 * gpio_set_debounce - sets @debounce time for a @gpio
1563 * @gpio: the gpio to set debounce time
1564 * @debounce: debounce time is microseconds
1566 int gpio_set_debounce(unsigned gpio
, unsigned debounce
)
1568 unsigned long flags
;
1569 struct gpio_chip
*chip
;
1570 struct gpio_desc
*desc
= &gpio_desc
[gpio
];
1571 int status
= -EINVAL
;
1573 spin_lock_irqsave(&gpio_lock
, flags
);
1575 if (!gpio_is_valid(gpio
))
1578 if (!chip
|| !chip
->set
|| !chip
->set_debounce
)
1581 if (gpio
>= chip
->ngpio
)
1583 status
= gpio_ensure_requested(desc
, gpio
);
1587 /* now we know the gpio is valid and chip won't vanish */
1589 spin_unlock_irqrestore(&gpio_lock
, flags
);
1591 might_sleep_if(chip
->can_sleep
);
1593 return chip
->set_debounce(chip
, gpio
, debounce
);
1596 spin_unlock_irqrestore(&gpio_lock
, flags
);
1598 pr_debug("%s: gpio-%d status %d\n",
1599 __func__
, gpio
, status
);
1603 EXPORT_SYMBOL_GPL(gpio_set_debounce
);
1605 /* I/O calls are only valid after configuration completed; the relevant
1606 * "is this a valid GPIO" error checks should already have been done.
1608 * "Get" operations are often inlinable as reading a pin value register,
1609 * and masking the relevant bit in that register.
1611 * When "set" operations are inlinable, they involve writing that mask to
1612 * one register to set a low value, or a different register to set it high.
1613 * Otherwise locking is needed, so there may be little value to inlining.
1615 *------------------------------------------------------------------------
1617 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1618 * have requested the GPIO. That can include implicit requesting by
1619 * a direction setting call. Marking a gpio as requested locks its chip
1620 * in memory, guaranteeing that these table lookups need no more locking
1621 * and that gpiochip_remove() will fail.
1623 * REVISIT when debugging, consider adding some instrumentation to ensure
1624 * that the GPIO was actually requested.
1628 * __gpio_get_value() - return a gpio's value
1629 * @gpio: gpio whose value will be returned
1632 * This is used directly or indirectly to implement gpio_get_value().
1633 * It returns the zero or nonzero value provided by the associated
1634 * gpio_chip.get() method; or zero if no such method is provided.
1636 int __gpio_get_value(unsigned gpio
)
1638 struct gpio_chip
*chip
;
1641 chip
= gpio_to_chip(gpio
);
1642 /* Should be using gpio_get_value_cansleep() */
1643 WARN_ON(chip
->can_sleep
);
1644 value
= chip
->get
? chip
->get(chip
, gpio
- chip
->base
) : 0;
1645 trace_gpio_value(gpio
, 1, value
);
1648 EXPORT_SYMBOL_GPL(__gpio_get_value
);
1651 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1652 * @gpio: Gpio whose state need to be set.
1654 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1656 static void _gpio_set_open_drain_value(unsigned gpio
,
1657 struct gpio_chip
*chip
, int value
)
1661 err
= chip
->direction_input(chip
, gpio
- chip
->base
);
1663 clear_bit(FLAG_IS_OUT
, &gpio_desc
[gpio
].flags
);
1665 err
= chip
->direction_output(chip
, gpio
- chip
->base
, 0);
1667 set_bit(FLAG_IS_OUT
, &gpio_desc
[gpio
].flags
);
1669 trace_gpio_direction(gpio
, value
, err
);
1671 pr_err("%s: Error in set_value for open drain gpio%d err %d\n",
1672 __func__
, gpio
, err
);
1676 * _gpio_set_open_source() - Set the open source gpio's value.
1677 * @gpio: Gpio whose state need to be set.
1679 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1681 static void _gpio_set_open_source_value(unsigned gpio
,
1682 struct gpio_chip
*chip
, int value
)
1686 err
= chip
->direction_output(chip
, gpio
- chip
->base
, 1);
1688 set_bit(FLAG_IS_OUT
, &gpio_desc
[gpio
].flags
);
1690 err
= chip
->direction_input(chip
, gpio
- chip
->base
);
1692 clear_bit(FLAG_IS_OUT
, &gpio_desc
[gpio
].flags
);
1694 trace_gpio_direction(gpio
, !value
, err
);
1696 pr_err("%s: Error in set_value for open source gpio%d err %d\n",
1697 __func__
, gpio
, err
);
1702 * __gpio_set_value() - assign a gpio's value
1703 * @gpio: gpio whose value will be assigned
1704 * @value: value to assign
1707 * This is used directly or indirectly to implement gpio_set_value().
1708 * It invokes the associated gpio_chip.set() method.
1710 void __gpio_set_value(unsigned gpio
, int value
)
1712 struct gpio_chip
*chip
;
1714 chip
= gpio_to_chip(gpio
);
1715 /* Should be using gpio_set_value_cansleep() */
1716 WARN_ON(chip
->can_sleep
);
1717 trace_gpio_value(gpio
, 0, value
);
1718 if (test_bit(FLAG_OPEN_DRAIN
, &gpio_desc
[gpio
].flags
))
1719 _gpio_set_open_drain_value(gpio
, chip
, value
);
1720 else if (test_bit(FLAG_OPEN_SOURCE
, &gpio_desc
[gpio
].flags
))
1721 _gpio_set_open_source_value(gpio
, chip
, value
);
1723 chip
->set(chip
, gpio
- chip
->base
, value
);
1725 EXPORT_SYMBOL_GPL(__gpio_set_value
);
1728 * __gpio_cansleep() - report whether gpio value access will sleep
1729 * @gpio: gpio in question
1732 * This is used directly or indirectly to implement gpio_cansleep(). It
1733 * returns nonzero if access reading or writing the GPIO value can sleep.
1735 int __gpio_cansleep(unsigned gpio
)
1737 struct gpio_chip
*chip
;
1739 /* only call this on GPIOs that are valid! */
1740 chip
= gpio_to_chip(gpio
);
1742 return chip
->can_sleep
;
1744 EXPORT_SYMBOL_GPL(__gpio_cansleep
);
1747 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1748 * @gpio: gpio whose IRQ will be returned (already requested)
1751 * This is used directly or indirectly to implement gpio_to_irq().
1752 * It returns the number of the IRQ signaled by this (input) GPIO,
1753 * or a negative errno.
1755 int __gpio_to_irq(unsigned gpio
)
1757 struct gpio_chip
*chip
;
1759 chip
= gpio_to_chip(gpio
);
1760 return chip
->to_irq
? chip
->to_irq(chip
, gpio
- chip
->base
) : -ENXIO
;
1762 EXPORT_SYMBOL_GPL(__gpio_to_irq
);
1766 /* There's no value in making it easy to inline GPIO calls that may sleep.
1767 * Common examples include ones connected to I2C or SPI chips.
1770 int gpio_get_value_cansleep(unsigned gpio
)
1772 struct gpio_chip
*chip
;
1775 might_sleep_if(extra_checks
);
1776 chip
= gpio_to_chip(gpio
);
1777 value
= chip
->get
? chip
->get(chip
, gpio
- chip
->base
) : 0;
1778 trace_gpio_value(gpio
, 1, value
);
1781 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep
);
1783 void gpio_set_value_cansleep(unsigned gpio
, int value
)
1785 struct gpio_chip
*chip
;
1787 might_sleep_if(extra_checks
);
1788 chip
= gpio_to_chip(gpio
);
1789 trace_gpio_value(gpio
, 0, value
);
1790 if (test_bit(FLAG_OPEN_DRAIN
, &gpio_desc
[gpio
].flags
))
1791 _gpio_set_open_drain_value(gpio
, chip
, value
);
1792 else if (test_bit(FLAG_OPEN_SOURCE
, &gpio_desc
[gpio
].flags
))
1793 _gpio_set_open_source_value(gpio
, chip
, value
);
1795 chip
->set(chip
, gpio
- chip
->base
, value
);
1797 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep
);
1800 #ifdef CONFIG_DEBUG_FS
1802 static void gpiolib_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
1805 unsigned gpio
= chip
->base
;
1806 struct gpio_desc
*gdesc
= &gpio_desc
[gpio
];
1809 for (i
= 0; i
< chip
->ngpio
; i
++, gpio
++, gdesc
++) {
1810 if (!test_bit(FLAG_REQUESTED
, &gdesc
->flags
))
1813 is_out
= test_bit(FLAG_IS_OUT
, &gdesc
->flags
);
1814 seq_printf(s
, " gpio-%-3d (%-20.20s) %s %s",
1816 is_out
? "out" : "in ",
1818 ? (chip
->get(chip
, i
) ? "hi" : "lo")
1820 seq_printf(s
, "\n");
1824 static void *gpiolib_seq_start(struct seq_file
*s
, loff_t
*pos
)
1826 struct gpio_chip
*chip
= NULL
;
1831 /* REVISIT this isn't locked against gpio_chip removal ... */
1833 for (gpio
= 0; gpio_is_valid(gpio
); gpio
++) {
1834 if (gpio_desc
[gpio
].chip
== chip
)
1837 chip
= gpio_desc
[gpio
].chip
;
1841 if (index
++ >= *pos
) {
1852 static void *gpiolib_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
1854 struct gpio_chip
*chip
= v
;
1858 /* skip GPIOs provided by the current chip */
1859 for (gpio
= chip
->base
+ chip
->ngpio
; gpio_is_valid(gpio
); gpio
++) {
1860 chip
= gpio_desc
[gpio
].chip
;
1873 static void gpiolib_seq_stop(struct seq_file
*s
, void *v
)
1877 static int gpiolib_seq_show(struct seq_file
*s
, void *v
)
1879 struct gpio_chip
*chip
= v
;
1882 seq_printf(s
, "%sGPIOs %d-%d", (char *)s
->private,
1883 chip
->base
, chip
->base
+ chip
->ngpio
- 1);
1886 seq_printf(s
, ", %s/%s", dev
->bus
? dev
->bus
->name
: "no-bus",
1889 seq_printf(s
, ", %s", chip
->label
);
1890 if (chip
->can_sleep
)
1891 seq_printf(s
, ", can sleep");
1892 seq_printf(s
, ":\n");
1895 chip
->dbg_show(s
, chip
);
1897 gpiolib_dbg_show(s
, chip
);
1902 static const struct seq_operations gpiolib_seq_ops
= {
1903 .start
= gpiolib_seq_start
,
1904 .next
= gpiolib_seq_next
,
1905 .stop
= gpiolib_seq_stop
,
1906 .show
= gpiolib_seq_show
,
1909 static int gpiolib_open(struct inode
*inode
, struct file
*file
)
1911 return seq_open(file
, &gpiolib_seq_ops
);
1914 static const struct file_operations gpiolib_operations
= {
1915 .owner
= THIS_MODULE
,
1916 .open
= gpiolib_open
,
1918 .llseek
= seq_lseek
,
1919 .release
= seq_release
,
1922 static int __init
gpiolib_debugfs_init(void)
1924 /* /sys/kernel/debug/gpio */
1925 (void) debugfs_create_file("gpio", S_IFREG
| S_IRUGO
,
1926 NULL
, NULL
, &gpiolib_operations
);
1929 subsys_initcall(gpiolib_debugfs_init
);
1931 #endif /* DEBUG_FS */