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
493 * /names ... matching gpio_chip.names
496 static ssize_t
chip_base_show(struct device
*dev
,
497 struct device_attribute
*attr
, char *buf
)
499 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
501 return sprintf(buf
, "%d\n", chip
->base
);
503 static DEVICE_ATTR(base
, 0444, chip_base_show
, NULL
);
505 static ssize_t
chip_label_show(struct device
*dev
,
506 struct device_attribute
*attr
, char *buf
)
508 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
510 return sprintf(buf
, "%s\n", chip
->label
? : "");
512 static DEVICE_ATTR(label
, 0444, chip_label_show
, NULL
);
514 static ssize_t
chip_ngpio_show(struct device
*dev
,
515 struct device_attribute
*attr
, char *buf
)
517 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
519 return sprintf(buf
, "%u\n", chip
->ngpio
);
521 static DEVICE_ATTR(ngpio
, 0444, chip_ngpio_show
, NULL
);
523 static ssize_t
chip_names_show(struct device
*dev
,
524 struct device_attribute
*attr
, char *buf
)
526 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
527 char **names
= chip
->names
;
534 for (name
= 0; name
< chip
->ngpio
&& ptr
< PAGE_SIZE
; name
++)
535 ptr
+= snprintf(buf
+ ptr
, PAGE_SIZE
- ptr
,
536 "%s\n", names
[name
] ? names
[name
] : "");
540 static DEVICE_ATTR(names
, 0444, chip_names_show
, NULL
);
542 static const struct attribute
*gpiochip_attrs
[] = {
544 &dev_attr_label
.attr
,
545 &dev_attr_ngpio
.attr
,
546 &dev_attr_names
.attr
,
550 static const struct attribute_group gpiochip_attr_group
= {
551 .attrs
= (struct attribute
**) gpiochip_attrs
,
555 * /sys/class/gpio/export ... write-only
556 * integer N ... number of GPIO to export (full access)
557 * /sys/class/gpio/unexport ... write-only
558 * integer N ... number of GPIO to unexport
560 static ssize_t
export_store(struct class *class, const char *buf
, size_t len
)
565 status
= strict_strtol(buf
, 0, &gpio
);
569 /* No extra locking here; FLAG_SYSFS just signifies that the
570 * request and export were done by on behalf of userspace, so
571 * they may be undone on its behalf too.
574 status
= gpio_request(gpio
, "sysfs");
578 status
= gpio_export(gpio
, true);
582 set_bit(FLAG_SYSFS
, &gpio_desc
[gpio
].flags
);
586 pr_debug("%s: status %d\n", __func__
, status
);
587 return status
? : len
;
590 static ssize_t
unexport_store(struct class *class, const char *buf
, size_t len
)
595 status
= strict_strtol(buf
, 0, &gpio
);
601 /* reject bogus commands (gpio_unexport ignores them) */
602 if (!gpio_is_valid(gpio
))
605 /* No extra locking here; FLAG_SYSFS just signifies that the
606 * request and export were done by on behalf of userspace, so
607 * they may be undone on its behalf too.
609 if (test_and_clear_bit(FLAG_SYSFS
, &gpio_desc
[gpio
].flags
)) {
615 pr_debug("%s: status %d\n", __func__
, status
);
616 return status
? : len
;
619 static struct class_attribute gpio_class_attrs
[] = {
620 __ATTR(export
, 0200, NULL
, export_store
),
621 __ATTR(unexport
, 0200, NULL
, unexport_store
),
625 static struct class gpio_class
= {
627 .owner
= THIS_MODULE
,
629 .class_attrs
= gpio_class_attrs
,
634 * gpio_export - export a GPIO through sysfs
635 * @gpio: gpio to make available, already requested
636 * @direction_may_change: true if userspace may change gpio direction
637 * Context: arch_initcall or later
639 * When drivers want to make a GPIO accessible to userspace after they
640 * have requested it -- perhaps while debugging, or as part of their
641 * public interface -- they may use this routine. If the GPIO can
642 * change direction (some can't) and the caller allows it, userspace
643 * will see "direction" sysfs attribute which may be used to change
644 * the gpio's direction. A "value" attribute will always be provided.
646 * Returns zero on success, else an error.
648 int gpio_export(unsigned gpio
, bool direction_may_change
)
651 struct gpio_desc
*desc
;
652 int status
= -EINVAL
;
655 /* can't export until sysfs is available ... */
657 pr_debug("%s: called too early!\n", __func__
);
661 if (!gpio_is_valid(gpio
))
664 mutex_lock(&sysfs_lock
);
666 spin_lock_irqsave(&gpio_lock
, flags
);
667 desc
= &gpio_desc
[gpio
];
668 if (test_bit(FLAG_REQUESTED
, &desc
->flags
)
669 && !test_bit(FLAG_EXPORT
, &desc
->flags
)) {
671 if (!desc
->chip
->direction_input
672 || !desc
->chip
->direction_output
)
673 direction_may_change
= false;
675 spin_unlock_irqrestore(&gpio_lock
, flags
);
677 if (desc
->chip
->names
&& desc
->chip
->names
[gpio
- desc
->chip
->base
])
678 ioname
= desc
->chip
->names
[gpio
- desc
->chip
->base
];
683 dev
= device_create(&gpio_class
, desc
->chip
->dev
, MKDEV(0, 0),
684 desc
, ioname
? ioname
: "gpio%d", gpio
);
686 if (direction_may_change
)
687 status
= sysfs_create_group(&dev
->kobj
,
690 status
= device_create_file(dev
,
693 if (!status
&& gpio_to_irq(gpio
) >= 0
694 && (direction_may_change
695 || !test_bit(FLAG_IS_OUT
,
697 status
= device_create_file(dev
,
701 device_unregister(dev
);
705 set_bit(FLAG_EXPORT
, &desc
->flags
);
708 mutex_unlock(&sysfs_lock
);
712 pr_debug("%s: gpio%d status %d\n", __func__
, gpio
, status
);
716 EXPORT_SYMBOL_GPL(gpio_export
);
718 static int match_export(struct device
*dev
, void *data
)
720 return dev_get_drvdata(dev
) == data
;
724 * gpio_export_link - create a sysfs link to an exported GPIO node
725 * @dev: device under which to create symlink
726 * @name: name of the symlink
727 * @gpio: gpio to create symlink to, already exported
729 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
730 * node. Caller is responsible for unlinking.
732 * Returns zero on success, else an error.
734 int gpio_export_link(struct device
*dev
, const char *name
, unsigned gpio
)
736 struct gpio_desc
*desc
;
737 int status
= -EINVAL
;
739 if (!gpio_is_valid(gpio
))
742 mutex_lock(&sysfs_lock
);
744 desc
= &gpio_desc
[gpio
];
746 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
749 tdev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
751 status
= sysfs_create_link(&dev
->kobj
, &tdev
->kobj
,
758 mutex_unlock(&sysfs_lock
);
762 pr_debug("%s: gpio%d status %d\n", __func__
, gpio
, status
);
766 EXPORT_SYMBOL_GPL(gpio_export_link
);
769 * gpio_unexport - reverse effect of gpio_export()
770 * @gpio: gpio to make unavailable
772 * This is implicit on gpio_free().
774 void gpio_unexport(unsigned gpio
)
776 struct gpio_desc
*desc
;
777 int status
= -EINVAL
;
779 if (!gpio_is_valid(gpio
))
782 mutex_lock(&sysfs_lock
);
784 desc
= &gpio_desc
[gpio
];
786 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
787 struct device
*dev
= NULL
;
789 dev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
791 gpio_setup_irq(desc
, dev
, 0);
792 clear_bit(FLAG_EXPORT
, &desc
->flags
);
794 device_unregister(dev
);
800 mutex_unlock(&sysfs_lock
);
803 pr_debug("%s: gpio%d status %d\n", __func__
, gpio
, status
);
805 EXPORT_SYMBOL_GPL(gpio_unexport
);
807 static int gpiochip_export(struct gpio_chip
*chip
)
812 /* Many systems register gpio chips for SOC support very early,
813 * before driver model support is available. In those cases we
814 * export this later, in gpiolib_sysfs_init() ... here we just
815 * verify that _some_ field of gpio_class got initialized.
820 /* use chip->base for the ID; it's already known to be unique */
821 mutex_lock(&sysfs_lock
);
822 dev
= device_create(&gpio_class
, chip
->dev
, MKDEV(0, 0), chip
,
823 "gpiochip%d", chip
->base
);
825 status
= sysfs_create_group(&dev
->kobj
,
826 &gpiochip_attr_group
);
829 chip
->exported
= (status
== 0);
830 mutex_unlock(&sysfs_lock
);
836 spin_lock_irqsave(&gpio_lock
, flags
);
838 while (gpio_desc
[gpio
].chip
== chip
)
839 gpio_desc
[gpio
++].chip
= NULL
;
840 spin_unlock_irqrestore(&gpio_lock
, flags
);
842 pr_debug("%s: chip %s status %d\n", __func__
,
843 chip
->label
, status
);
849 static void gpiochip_unexport(struct gpio_chip
*chip
)
854 mutex_lock(&sysfs_lock
);
855 dev
= class_find_device(&gpio_class
, NULL
, chip
, match_export
);
858 device_unregister(dev
);
863 mutex_unlock(&sysfs_lock
);
866 pr_debug("%s: chip %s status %d\n", __func__
,
867 chip
->label
, status
);
870 static int __init
gpiolib_sysfs_init(void)
876 idr_init(&pdesc_idr
);
878 status
= class_register(&gpio_class
);
882 /* Scan and register the gpio_chips which registered very
883 * early (e.g. before the class_register above was called).
885 * We run before arch_initcall() so chip->dev nodes can have
886 * registered, and so arch_initcall() can always gpio_export().
888 spin_lock_irqsave(&gpio_lock
, flags
);
889 for (gpio
= 0; gpio
< ARCH_NR_GPIOS
; gpio
++) {
890 struct gpio_chip
*chip
;
892 chip
= gpio_desc
[gpio
].chip
;
893 if (!chip
|| chip
->exported
)
896 spin_unlock_irqrestore(&gpio_lock
, flags
);
897 status
= gpiochip_export(chip
);
898 spin_lock_irqsave(&gpio_lock
, flags
);
900 spin_unlock_irqrestore(&gpio_lock
, flags
);
905 postcore_initcall(gpiolib_sysfs_init
);
908 static inline int gpiochip_export(struct gpio_chip
*chip
)
913 static inline void gpiochip_unexport(struct gpio_chip
*chip
)
917 #endif /* CONFIG_GPIO_SYSFS */
920 * gpiochip_add() - register a gpio_chip
921 * @chip: the chip to register, with chip->base initialized
922 * Context: potentially before irqs or kmalloc will work
924 * Returns a negative errno if the chip can't be registered, such as
925 * because the chip->base is invalid or already associated with a
926 * different chip. Otherwise it returns zero as a success code.
928 * When gpiochip_add() is called very early during boot, so that GPIOs
929 * can be freely used, the chip->dev device must be registered before
930 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
931 * for GPIOs will fail rudely.
933 * If chip->base is negative, this requests dynamic assignment of
934 * a range of valid GPIOs.
936 int gpiochip_add(struct gpio_chip
*chip
)
941 int base
= chip
->base
;
943 if ((!gpio_is_valid(base
) || !gpio_is_valid(base
+ chip
->ngpio
- 1))
949 spin_lock_irqsave(&gpio_lock
, flags
);
952 base
= gpiochip_find_base(chip
->ngpio
);
960 /* these GPIO numbers must not be managed by another gpio_chip */
961 for (id
= base
; id
< base
+ chip
->ngpio
; id
++) {
962 if (gpio_desc
[id
].chip
!= NULL
) {
968 for (id
= base
; id
< base
+ chip
->ngpio
; id
++) {
969 gpio_desc
[id
].chip
= chip
;
971 /* REVISIT: most hardware initializes GPIOs as
972 * inputs (often with pullups enabled) so power
973 * usage is minimized. Linux code should set the
974 * gpio direction first thing; but until it does,
975 * we may expose the wrong direction in sysfs.
977 gpio_desc
[id
].flags
= !chip
->direction_input
984 spin_unlock_irqrestore(&gpio_lock
, flags
);
986 status
= gpiochip_export(chip
);
988 /* failures here can mean systems won't boot... */
990 pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n",
991 chip
->base
, chip
->base
+ chip
->ngpio
- 1,
992 chip
->label
? : "generic");
995 EXPORT_SYMBOL_GPL(gpiochip_add
);
998 * gpiochip_remove() - unregister a gpio_chip
999 * @chip: the chip to unregister
1001 * A gpio_chip with any GPIOs still requested may not be removed.
1003 int gpiochip_remove(struct gpio_chip
*chip
)
1005 unsigned long flags
;
1009 spin_lock_irqsave(&gpio_lock
, flags
);
1011 for (id
= chip
->base
; id
< chip
->base
+ chip
->ngpio
; id
++) {
1012 if (test_bit(FLAG_REQUESTED
, &gpio_desc
[id
].flags
)) {
1018 for (id
= chip
->base
; id
< chip
->base
+ chip
->ngpio
; id
++)
1019 gpio_desc
[id
].chip
= NULL
;
1022 spin_unlock_irqrestore(&gpio_lock
, flags
);
1025 gpiochip_unexport(chip
);
1029 EXPORT_SYMBOL_GPL(gpiochip_remove
);
1032 /* These "optional" allocation calls help prevent drivers from stomping
1033 * on each other, and help provide better diagnostics in debugfs.
1034 * They're called even less than the "set direction" calls.
1036 int gpio_request(unsigned gpio
, const char *label
)
1038 struct gpio_desc
*desc
;
1039 struct gpio_chip
*chip
;
1040 int status
= -EINVAL
;
1041 unsigned long flags
;
1043 spin_lock_irqsave(&gpio_lock
, flags
);
1045 if (!gpio_is_valid(gpio
))
1047 desc
= &gpio_desc
[gpio
];
1052 if (!try_module_get(chip
->owner
))
1055 /* NOTE: gpio_request() can be called in early boot,
1056 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1059 if (test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0) {
1060 desc_set_label(desc
, label
? : "?");
1064 module_put(chip
->owner
);
1068 if (chip
->request
) {
1069 /* chip->request may sleep */
1070 spin_unlock_irqrestore(&gpio_lock
, flags
);
1071 status
= chip
->request(chip
, gpio
- chip
->base
);
1072 spin_lock_irqsave(&gpio_lock
, flags
);
1075 desc_set_label(desc
, NULL
);
1076 module_put(chip
->owner
);
1077 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
1083 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
1084 gpio
, label
? : "?", status
);
1085 spin_unlock_irqrestore(&gpio_lock
, flags
);
1088 EXPORT_SYMBOL_GPL(gpio_request
);
1090 void gpio_free(unsigned gpio
)
1092 unsigned long flags
;
1093 struct gpio_desc
*desc
;
1094 struct gpio_chip
*chip
;
1098 if (!gpio_is_valid(gpio
)) {
1099 WARN_ON(extra_checks
);
1103 gpio_unexport(gpio
);
1105 spin_lock_irqsave(&gpio_lock
, flags
);
1107 desc
= &gpio_desc
[gpio
];
1109 if (chip
&& test_bit(FLAG_REQUESTED
, &desc
->flags
)) {
1111 spin_unlock_irqrestore(&gpio_lock
, flags
);
1112 might_sleep_if(extra_checks
&& chip
->can_sleep
);
1113 chip
->free(chip
, gpio
- chip
->base
);
1114 spin_lock_irqsave(&gpio_lock
, flags
);
1116 desc_set_label(desc
, NULL
);
1117 module_put(desc
->chip
->owner
);
1118 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
1120 WARN_ON(extra_checks
);
1122 spin_unlock_irqrestore(&gpio_lock
, flags
);
1124 EXPORT_SYMBOL_GPL(gpio_free
);
1128 * gpiochip_is_requested - return string iff signal was requested
1129 * @chip: controller managing the signal
1130 * @offset: of signal within controller's 0..(ngpio - 1) range
1132 * Returns NULL if the GPIO is not currently requested, else a string.
1133 * If debugfs support is enabled, the string returned is the label passed
1134 * to gpio_request(); otherwise it is a meaningless constant.
1136 * This function is for use by GPIO controller drivers. The label can
1137 * help with diagnostics, and knowing that the signal is used as a GPIO
1138 * can help avoid accidentally multiplexing it to another controller.
1140 const char *gpiochip_is_requested(struct gpio_chip
*chip
, unsigned offset
)
1142 unsigned gpio
= chip
->base
+ offset
;
1144 if (!gpio_is_valid(gpio
) || gpio_desc
[gpio
].chip
!= chip
)
1146 if (test_bit(FLAG_REQUESTED
, &gpio_desc
[gpio
].flags
) == 0)
1148 #ifdef CONFIG_DEBUG_FS
1149 return gpio_desc
[gpio
].label
;
1154 EXPORT_SYMBOL_GPL(gpiochip_is_requested
);
1157 /* Drivers MUST set GPIO direction before making get/set calls. In
1158 * some cases this is done in early boot, before IRQs are enabled.
1160 * As a rule these aren't called more than once (except for drivers
1161 * using the open-drain emulation idiom) so these are natural places
1162 * to accumulate extra debugging checks. Note that we can't (yet)
1163 * rely on gpio_request() having been called beforehand.
1166 int gpio_direction_input(unsigned gpio
)
1168 unsigned long flags
;
1169 struct gpio_chip
*chip
;
1170 struct gpio_desc
*desc
= &gpio_desc
[gpio
];
1171 int status
= -EINVAL
;
1173 spin_lock_irqsave(&gpio_lock
, flags
);
1175 if (!gpio_is_valid(gpio
))
1178 if (!chip
|| !chip
->get
|| !chip
->direction_input
)
1181 if (gpio
>= chip
->ngpio
)
1183 status
= gpio_ensure_requested(desc
, gpio
);
1187 /* now we know the gpio is valid and chip won't vanish */
1189 spin_unlock_irqrestore(&gpio_lock
, flags
);
1191 might_sleep_if(extra_checks
&& chip
->can_sleep
);
1194 status
= chip
->request(chip
, gpio
);
1196 pr_debug("GPIO-%d: chip request fail, %d\n",
1197 chip
->base
+ gpio
, status
);
1198 /* and it's not available to anyone else ...
1199 * gpio_request() is the fully clean solution.
1205 status
= chip
->direction_input(chip
, gpio
);
1207 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1211 spin_unlock_irqrestore(&gpio_lock
, flags
);
1213 pr_debug("%s: gpio-%d status %d\n",
1214 __func__
, gpio
, status
);
1217 EXPORT_SYMBOL_GPL(gpio_direction_input
);
1219 int gpio_direction_output(unsigned gpio
, int value
)
1221 unsigned long flags
;
1222 struct gpio_chip
*chip
;
1223 struct gpio_desc
*desc
= &gpio_desc
[gpio
];
1224 int status
= -EINVAL
;
1226 spin_lock_irqsave(&gpio_lock
, flags
);
1228 if (!gpio_is_valid(gpio
))
1231 if (!chip
|| !chip
->set
|| !chip
->direction_output
)
1234 if (gpio
>= chip
->ngpio
)
1236 status
= gpio_ensure_requested(desc
, gpio
);
1240 /* now we know the gpio is valid and chip won't vanish */
1242 spin_unlock_irqrestore(&gpio_lock
, flags
);
1244 might_sleep_if(extra_checks
&& chip
->can_sleep
);
1247 status
= chip
->request(chip
, gpio
);
1249 pr_debug("GPIO-%d: chip request fail, %d\n",
1250 chip
->base
+ gpio
, status
);
1251 /* and it's not available to anyone else ...
1252 * gpio_request() is the fully clean solution.
1258 status
= chip
->direction_output(chip
, gpio
, value
);
1260 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1264 spin_unlock_irqrestore(&gpio_lock
, flags
);
1266 pr_debug("%s: gpio-%d status %d\n",
1267 __func__
, gpio
, status
);
1270 EXPORT_SYMBOL_GPL(gpio_direction_output
);
1273 /* I/O calls are only valid after configuration completed; the relevant
1274 * "is this a valid GPIO" error checks should already have been done.
1276 * "Get" operations are often inlinable as reading a pin value register,
1277 * and masking the relevant bit in that register.
1279 * When "set" operations are inlinable, they involve writing that mask to
1280 * one register to set a low value, or a different register to set it high.
1281 * Otherwise locking is needed, so there may be little value to inlining.
1283 *------------------------------------------------------------------------
1285 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1286 * have requested the GPIO. That can include implicit requesting by
1287 * a direction setting call. Marking a gpio as requested locks its chip
1288 * in memory, guaranteeing that these table lookups need no more locking
1289 * and that gpiochip_remove() will fail.
1291 * REVISIT when debugging, consider adding some instrumentation to ensure
1292 * that the GPIO was actually requested.
1296 * __gpio_get_value() - return a gpio's value
1297 * @gpio: gpio whose value will be returned
1300 * This is used directly or indirectly to implement gpio_get_value().
1301 * It returns the zero or nonzero value provided by the associated
1302 * gpio_chip.get() method; or zero if no such method is provided.
1304 int __gpio_get_value(unsigned gpio
)
1306 struct gpio_chip
*chip
;
1308 chip
= gpio_to_chip(gpio
);
1309 WARN_ON(extra_checks
&& chip
->can_sleep
);
1310 return chip
->get
? chip
->get(chip
, gpio
- chip
->base
) : 0;
1312 EXPORT_SYMBOL_GPL(__gpio_get_value
);
1315 * __gpio_set_value() - assign a gpio's value
1316 * @gpio: gpio whose value will be assigned
1317 * @value: value to assign
1320 * This is used directly or indirectly to implement gpio_set_value().
1321 * It invokes the associated gpio_chip.set() method.
1323 void __gpio_set_value(unsigned gpio
, int value
)
1325 struct gpio_chip
*chip
;
1327 chip
= gpio_to_chip(gpio
);
1328 WARN_ON(extra_checks
&& chip
->can_sleep
);
1329 chip
->set(chip
, gpio
- chip
->base
, value
);
1331 EXPORT_SYMBOL_GPL(__gpio_set_value
);
1334 * __gpio_cansleep() - report whether gpio value access will sleep
1335 * @gpio: gpio in question
1338 * This is used directly or indirectly to implement gpio_cansleep(). It
1339 * returns nonzero if access reading or writing the GPIO value can sleep.
1341 int __gpio_cansleep(unsigned gpio
)
1343 struct gpio_chip
*chip
;
1345 /* only call this on GPIOs that are valid! */
1346 chip
= gpio_to_chip(gpio
);
1348 return chip
->can_sleep
;
1350 EXPORT_SYMBOL_GPL(__gpio_cansleep
);
1353 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1354 * @gpio: gpio whose IRQ will be returned (already requested)
1357 * This is used directly or indirectly to implement gpio_to_irq().
1358 * It returns the number of the IRQ signaled by this (input) GPIO,
1359 * or a negative errno.
1361 int __gpio_to_irq(unsigned gpio
)
1363 struct gpio_chip
*chip
;
1365 chip
= gpio_to_chip(gpio
);
1366 return chip
->to_irq
? chip
->to_irq(chip
, gpio
- chip
->base
) : -ENXIO
;
1368 EXPORT_SYMBOL_GPL(__gpio_to_irq
);
1372 /* There's no value in making it easy to inline GPIO calls that may sleep.
1373 * Common examples include ones connected to I2C or SPI chips.
1376 int gpio_get_value_cansleep(unsigned gpio
)
1378 struct gpio_chip
*chip
;
1380 might_sleep_if(extra_checks
);
1381 chip
= gpio_to_chip(gpio
);
1382 return chip
->get
? chip
->get(chip
, gpio
- chip
->base
) : 0;
1384 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep
);
1386 void gpio_set_value_cansleep(unsigned gpio
, int value
)
1388 struct gpio_chip
*chip
;
1390 might_sleep_if(extra_checks
);
1391 chip
= gpio_to_chip(gpio
);
1392 chip
->set(chip
, gpio
- chip
->base
, value
);
1394 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep
);
1397 #ifdef CONFIG_DEBUG_FS
1399 static void gpiolib_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
1402 unsigned gpio
= chip
->base
;
1403 struct gpio_desc
*gdesc
= &gpio_desc
[gpio
];
1406 for (i
= 0; i
< chip
->ngpio
; i
++, gpio
++, gdesc
++) {
1407 if (!test_bit(FLAG_REQUESTED
, &gdesc
->flags
))
1410 is_out
= test_bit(FLAG_IS_OUT
, &gdesc
->flags
);
1411 seq_printf(s
, " gpio-%-3d (%-20.20s) %s %s",
1413 is_out
? "out" : "in ",
1415 ? (chip
->get(chip
, i
) ? "hi" : "lo")
1419 int irq
= gpio_to_irq(gpio
);
1420 struct irq_desc
*desc
= irq_to_desc(irq
);
1422 /* This races with request_irq(), set_irq_type(),
1423 * and set_irq_wake() ... but those are "rare".
1425 * More significantly, trigger type flags aren't
1426 * currently maintained by genirq.
1428 if (irq
>= 0 && desc
->action
) {
1431 switch (desc
->status
& IRQ_TYPE_SENSE_MASK
) {
1433 trigger
= "(default)";
1435 case IRQ_TYPE_EDGE_FALLING
:
1436 trigger
= "edge-falling";
1438 case IRQ_TYPE_EDGE_RISING
:
1439 trigger
= "edge-rising";
1441 case IRQ_TYPE_EDGE_BOTH
:
1442 trigger
= "edge-both";
1444 case IRQ_TYPE_LEVEL_HIGH
:
1445 trigger
= "level-high";
1447 case IRQ_TYPE_LEVEL_LOW
:
1448 trigger
= "level-low";
1451 trigger
= "?trigger?";
1455 seq_printf(s
, " irq-%d %s%s",
1457 (desc
->status
& IRQ_WAKEUP
)
1462 seq_printf(s
, "\n");
1466 static int gpiolib_show(struct seq_file
*s
, void *unused
)
1468 struct gpio_chip
*chip
= NULL
;
1472 /* REVISIT this isn't locked against gpio_chip removal ... */
1474 for (gpio
= 0; gpio_is_valid(gpio
); gpio
++) {
1477 if (chip
== gpio_desc
[gpio
].chip
)
1479 chip
= gpio_desc
[gpio
].chip
;
1483 seq_printf(s
, "%sGPIOs %d-%d",
1484 started
? "\n" : "",
1485 chip
->base
, chip
->base
+ chip
->ngpio
- 1);
1488 seq_printf(s
, ", %s/%s",
1489 dev
->bus
? dev
->bus
->name
: "no-bus",
1492 seq_printf(s
, ", %s", chip
->label
);
1493 if (chip
->can_sleep
)
1494 seq_printf(s
, ", can sleep");
1495 seq_printf(s
, ":\n");
1499 chip
->dbg_show(s
, chip
);
1501 gpiolib_dbg_show(s
, chip
);
1506 static int gpiolib_open(struct inode
*inode
, struct file
*file
)
1508 return single_open(file
, gpiolib_show
, NULL
);
1511 static const struct file_operations gpiolib_operations
= {
1512 .open
= gpiolib_open
,
1514 .llseek
= seq_lseek
,
1515 .release
= single_release
,
1518 static int __init
gpiolib_debugfs_init(void)
1520 /* /sys/kernel/debug/gpio */
1521 (void) debugfs_create_file("gpio", S_IFREG
| S_IRUGO
,
1522 NULL
, NULL
, &gpiolib_operations
);
1525 subsys_initcall(gpiolib_debugfs_init
);
1527 #endif /* DEBUG_FS */