1 #include <linux/kernel.h>
2 #include <linux/module.h>
4 #include <linux/spinlock.h>
9 /* Optional implementation infrastructure for GPIO interfaces.
11 * Platforms may want to use this if they tend to use very many GPIOs
12 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
14 * When kernel footprint or instruction count is an issue, simpler
15 * implementations may be preferred. The GPIO programming interface
16 * allows for inlining speed-critical get/set operations for common
17 * cases, so that access to SOC-integrated GPIOs can sometimes cost
18 * only an instruction or two per bit.
22 /* When debugging, extend minimal trust to callers and platform code.
23 * Also emit diagnostic messages that may help initial bringup, when
24 * board setup or driver bugs are most common.
26 * Otherwise, minimize overhead in what may be bitbanging codepaths.
29 #define extra_checks 1
31 #define extra_checks 0
34 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
35 * While any GPIO is requested, its gpio_chip is not removable;
36 * each GPIO's "requested" flag serves as a lock and refcount.
38 static DEFINE_SPINLOCK(gpio_lock);
41 struct gpio_chip *chip;
43 /* flag symbols are bit numbers */
44 #define FLAG_REQUESTED 0
46 #define FLAG_RESERVED 2
48 #ifdef CONFIG_DEBUG_FS
52 static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
54 static inline void desc_set_label(struct gpio_desc *d, const char *label)
56 #ifdef CONFIG_DEBUG_FS
61 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
62 * when setting direction, and otherwise illegal. Until board setup code
63 * and drivers use explicit requests everywhere (which won't happen when
64 * those calls have no teeth) we can't avoid autorequesting. This nag
65 * message should motivate switching to explicit requests...
67 static void gpio_ensure_requested(struct gpio_desc *desc)
69 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
70 pr_warning("GPIO-%d autorequested\n", (int)(desc - gpio_desc));
71 desc_set_label(desc, "[auto]");
72 if (!try_module_get(desc->chip->owner))
73 pr_err("GPIO-%d: module can't be gotten \n",
74 (int)(desc - gpio_desc));
78 /* caller holds gpio_lock *OR* gpio is marked as requested */
79 static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
81 return gpio_desc[gpio].chip;
84 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
85 static int gpiochip_find_base(int ngpio)
91 for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
92 struct gpio_desc *desc = &gpio_desc[i];
93 struct gpio_chip *chip = desc->chip;
95 if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) {
104 i -= chip->ngpio - 1;
108 if (gpio_is_valid(base))
109 pr_debug("%s: found new base at %d\n", __func__, base);
114 * gpiochip_reserve() - reserve range of gpios to use with platform code only
115 * @start: starting gpio number
116 * @ngpio: number of gpios to reserve
117 * Context: platform init, potentially before irqs or kmalloc will work
119 * Returns a negative errno if any gpio within the range is already reserved
120 * or registered, else returns zero as a success code. Use this function
121 * to mark a range of gpios as unavailable for dynamic gpio number allocation,
122 * for example because its driver support is not yet loaded.
124 int __init gpiochip_reserve(int start, int ngpio)
130 if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio))
133 spin_lock_irqsave(&gpio_lock, flags);
135 for (i = start; i < start + ngpio; i++) {
136 struct gpio_desc *desc = &gpio_desc[i];
138 if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) {
143 set_bit(FLAG_RESERVED, &desc->flags);
146 pr_debug("%s: reserved gpios from %d to %d\n",
147 __func__, start, start + ngpio - 1);
149 spin_unlock_irqrestore(&gpio_lock, flags);
155 * gpiochip_add() - register a gpio_chip
156 * @chip: the chip to register, with chip->base initialized
157 * Context: potentially before irqs or kmalloc will work
159 * Returns a negative errno if the chip can't be registered, such as
160 * because the chip->base is invalid or already associated with a
161 * different chip. Otherwise it returns zero as a success code.
163 * If chip->base is negative, this requests dynamic assignment of
164 * a range of valid GPIOs.
166 int gpiochip_add(struct gpio_chip *chip)
171 int base = chip->base;
173 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio))
179 spin_lock_irqsave(&gpio_lock, flags);
182 base = gpiochip_find_base(chip->ngpio);
190 /* these GPIO numbers must not be managed by another gpio_chip */
191 for (id = base; id < base + chip->ngpio; id++) {
192 if (gpio_desc[id].chip != NULL) {
198 for (id = base; id < base + chip->ngpio; id++) {
199 gpio_desc[id].chip = chip;
200 gpio_desc[id].flags = 0;
205 spin_unlock_irqrestore(&gpio_lock, flags);
207 /* failures here can mean systems won't boot... */
209 pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n",
210 chip->base, chip->base + chip->ngpio,
211 chip->label ? : "generic");
214 EXPORT_SYMBOL_GPL(gpiochip_add);
217 * gpiochip_remove() - unregister a gpio_chip
218 * @chip: the chip to unregister
220 * A gpio_chip with any GPIOs still requested may not be removed.
222 int gpiochip_remove(struct gpio_chip *chip)
228 spin_lock_irqsave(&gpio_lock, flags);
230 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
231 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
237 for (id = chip->base; id < chip->base + chip->ngpio; id++)
238 gpio_desc[id].chip = NULL;
241 spin_unlock_irqrestore(&gpio_lock, flags);
244 EXPORT_SYMBOL_GPL(gpiochip_remove);
247 /* These "optional" allocation calls help prevent drivers from stomping
248 * on each other, and help provide better diagnostics in debugfs.
249 * They're called even less than the "set direction" calls.
251 int gpio_request(unsigned gpio, const char *label)
253 struct gpio_desc *desc;
254 int status = -EINVAL;
257 spin_lock_irqsave(&gpio_lock, flags);
259 if (!gpio_is_valid(gpio))
261 desc = &gpio_desc[gpio];
262 if (desc->chip == NULL)
265 if (!try_module_get(desc->chip->owner))
268 /* NOTE: gpio_request() can be called in early boot,
269 * before IRQs are enabled.
272 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
273 desc_set_label(desc, label ? : "?");
277 module_put(desc->chip->owner);
282 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
283 gpio, label ? : "?", status);
284 spin_unlock_irqrestore(&gpio_lock, flags);
287 EXPORT_SYMBOL_GPL(gpio_request);
289 void gpio_free(unsigned gpio)
292 struct gpio_desc *desc;
294 if (!gpio_is_valid(gpio)) {
295 WARN_ON(extra_checks);
299 spin_lock_irqsave(&gpio_lock, flags);
301 desc = &gpio_desc[gpio];
302 if (desc->chip && test_and_clear_bit(FLAG_REQUESTED, &desc->flags)) {
303 desc_set_label(desc, NULL);
304 module_put(desc->chip->owner);
306 WARN_ON(extra_checks);
308 spin_unlock_irqrestore(&gpio_lock, flags);
310 EXPORT_SYMBOL_GPL(gpio_free);
314 * gpiochip_is_requested - return string iff signal was requested
315 * @chip: controller managing the signal
316 * @offset: of signal within controller's 0..(ngpio - 1) range
318 * Returns NULL if the GPIO is not currently requested, else a string.
319 * If debugfs support is enabled, the string returned is the label passed
320 * to gpio_request(); otherwise it is a meaningless constant.
322 * This function is for use by GPIO controller drivers. The label can
323 * help with diagnostics, and knowing that the signal is used as a GPIO
324 * can help avoid accidentally multiplexing it to another controller.
326 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
328 unsigned gpio = chip->base + offset;
330 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
332 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
334 #ifdef CONFIG_DEBUG_FS
335 return gpio_desc[gpio].label;
340 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
343 /* Drivers MUST set GPIO direction before making get/set calls. In
344 * some cases this is done in early boot, before IRQs are enabled.
346 * As a rule these aren't called more than once (except for drivers
347 * using the open-drain emulation idiom) so these are natural places
348 * to accumulate extra debugging checks. Note that we can't (yet)
349 * rely on gpio_request() having been called beforehand.
352 int gpio_direction_input(unsigned gpio)
355 struct gpio_chip *chip;
356 struct gpio_desc *desc = &gpio_desc[gpio];
357 int status = -EINVAL;
359 spin_lock_irqsave(&gpio_lock, flags);
361 if (!gpio_is_valid(gpio))
364 if (!chip || !chip->get || !chip->direction_input)
367 if (gpio >= chip->ngpio)
369 gpio_ensure_requested(desc);
371 /* now we know the gpio is valid and chip won't vanish */
373 spin_unlock_irqrestore(&gpio_lock, flags);
375 might_sleep_if(extra_checks && chip->can_sleep);
377 status = chip->direction_input(chip, gpio);
379 clear_bit(FLAG_IS_OUT, &desc->flags);
382 spin_unlock_irqrestore(&gpio_lock, flags);
384 pr_debug("%s: gpio-%d status %d\n",
385 __FUNCTION__, gpio, status);
388 EXPORT_SYMBOL_GPL(gpio_direction_input);
390 int gpio_direction_output(unsigned gpio, int value)
393 struct gpio_chip *chip;
394 struct gpio_desc *desc = &gpio_desc[gpio];
395 int status = -EINVAL;
397 spin_lock_irqsave(&gpio_lock, flags);
399 if (!gpio_is_valid(gpio))
402 if (!chip || !chip->set || !chip->direction_output)
405 if (gpio >= chip->ngpio)
407 gpio_ensure_requested(desc);
409 /* now we know the gpio is valid and chip won't vanish */
411 spin_unlock_irqrestore(&gpio_lock, flags);
413 might_sleep_if(extra_checks && chip->can_sleep);
415 status = chip->direction_output(chip, gpio, value);
417 set_bit(FLAG_IS_OUT, &desc->flags);
420 spin_unlock_irqrestore(&gpio_lock, flags);
422 pr_debug("%s: gpio-%d status %d\n",
423 __FUNCTION__, gpio, status);
426 EXPORT_SYMBOL_GPL(gpio_direction_output);
429 /* I/O calls are only valid after configuration completed; the relevant
430 * "is this a valid GPIO" error checks should already have been done.
432 * "Get" operations are often inlinable as reading a pin value register,
433 * and masking the relevant bit in that register.
435 * When "set" operations are inlinable, they involve writing that mask to
436 * one register to set a low value, or a different register to set it high.
437 * Otherwise locking is needed, so there may be little value to inlining.
439 *------------------------------------------------------------------------
441 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
442 * have requested the GPIO. That can include implicit requesting by
443 * a direction setting call. Marking a gpio as requested locks its chip
444 * in memory, guaranteeing that these table lookups need no more locking
445 * and that gpiochip_remove() will fail.
447 * REVISIT when debugging, consider adding some instrumentation to ensure
448 * that the GPIO was actually requested.
452 * __gpio_get_value() - return a gpio's value
453 * @gpio: gpio whose value will be returned
456 * This is used directly or indirectly to implement gpio_get_value().
457 * It returns the zero or nonzero value provided by the associated
458 * gpio_chip.get() method; or zero if no such method is provided.
460 int __gpio_get_value(unsigned gpio)
462 struct gpio_chip *chip;
464 chip = gpio_to_chip(gpio);
465 WARN_ON(extra_checks && chip->can_sleep);
466 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
468 EXPORT_SYMBOL_GPL(__gpio_get_value);
471 * __gpio_set_value() - assign a gpio's value
472 * @gpio: gpio whose value will be assigned
473 * @value: value to assign
476 * This is used directly or indirectly to implement gpio_set_value().
477 * It invokes the associated gpio_chip.set() method.
479 void __gpio_set_value(unsigned gpio, int value)
481 struct gpio_chip *chip;
483 chip = gpio_to_chip(gpio);
484 WARN_ON(extra_checks && chip->can_sleep);
485 chip->set(chip, gpio - chip->base, value);
487 EXPORT_SYMBOL_GPL(__gpio_set_value);
490 * __gpio_cansleep() - report whether gpio value access will sleep
491 * @gpio: gpio in question
494 * This is used directly or indirectly to implement gpio_cansleep(). It
495 * returns nonzero if access reading or writing the GPIO value can sleep.
497 int __gpio_cansleep(unsigned gpio)
499 struct gpio_chip *chip;
501 /* only call this on GPIOs that are valid! */
502 chip = gpio_to_chip(gpio);
504 return chip->can_sleep;
506 EXPORT_SYMBOL_GPL(__gpio_cansleep);
510 /* There's no value in making it easy to inline GPIO calls that may sleep.
511 * Common examples include ones connected to I2C or SPI chips.
514 int gpio_get_value_cansleep(unsigned gpio)
516 struct gpio_chip *chip;
518 might_sleep_if(extra_checks);
519 chip = gpio_to_chip(gpio);
520 return chip->get(chip, gpio - chip->base);
522 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
524 void gpio_set_value_cansleep(unsigned gpio, int value)
526 struct gpio_chip *chip;
528 might_sleep_if(extra_checks);
529 chip = gpio_to_chip(gpio);
530 chip->set(chip, gpio - chip->base, value);
532 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
535 #ifdef CONFIG_DEBUG_FS
537 #include <linux/debugfs.h>
538 #include <linux/seq_file.h>
541 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
544 unsigned gpio = chip->base;
545 struct gpio_desc *gdesc = &gpio_desc[gpio];
548 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
549 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
552 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
553 seq_printf(s, " gpio-%-3d (%-12s) %s %s",
555 is_out ? "out" : "in ",
557 ? (chip->get(chip, i) ? "hi" : "lo")
561 int irq = gpio_to_irq(gpio);
562 struct irq_desc *desc = irq_desc + irq;
564 /* This races with request_irq(), set_irq_type(),
565 * and set_irq_wake() ... but those are "rare".
567 * More significantly, trigger type flags aren't
568 * currently maintained by genirq.
570 if (irq >= 0 && desc->action) {
573 switch (desc->status & IRQ_TYPE_SENSE_MASK) {
575 trigger = "(default)";
577 case IRQ_TYPE_EDGE_FALLING:
578 trigger = "edge-falling";
580 case IRQ_TYPE_EDGE_RISING:
581 trigger = "edge-rising";
583 case IRQ_TYPE_EDGE_BOTH:
584 trigger = "edge-both";
586 case IRQ_TYPE_LEVEL_HIGH:
587 trigger = "level-high";
589 case IRQ_TYPE_LEVEL_LOW:
590 trigger = "level-low";
593 trigger = "?trigger?";
597 seq_printf(s, " irq-%d %s%s",
599 (desc->status & IRQ_WAKEUP)
608 static int gpiolib_show(struct seq_file *s, void *unused)
610 struct gpio_chip *chip = NULL;
614 /* REVISIT this isn't locked against gpio_chip removal ... */
616 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
617 if (chip == gpio_desc[gpio].chip)
619 chip = gpio_desc[gpio].chip;
623 seq_printf(s, "%sGPIOs %d-%d, %s%s:\n",
625 chip->base, chip->base + chip->ngpio - 1,
626 chip->label ? : "generic",
627 chip->can_sleep ? ", can sleep" : "");
630 chip->dbg_show(s, chip);
632 gpiolib_dbg_show(s, chip);
637 static int gpiolib_open(struct inode *inode, struct file *file)
639 return single_open(file, gpiolib_show, NULL);
642 static struct file_operations gpiolib_operations = {
643 .open = gpiolib_open,
646 .release = single_release,
649 static int __init gpiolib_debugfs_init(void)
651 /* /sys/kernel/debug/gpio */
652 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
653 NULL, NULL, &gpiolib_operations);
656 subsys_initcall(gpiolib_debugfs_init);
658 #endif /* DEBUG_FS */