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/idr.h>
14 /* Optional implementation infrastructure for GPIO interfaces.
16 * Platforms may want to use this if they tend to use very many GPIOs
17 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
19 * When kernel footprint or instruction count is an issue, simpler
20 * implementations may be preferred. The GPIO programming interface
21 * allows for inlining speed-critical get/set operations for common
22 * cases, so that access to SOC-integrated GPIOs can sometimes cost
23 * only an instruction or two per bit.
27 /* When debugging, extend minimal trust to callers and platform code.
28 * Also emit diagnostic messages that may help initial bringup, when
29 * board setup or driver bugs are most common.
31 * Otherwise, minimize overhead in what may be bitbanging codepaths.
34 #define extra_checks 1
36 #define extra_checks 0
39 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
40 * While any GPIO is requested, its gpio_chip is not removable;
41 * each GPIO's "requested" flag serves as a lock and refcount.
43 static DEFINE_SPINLOCK(gpio_lock
);
46 struct gpio_chip
*chip
;
48 /* flag symbols are bit numbers */
49 #define FLAG_REQUESTED 0
51 #define FLAG_RESERVED 2
52 #define FLAG_EXPORT 3 /* protected by sysfs_lock */
53 #define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */
54 #define FLAG_TRIG_FALL 5 /* trigger on falling edge */
55 #define FLAG_TRIG_RISE 6 /* trigger on rising edge */
57 #define PDESC_ID_SHIFT 16 /* add new flags before this one */
59 #define GPIO_FLAGS_MASK ((1 << PDESC_ID_SHIFT) - 1)
60 #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
62 #ifdef CONFIG_DEBUG_FS
66 static struct gpio_desc gpio_desc
[ARCH_NR_GPIOS
];
68 #ifdef CONFIG_GPIO_SYSFS
70 struct work_struct work
;
71 struct sysfs_dirent
*value_sd
;
74 static struct idr pdesc_idr
;
77 static inline void desc_set_label(struct gpio_desc
*d
, const char *label
)
79 #ifdef CONFIG_DEBUG_FS
84 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
85 * when setting direction, and otherwise illegal. Until board setup code
86 * and drivers use explicit requests everywhere (which won't happen when
87 * those calls have no teeth) we can't avoid autorequesting. This nag
88 * message should motivate switching to explicit requests... so should
89 * the weaker cleanup after faults, compared to gpio_request().
91 * NOTE: the autorequest mechanism is going away; at this point it's
92 * only "legal" in the sense that (old) code using it won't break yet,
93 * but instead only triggers a WARN() stack dump.
95 static int gpio_ensure_requested(struct gpio_desc
*desc
, unsigned offset
)
97 const struct gpio_chip
*chip
= desc
->chip
;
98 const int gpio
= chip
->base
+ offset
;
100 if (WARN(test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0,
101 "autorequest GPIO-%d\n", gpio
)) {
102 if (!try_module_get(chip
->owner
)) {
103 pr_err("GPIO-%d: module can't be gotten \n", gpio
);
104 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
108 desc_set_label(desc
, "[auto]");
109 /* caller must chip->request() w/o spinlock */
116 /* caller holds gpio_lock *OR* gpio is marked as requested */
117 static inline struct gpio_chip
*gpio_to_chip(unsigned gpio
)
119 return gpio_desc
[gpio
].chip
;
122 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
123 static int gpiochip_find_base(int ngpio
)
129 for (i
= ARCH_NR_GPIOS
- 1; i
>= 0 ; i
--) {
130 struct gpio_desc
*desc
= &gpio_desc
[i
];
131 struct gpio_chip
*chip
= desc
->chip
;
133 if (!chip
&& !test_bit(FLAG_RESERVED
, &desc
->flags
)) {
135 if (spare
== ngpio
) {
142 i
-= chip
->ngpio
- 1;
146 if (gpio_is_valid(base
))
147 pr_debug("%s: found new base at %d\n", __func__
, base
);
152 * gpiochip_reserve() - reserve range of gpios to use with platform code only
153 * @start: starting gpio number
154 * @ngpio: number of gpios to reserve
155 * Context: platform init, potentially before irqs or kmalloc will work
157 * Returns a negative errno if any gpio within the range is already reserved
158 * or registered, else returns zero as a success code. Use this function
159 * to mark a range of gpios as unavailable for dynamic gpio number allocation,
160 * for example because its driver support is not yet loaded.
162 int __init
gpiochip_reserve(int start
, int ngpio
)
168 if (!gpio_is_valid(start
) || !gpio_is_valid(start
+ ngpio
- 1))
171 spin_lock_irqsave(&gpio_lock
, flags
);
173 for (i
= start
; i
< start
+ ngpio
; i
++) {
174 struct gpio_desc
*desc
= &gpio_desc
[i
];
176 if (desc
->chip
|| test_bit(FLAG_RESERVED
, &desc
->flags
)) {
181 set_bit(FLAG_RESERVED
, &desc
->flags
);
184 pr_debug("%s: reserved gpios from %d to %d\n",
185 __func__
, start
, start
+ ngpio
- 1);
187 spin_unlock_irqrestore(&gpio_lock
, flags
);
192 #ifdef CONFIG_GPIO_SYSFS
194 /* lock protects against unexport_gpio() being called while
195 * sysfs files are active.
197 static DEFINE_MUTEX(sysfs_lock
);
200 * /sys/class/gpio/gpioN... only for GPIOs that are exported
202 * * MAY BE OMITTED if kernel won't allow direction changes
203 * * is read/write as "in" or "out"
204 * * may also be written as "high" or "low", initializing
205 * output value as specified ("out" implies "low")
207 * * always readable, subject to hardware behavior
208 * * may be writable, as zero/nonzero
210 * * configures behavior of poll(2) on /value
211 * * available only if pin can generate IRQs on input
212 * * is read/write as "none", "falling", "rising", or "both"
215 static ssize_t
gpio_direction_show(struct device
*dev
,
216 struct device_attribute
*attr
, char *buf
)
218 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
221 mutex_lock(&sysfs_lock
);
223 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
226 status
= sprintf(buf
, "%s\n",
227 test_bit(FLAG_IS_OUT
, &desc
->flags
)
230 mutex_unlock(&sysfs_lock
);
234 static ssize_t
gpio_direction_store(struct device
*dev
,
235 struct device_attribute
*attr
, const char *buf
, size_t size
)
237 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
238 unsigned gpio
= desc
- gpio_desc
;
241 mutex_lock(&sysfs_lock
);
243 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
245 else if (sysfs_streq(buf
, "high"))
246 status
= gpio_direction_output(gpio
, 1);
247 else if (sysfs_streq(buf
, "out") || sysfs_streq(buf
, "low"))
248 status
= gpio_direction_output(gpio
, 0);
249 else if (sysfs_streq(buf
, "in"))
250 status
= gpio_direction_input(gpio
);
254 mutex_unlock(&sysfs_lock
);
255 return status
? : size
;
258 static const DEVICE_ATTR(direction
, 0644,
259 gpio_direction_show
, gpio_direction_store
);
261 static ssize_t
gpio_value_show(struct device
*dev
,
262 struct device_attribute
*attr
, char *buf
)
264 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
265 unsigned gpio
= desc
- gpio_desc
;
268 mutex_lock(&sysfs_lock
);
270 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
273 status
= sprintf(buf
, "%d\n", !!gpio_get_value_cansleep(gpio
));
275 mutex_unlock(&sysfs_lock
);
279 static ssize_t
gpio_value_store(struct device
*dev
,
280 struct device_attribute
*attr
, const char *buf
, size_t size
)
282 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
283 unsigned gpio
= desc
- gpio_desc
;
286 mutex_lock(&sysfs_lock
);
288 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
290 else if (!test_bit(FLAG_IS_OUT
, &desc
->flags
))
295 status
= strict_strtol(buf
, 0, &value
);
297 gpio_set_value_cansleep(gpio
, value
!= 0);
302 mutex_unlock(&sysfs_lock
);
306 static /*const*/ DEVICE_ATTR(value
, 0644,
307 gpio_value_show
, gpio_value_store
);
309 static irqreturn_t
gpio_sysfs_irq(int irq
, void *priv
)
311 struct work_struct
*work
= priv
;
317 static void gpio_notify_sysfs(struct work_struct
*work
)
319 struct poll_desc
*pdesc
;
321 pdesc
= container_of(work
, struct poll_desc
, work
);
322 sysfs_notify_dirent(pdesc
->value_sd
);
325 static int gpio_setup_irq(struct gpio_desc
*desc
, struct device
*dev
,
326 unsigned long gpio_flags
)
328 struct poll_desc
*pdesc
;
329 unsigned long irq_flags
;
332 if ((desc
->flags
& GPIO_TRIGGER_MASK
) == gpio_flags
)
335 irq
= gpio_to_irq(desc
- gpio_desc
);
339 id
= desc
->flags
>> PDESC_ID_SHIFT
;
340 pdesc
= idr_find(&pdesc_idr
, id
);
342 free_irq(irq
, &pdesc
->work
);
343 cancel_work_sync(&pdesc
->work
);
346 desc
->flags
&= ~GPIO_TRIGGER_MASK
;
353 irq_flags
= IRQF_SHARED
;
354 if (test_bit(FLAG_TRIG_FALL
, &gpio_flags
))
355 irq_flags
|= IRQF_TRIGGER_FALLING
;
356 if (test_bit(FLAG_TRIG_RISE
, &gpio_flags
))
357 irq_flags
|= IRQF_TRIGGER_RISING
;
360 pdesc
= kmalloc(sizeof(*pdesc
), GFP_KERNEL
);
368 if (idr_pre_get(&pdesc_idr
, GFP_KERNEL
))
369 ret
= idr_get_new_above(&pdesc_idr
,
371 } while (ret
== -EAGAIN
);
376 desc
->flags
&= GPIO_FLAGS_MASK
;
377 desc
->flags
|= (unsigned long)id
<< PDESC_ID_SHIFT
;
379 if (desc
->flags
>> PDESC_ID_SHIFT
!= id
) {
384 pdesc
->value_sd
= sysfs_get_dirent(dev
->kobj
.sd
, "value");
385 if (!pdesc
->value_sd
) {
389 INIT_WORK(&pdesc
->work
, gpio_notify_sysfs
);
392 ret
= request_irq(irq
, gpio_sysfs_irq
, irq_flags
,
393 "gpiolib", &pdesc
->work
);
397 desc
->flags
|= gpio_flags
;
401 sysfs_put(pdesc
->value_sd
);
403 idr_remove(&pdesc_idr
, id
);
404 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 const struct attribute
*gpio_attrs
[] = {
479 &dev_attr_direction
.attr
,
480 &dev_attr_value
.attr
,
484 static const struct attribute_group gpio_attr_group
= {
485 .attrs
= (struct attribute
**) gpio_attrs
,
489 * /sys/class/gpio/gpiochipN/
490 * /base ... matching gpio_chip.base (N)
491 * /label ... matching gpio_chip.label
492 * /ngpio ... matching gpio_chip.ngpio
495 static ssize_t
chip_base_show(struct device
*dev
,
496 struct device_attribute
*attr
, char *buf
)
498 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
500 return sprintf(buf
, "%d\n", chip
->base
);
502 static DEVICE_ATTR(base
, 0444, chip_base_show
, NULL
);
504 static ssize_t
chip_label_show(struct device
*dev
,
505 struct device_attribute
*attr
, char *buf
)
507 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
509 return sprintf(buf
, "%s\n", chip
->label
? : "");
511 static DEVICE_ATTR(label
, 0444, chip_label_show
, NULL
);
513 static ssize_t
chip_ngpio_show(struct device
*dev
,
514 struct device_attribute
*attr
, char *buf
)
516 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
518 return sprintf(buf
, "%u\n", chip
->ngpio
);
520 static DEVICE_ATTR(ngpio
, 0444, chip_ngpio_show
, NULL
);
522 static const struct attribute
*gpiochip_attrs
[] = {
524 &dev_attr_label
.attr
,
525 &dev_attr_ngpio
.attr
,
529 static const struct attribute_group gpiochip_attr_group
= {
530 .attrs
= (struct attribute
**) gpiochip_attrs
,
534 * /sys/class/gpio/export ... write-only
535 * integer N ... number of GPIO to export (full access)
536 * /sys/class/gpio/unexport ... write-only
537 * integer N ... number of GPIO to unexport
539 static ssize_t
export_store(struct class *class, const char *buf
, size_t len
)
544 status
= strict_strtol(buf
, 0, &gpio
);
548 /* No extra locking here; FLAG_SYSFS just signifies that the
549 * request and export were done by on behalf of userspace, so
550 * they may be undone on its behalf too.
553 status
= gpio_request(gpio
, "sysfs");
557 status
= gpio_export(gpio
, true);
561 set_bit(FLAG_SYSFS
, &gpio_desc
[gpio
].flags
);
565 pr_debug("%s: status %d\n", __func__
, status
);
566 return status
? : len
;
569 static ssize_t
unexport_store(struct class *class, const char *buf
, size_t len
)
574 status
= strict_strtol(buf
, 0, &gpio
);
580 /* reject bogus commands (gpio_unexport ignores them) */
581 if (!gpio_is_valid(gpio
))
584 /* No extra locking here; FLAG_SYSFS just signifies that the
585 * request and export were done by on behalf of userspace, so
586 * they may be undone on its behalf too.
588 if (test_and_clear_bit(FLAG_SYSFS
, &gpio_desc
[gpio
].flags
)) {
594 pr_debug("%s: status %d\n", __func__
, status
);
595 return status
? : len
;
598 static struct class_attribute gpio_class_attrs
[] = {
599 __ATTR(export
, 0200, NULL
, export_store
),
600 __ATTR(unexport
, 0200, NULL
, unexport_store
),
604 static struct class gpio_class
= {
606 .owner
= THIS_MODULE
,
608 .class_attrs
= gpio_class_attrs
,
613 * gpio_export - export a GPIO through sysfs
614 * @gpio: gpio to make available, already requested
615 * @direction_may_change: true if userspace may change gpio direction
616 * Context: arch_initcall or later
618 * When drivers want to make a GPIO accessible to userspace after they
619 * have requested it -- perhaps while debugging, or as part of their
620 * public interface -- they may use this routine. If the GPIO can
621 * change direction (some can't) and the caller allows it, userspace
622 * will see "direction" sysfs attribute which may be used to change
623 * the gpio's direction. A "value" attribute will always be provided.
625 * Returns zero on success, else an error.
627 int gpio_export(unsigned gpio
, bool direction_may_change
)
630 struct gpio_desc
*desc
;
631 int status
= -EINVAL
;
634 /* can't export until sysfs is available ... */
636 pr_debug("%s: called too early!\n", __func__
);
640 if (!gpio_is_valid(gpio
))
643 mutex_lock(&sysfs_lock
);
645 spin_lock_irqsave(&gpio_lock
, flags
);
646 desc
= &gpio_desc
[gpio
];
647 if (test_bit(FLAG_REQUESTED
, &desc
->flags
)
648 && !test_bit(FLAG_EXPORT
, &desc
->flags
)) {
650 if (!desc
->chip
->direction_input
651 || !desc
->chip
->direction_output
)
652 direction_may_change
= false;
654 spin_unlock_irqrestore(&gpio_lock
, flags
);
656 if (desc
->chip
->names
&& desc
->chip
->names
[gpio
- desc
->chip
->base
])
657 ioname
= desc
->chip
->names
[gpio
- desc
->chip
->base
];
662 dev
= device_create(&gpio_class
, desc
->chip
->dev
, MKDEV(0, 0),
663 desc
, ioname
? ioname
: "gpio%d", gpio
);
665 if (direction_may_change
)
666 status
= sysfs_create_group(&dev
->kobj
,
669 status
= device_create_file(dev
,
672 if (!status
&& gpio_to_irq(gpio
) >= 0
673 && (direction_may_change
674 || !test_bit(FLAG_IS_OUT
,
676 status
= device_create_file(dev
,
680 device_unregister(dev
);
684 set_bit(FLAG_EXPORT
, &desc
->flags
);
687 mutex_unlock(&sysfs_lock
);
691 pr_debug("%s: gpio%d status %d\n", __func__
, gpio
, status
);
695 EXPORT_SYMBOL_GPL(gpio_export
);
697 static int match_export(struct device
*dev
, void *data
)
699 return dev_get_drvdata(dev
) == data
;
703 * gpio_export_link - create a sysfs link to an exported GPIO node
704 * @dev: device under which to create symlink
705 * @name: name of the symlink
706 * @gpio: gpio to create symlink to, already exported
708 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
709 * node. Caller is responsible for unlinking.
711 * Returns zero on success, else an error.
713 int gpio_export_link(struct device
*dev
, const char *name
, unsigned gpio
)
715 struct gpio_desc
*desc
;
716 int status
= -EINVAL
;
718 if (!gpio_is_valid(gpio
))
721 mutex_lock(&sysfs_lock
);
723 desc
= &gpio_desc
[gpio
];
725 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
728 tdev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
730 status
= sysfs_create_link(&dev
->kobj
, &tdev
->kobj
,
737 mutex_unlock(&sysfs_lock
);
741 pr_debug("%s: gpio%d status %d\n", __func__
, gpio
, status
);
745 EXPORT_SYMBOL_GPL(gpio_export_link
);
748 * gpio_unexport - reverse effect of gpio_export()
749 * @gpio: gpio to make unavailable
751 * This is implicit on gpio_free().
753 void gpio_unexport(unsigned gpio
)
755 struct gpio_desc
*desc
;
756 int status
= -EINVAL
;
758 if (!gpio_is_valid(gpio
))
761 mutex_lock(&sysfs_lock
);
763 desc
= &gpio_desc
[gpio
];
765 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
766 struct device
*dev
= NULL
;
768 dev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
770 gpio_setup_irq(desc
, dev
, 0);
771 clear_bit(FLAG_EXPORT
, &desc
->flags
);
773 device_unregister(dev
);
779 mutex_unlock(&sysfs_lock
);
782 pr_debug("%s: gpio%d status %d\n", __func__
, gpio
, status
);
784 EXPORT_SYMBOL_GPL(gpio_unexport
);
786 static int gpiochip_export(struct gpio_chip
*chip
)
791 /* Many systems register gpio chips for SOC support very early,
792 * before driver model support is available. In those cases we
793 * export this later, in gpiolib_sysfs_init() ... here we just
794 * verify that _some_ field of gpio_class got initialized.
799 /* use chip->base for the ID; it's already known to be unique */
800 mutex_lock(&sysfs_lock
);
801 dev
= device_create(&gpio_class
, chip
->dev
, MKDEV(0, 0), chip
,
802 "gpiochip%d", chip
->base
);
804 status
= sysfs_create_group(&dev
->kobj
,
805 &gpiochip_attr_group
);
808 chip
->exported
= (status
== 0);
809 mutex_unlock(&sysfs_lock
);
815 spin_lock_irqsave(&gpio_lock
, flags
);
817 while (gpio_desc
[gpio
].chip
== chip
)
818 gpio_desc
[gpio
++].chip
= NULL
;
819 spin_unlock_irqrestore(&gpio_lock
, flags
);
821 pr_debug("%s: chip %s status %d\n", __func__
,
822 chip
->label
, status
);
828 static void gpiochip_unexport(struct gpio_chip
*chip
)
833 mutex_lock(&sysfs_lock
);
834 dev
= class_find_device(&gpio_class
, NULL
, chip
, match_export
);
837 device_unregister(dev
);
842 mutex_unlock(&sysfs_lock
);
845 pr_debug("%s: chip %s status %d\n", __func__
,
846 chip
->label
, status
);
849 static int __init
gpiolib_sysfs_init(void)
855 idr_init(&pdesc_idr
);
857 status
= class_register(&gpio_class
);
861 /* Scan and register the gpio_chips which registered very
862 * early (e.g. before the class_register above was called).
864 * We run before arch_initcall() so chip->dev nodes can have
865 * registered, and so arch_initcall() can always gpio_export().
867 spin_lock_irqsave(&gpio_lock
, flags
);
868 for (gpio
= 0; gpio
< ARCH_NR_GPIOS
; gpio
++) {
869 struct gpio_chip
*chip
;
871 chip
= gpio_desc
[gpio
].chip
;
872 if (!chip
|| chip
->exported
)
875 spin_unlock_irqrestore(&gpio_lock
, flags
);
876 status
= gpiochip_export(chip
);
877 spin_lock_irqsave(&gpio_lock
, flags
);
879 spin_unlock_irqrestore(&gpio_lock
, flags
);
884 postcore_initcall(gpiolib_sysfs_init
);
887 static inline int gpiochip_export(struct gpio_chip
*chip
)
892 static inline void gpiochip_unexport(struct gpio_chip
*chip
)
896 #endif /* CONFIG_GPIO_SYSFS */
899 * gpiochip_add() - register a gpio_chip
900 * @chip: the chip to register, with chip->base initialized
901 * Context: potentially before irqs or kmalloc will work
903 * Returns a negative errno if the chip can't be registered, such as
904 * because the chip->base is invalid or already associated with a
905 * different chip. Otherwise it returns zero as a success code.
907 * When gpiochip_add() is called very early during boot, so that GPIOs
908 * can be freely used, the chip->dev device must be registered before
909 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
910 * for GPIOs will fail rudely.
912 * If chip->base is negative, this requests dynamic assignment of
913 * a range of valid GPIOs.
915 int gpiochip_add(struct gpio_chip
*chip
)
920 int base
= chip
->base
;
922 if ((!gpio_is_valid(base
) || !gpio_is_valid(base
+ chip
->ngpio
- 1))
928 spin_lock_irqsave(&gpio_lock
, flags
);
931 base
= gpiochip_find_base(chip
->ngpio
);
939 /* these GPIO numbers must not be managed by another gpio_chip */
940 for (id
= base
; id
< base
+ chip
->ngpio
; id
++) {
941 if (gpio_desc
[id
].chip
!= NULL
) {
947 for (id
= base
; id
< base
+ chip
->ngpio
; id
++) {
948 gpio_desc
[id
].chip
= chip
;
950 /* REVISIT: most hardware initializes GPIOs as
951 * inputs (often with pullups enabled) so power
952 * usage is minimized. Linux code should set the
953 * gpio direction first thing; but until it does,
954 * we may expose the wrong direction in sysfs.
956 gpio_desc
[id
].flags
= !chip
->direction_input
963 spin_unlock_irqrestore(&gpio_lock
, flags
);
965 status
= gpiochip_export(chip
);
967 /* failures here can mean systems won't boot... */
969 pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n",
970 chip
->base
, chip
->base
+ chip
->ngpio
- 1,
971 chip
->label
? : "generic");
974 EXPORT_SYMBOL_GPL(gpiochip_add
);
977 * gpiochip_remove() - unregister a gpio_chip
978 * @chip: the chip to unregister
980 * A gpio_chip with any GPIOs still requested may not be removed.
982 int gpiochip_remove(struct gpio_chip
*chip
)
988 spin_lock_irqsave(&gpio_lock
, flags
);
990 for (id
= chip
->base
; id
< chip
->base
+ chip
->ngpio
; id
++) {
991 if (test_bit(FLAG_REQUESTED
, &gpio_desc
[id
].flags
)) {
997 for (id
= chip
->base
; id
< chip
->base
+ chip
->ngpio
; id
++)
998 gpio_desc
[id
].chip
= NULL
;
1001 spin_unlock_irqrestore(&gpio_lock
, flags
);
1004 gpiochip_unexport(chip
);
1008 EXPORT_SYMBOL_GPL(gpiochip_remove
);
1011 /* These "optional" allocation calls help prevent drivers from stomping
1012 * on each other, and help provide better diagnostics in debugfs.
1013 * They're called even less than the "set direction" calls.
1015 int gpio_request(unsigned gpio
, const char *label
)
1017 struct gpio_desc
*desc
;
1018 struct gpio_chip
*chip
;
1019 int status
= -EINVAL
;
1020 unsigned long flags
;
1022 spin_lock_irqsave(&gpio_lock
, flags
);
1024 if (!gpio_is_valid(gpio
))
1026 desc
= &gpio_desc
[gpio
];
1031 if (!try_module_get(chip
->owner
))
1034 /* NOTE: gpio_request() can be called in early boot,
1035 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1038 if (test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0) {
1039 desc_set_label(desc
, label
? : "?");
1043 module_put(chip
->owner
);
1047 if (chip
->request
) {
1048 /* chip->request may sleep */
1049 spin_unlock_irqrestore(&gpio_lock
, flags
);
1050 status
= chip
->request(chip
, gpio
- chip
->base
);
1051 spin_lock_irqsave(&gpio_lock
, flags
);
1054 desc_set_label(desc
, NULL
);
1055 module_put(chip
->owner
);
1056 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
1062 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
1063 gpio
, label
? : "?", status
);
1064 spin_unlock_irqrestore(&gpio_lock
, flags
);
1067 EXPORT_SYMBOL_GPL(gpio_request
);
1069 void gpio_free(unsigned gpio
)
1071 unsigned long flags
;
1072 struct gpio_desc
*desc
;
1073 struct gpio_chip
*chip
;
1077 if (!gpio_is_valid(gpio
)) {
1078 WARN_ON(extra_checks
);
1082 gpio_unexport(gpio
);
1084 spin_lock_irqsave(&gpio_lock
, flags
);
1086 desc
= &gpio_desc
[gpio
];
1088 if (chip
&& test_bit(FLAG_REQUESTED
, &desc
->flags
)) {
1090 spin_unlock_irqrestore(&gpio_lock
, flags
);
1091 might_sleep_if(extra_checks
&& chip
->can_sleep
);
1092 chip
->free(chip
, gpio
- chip
->base
);
1093 spin_lock_irqsave(&gpio_lock
, flags
);
1095 desc_set_label(desc
, NULL
);
1096 module_put(desc
->chip
->owner
);
1097 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
1099 WARN_ON(extra_checks
);
1101 spin_unlock_irqrestore(&gpio_lock
, flags
);
1103 EXPORT_SYMBOL_GPL(gpio_free
);
1107 * gpiochip_is_requested - return string iff signal was requested
1108 * @chip: controller managing the signal
1109 * @offset: of signal within controller's 0..(ngpio - 1) range
1111 * Returns NULL if the GPIO is not currently requested, else a string.
1112 * If debugfs support is enabled, the string returned is the label passed
1113 * to gpio_request(); otherwise it is a meaningless constant.
1115 * This function is for use by GPIO controller drivers. The label can
1116 * help with diagnostics, and knowing that the signal is used as a GPIO
1117 * can help avoid accidentally multiplexing it to another controller.
1119 const char *gpiochip_is_requested(struct gpio_chip
*chip
, unsigned offset
)
1121 unsigned gpio
= chip
->base
+ offset
;
1123 if (!gpio_is_valid(gpio
) || gpio_desc
[gpio
].chip
!= chip
)
1125 if (test_bit(FLAG_REQUESTED
, &gpio_desc
[gpio
].flags
) == 0)
1127 #ifdef CONFIG_DEBUG_FS
1128 return gpio_desc
[gpio
].label
;
1133 EXPORT_SYMBOL_GPL(gpiochip_is_requested
);
1136 /* Drivers MUST set GPIO direction before making get/set calls. In
1137 * some cases this is done in early boot, before IRQs are enabled.
1139 * As a rule these aren't called more than once (except for drivers
1140 * using the open-drain emulation idiom) so these are natural places
1141 * to accumulate extra debugging checks. Note that we can't (yet)
1142 * rely on gpio_request() having been called beforehand.
1145 int gpio_direction_input(unsigned gpio
)
1147 unsigned long flags
;
1148 struct gpio_chip
*chip
;
1149 struct gpio_desc
*desc
= &gpio_desc
[gpio
];
1150 int status
= -EINVAL
;
1152 spin_lock_irqsave(&gpio_lock
, flags
);
1154 if (!gpio_is_valid(gpio
))
1157 if (!chip
|| !chip
->get
|| !chip
->direction_input
)
1160 if (gpio
>= chip
->ngpio
)
1162 status
= gpio_ensure_requested(desc
, gpio
);
1166 /* now we know the gpio is valid and chip won't vanish */
1168 spin_unlock_irqrestore(&gpio_lock
, flags
);
1170 might_sleep_if(extra_checks
&& chip
->can_sleep
);
1173 status
= chip
->request(chip
, gpio
);
1175 pr_debug("GPIO-%d: chip request fail, %d\n",
1176 chip
->base
+ gpio
, status
);
1177 /* and it's not available to anyone else ...
1178 * gpio_request() is the fully clean solution.
1184 status
= chip
->direction_input(chip
, gpio
);
1186 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1190 spin_unlock_irqrestore(&gpio_lock
, flags
);
1192 pr_debug("%s: gpio-%d status %d\n",
1193 __func__
, gpio
, status
);
1196 EXPORT_SYMBOL_GPL(gpio_direction_input
);
1198 int gpio_direction_output(unsigned gpio
, int value
)
1200 unsigned long flags
;
1201 struct gpio_chip
*chip
;
1202 struct gpio_desc
*desc
= &gpio_desc
[gpio
];
1203 int status
= -EINVAL
;
1205 spin_lock_irqsave(&gpio_lock
, flags
);
1207 if (!gpio_is_valid(gpio
))
1210 if (!chip
|| !chip
->set
|| !chip
->direction_output
)
1213 if (gpio
>= chip
->ngpio
)
1215 status
= gpio_ensure_requested(desc
, gpio
);
1219 /* now we know the gpio is valid and chip won't vanish */
1221 spin_unlock_irqrestore(&gpio_lock
, flags
);
1223 might_sleep_if(extra_checks
&& chip
->can_sleep
);
1226 status
= chip
->request(chip
, gpio
);
1228 pr_debug("GPIO-%d: chip request fail, %d\n",
1229 chip
->base
+ gpio
, status
);
1230 /* and it's not available to anyone else ...
1231 * gpio_request() is the fully clean solution.
1237 status
= chip
->direction_output(chip
, gpio
, value
);
1239 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1243 spin_unlock_irqrestore(&gpio_lock
, flags
);
1245 pr_debug("%s: gpio-%d status %d\n",
1246 __func__
, gpio
, status
);
1249 EXPORT_SYMBOL_GPL(gpio_direction_output
);
1252 /* I/O calls are only valid after configuration completed; the relevant
1253 * "is this a valid GPIO" error checks should already have been done.
1255 * "Get" operations are often inlinable as reading a pin value register,
1256 * and masking the relevant bit in that register.
1258 * When "set" operations are inlinable, they involve writing that mask to
1259 * one register to set a low value, or a different register to set it high.
1260 * Otherwise locking is needed, so there may be little value to inlining.
1262 *------------------------------------------------------------------------
1264 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1265 * have requested the GPIO. That can include implicit requesting by
1266 * a direction setting call. Marking a gpio as requested locks its chip
1267 * in memory, guaranteeing that these table lookups need no more locking
1268 * and that gpiochip_remove() will fail.
1270 * REVISIT when debugging, consider adding some instrumentation to ensure
1271 * that the GPIO was actually requested.
1275 * __gpio_get_value() - return a gpio's value
1276 * @gpio: gpio whose value will be returned
1279 * This is used directly or indirectly to implement gpio_get_value().
1280 * It returns the zero or nonzero value provided by the associated
1281 * gpio_chip.get() method; or zero if no such method is provided.
1283 int __gpio_get_value(unsigned gpio
)
1285 struct gpio_chip
*chip
;
1287 chip
= gpio_to_chip(gpio
);
1288 WARN_ON(extra_checks
&& chip
->can_sleep
);
1289 return chip
->get
? chip
->get(chip
, gpio
- chip
->base
) : 0;
1291 EXPORT_SYMBOL_GPL(__gpio_get_value
);
1294 * __gpio_set_value() - assign a gpio's value
1295 * @gpio: gpio whose value will be assigned
1296 * @value: value to assign
1299 * This is used directly or indirectly to implement gpio_set_value().
1300 * It invokes the associated gpio_chip.set() method.
1302 void __gpio_set_value(unsigned gpio
, int value
)
1304 struct gpio_chip
*chip
;
1306 chip
= gpio_to_chip(gpio
);
1307 WARN_ON(extra_checks
&& chip
->can_sleep
);
1308 chip
->set(chip
, gpio
- chip
->base
, value
);
1310 EXPORT_SYMBOL_GPL(__gpio_set_value
);
1313 * __gpio_cansleep() - report whether gpio value access will sleep
1314 * @gpio: gpio in question
1317 * This is used directly or indirectly to implement gpio_cansleep(). It
1318 * returns nonzero if access reading or writing the GPIO value can sleep.
1320 int __gpio_cansleep(unsigned gpio
)
1322 struct gpio_chip
*chip
;
1324 /* only call this on GPIOs that are valid! */
1325 chip
= gpio_to_chip(gpio
);
1327 return chip
->can_sleep
;
1329 EXPORT_SYMBOL_GPL(__gpio_cansleep
);
1332 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1333 * @gpio: gpio whose IRQ will be returned (already requested)
1336 * This is used directly or indirectly to implement gpio_to_irq().
1337 * It returns the number of the IRQ signaled by this (input) GPIO,
1338 * or a negative errno.
1340 int __gpio_to_irq(unsigned gpio
)
1342 struct gpio_chip
*chip
;
1344 chip
= gpio_to_chip(gpio
);
1345 return chip
->to_irq
? chip
->to_irq(chip
, gpio
- chip
->base
) : -ENXIO
;
1347 EXPORT_SYMBOL_GPL(__gpio_to_irq
);
1351 /* There's no value in making it easy to inline GPIO calls that may sleep.
1352 * Common examples include ones connected to I2C or SPI chips.
1355 int gpio_get_value_cansleep(unsigned gpio
)
1357 struct gpio_chip
*chip
;
1359 might_sleep_if(extra_checks
);
1360 chip
= gpio_to_chip(gpio
);
1361 return chip
->get
? chip
->get(chip
, gpio
- chip
->base
) : 0;
1363 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep
);
1365 void gpio_set_value_cansleep(unsigned gpio
, int value
)
1367 struct gpio_chip
*chip
;
1369 might_sleep_if(extra_checks
);
1370 chip
= gpio_to_chip(gpio
);
1371 chip
->set(chip
, gpio
- chip
->base
, value
);
1373 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep
);
1376 #ifdef CONFIG_DEBUG_FS
1378 static void gpiolib_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
1381 unsigned gpio
= chip
->base
;
1382 struct gpio_desc
*gdesc
= &gpio_desc
[gpio
];
1385 for (i
= 0; i
< chip
->ngpio
; i
++, gpio
++, gdesc
++) {
1386 if (!test_bit(FLAG_REQUESTED
, &gdesc
->flags
))
1389 is_out
= test_bit(FLAG_IS_OUT
, &gdesc
->flags
);
1390 seq_printf(s
, " gpio-%-3d (%-20.20s) %s %s",
1392 is_out
? "out" : "in ",
1394 ? (chip
->get(chip
, i
) ? "hi" : "lo")
1398 int irq
= gpio_to_irq(gpio
);
1399 struct irq_desc
*desc
= irq_to_desc(irq
);
1401 /* This races with request_irq(), set_irq_type(),
1402 * and set_irq_wake() ... but those are "rare".
1404 * More significantly, trigger type flags aren't
1405 * currently maintained by genirq.
1407 if (irq
>= 0 && desc
->action
) {
1410 switch (desc
->status
& IRQ_TYPE_SENSE_MASK
) {
1412 trigger
= "(default)";
1414 case IRQ_TYPE_EDGE_FALLING
:
1415 trigger
= "edge-falling";
1417 case IRQ_TYPE_EDGE_RISING
:
1418 trigger
= "edge-rising";
1420 case IRQ_TYPE_EDGE_BOTH
:
1421 trigger
= "edge-both";
1423 case IRQ_TYPE_LEVEL_HIGH
:
1424 trigger
= "level-high";
1426 case IRQ_TYPE_LEVEL_LOW
:
1427 trigger
= "level-low";
1430 trigger
= "?trigger?";
1434 seq_printf(s
, " irq-%d %s%s",
1436 (desc
->status
& IRQ_WAKEUP
)
1441 seq_printf(s
, "\n");
1445 static int gpiolib_show(struct seq_file
*s
, void *unused
)
1447 struct gpio_chip
*chip
= NULL
;
1451 /* REVISIT this isn't locked against gpio_chip removal ... */
1453 for (gpio
= 0; gpio_is_valid(gpio
); gpio
++) {
1456 if (chip
== gpio_desc
[gpio
].chip
)
1458 chip
= gpio_desc
[gpio
].chip
;
1462 seq_printf(s
, "%sGPIOs %d-%d",
1463 started
? "\n" : "",
1464 chip
->base
, chip
->base
+ chip
->ngpio
- 1);
1467 seq_printf(s
, ", %s/%s",
1468 dev
->bus
? dev
->bus
->name
: "no-bus",
1471 seq_printf(s
, ", %s", chip
->label
);
1472 if (chip
->can_sleep
)
1473 seq_printf(s
, ", can sleep");
1474 seq_printf(s
, ":\n");
1478 chip
->dbg_show(s
, chip
);
1480 gpiolib_dbg_show(s
, chip
);
1485 static int gpiolib_open(struct inode
*inode
, struct file
*file
)
1487 return single_open(file
, gpiolib_show
, NULL
);
1490 static struct file_operations gpiolib_operations
= {
1491 .open
= gpiolib_open
,
1493 .llseek
= seq_lseek
,
1494 .release
= single_release
,
1497 static int __init
gpiolib_debugfs_init(void)
1499 /* /sys/kernel/debug/gpio */
1500 (void) debugfs_create_file("gpio", S_IFREG
| S_IRUGO
,
1501 NULL
, NULL
, &gpiolib_operations
);
1504 subsys_initcall(gpiolib_debugfs_init
);
1506 #endif /* DEBUG_FS */