Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

gpiolib: Allow GPIO chips to request their own GPIOs

Sometimes it is useful to allow GPIO chips themselves to request GPIOs they
own through gpiolib API. One use case is ACPI ASL code that should be able
to toggle GPIOs through GPIO operation regions.

We can't use gpio_request() because it will pin the module to the kernel
forever (it calls try_module_get()). To solve this we move module refcount
manipulation to gpiod_request() and let __gpiod_request() handle the actual
request. This changes the sequence a bit as now try_module_get() is called
outside of gpio_lock (I think this is safe, try_module_get() handles
serialization it needs already).

Then we provide gpiolib internal functions gpiochip_request/free_own_desc()
that do the same as gpio_request() but don't manipulate module refrence
count. This allows the GPIO chip driver to request and free descriptors it
owns without being pinned to the kernel forever.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

authored by

Mika Westerberg and committed by
Linus Walleij
77c2d792 7b7f588b

+76 -27
+73 -27
drivers/gpio/gpiolib.c
··· 1458 1458 * on each other, and help provide better diagnostics in debugfs. 1459 1459 * They're called even less than the "set direction" calls. 1460 1460 */ 1461 - static int gpiod_request(struct gpio_desc *desc, const char *label) 1461 + static int __gpiod_request(struct gpio_desc *desc, const char *label) 1462 1462 { 1463 - struct gpio_chip *chip; 1464 - int status = -EPROBE_DEFER; 1463 + struct gpio_chip *chip = desc->chip; 1464 + int status; 1465 1465 unsigned long flags; 1466 1466 1467 - if (!desc) { 1468 - pr_warn("%s: invalid GPIO\n", __func__); 1469 - return -EINVAL; 1470 - } 1471 - 1472 1467 spin_lock_irqsave(&gpio_lock, flags); 1473 - 1474 - chip = desc->chip; 1475 - if (chip == NULL) 1476 - goto done; 1477 - 1478 - if (!try_module_get(chip->owner)) 1479 - goto done; 1480 1468 1481 1469 /* NOTE: gpio_request() can be called in early boot, 1482 1470 * before IRQs are enabled, for non-sleeping (SOC) GPIOs. ··· 1475 1487 status = 0; 1476 1488 } else { 1477 1489 status = -EBUSY; 1478 - module_put(chip->owner); 1479 1490 goto done; 1480 1491 } 1481 1492 ··· 1486 1499 1487 1500 if (status < 0) { 1488 1501 desc_set_label(desc, NULL); 1489 - module_put(chip->owner); 1490 1502 clear_bit(FLAG_REQUESTED, &desc->flags); 1491 1503 goto done; 1492 1504 } ··· 1497 1511 spin_lock_irqsave(&gpio_lock, flags); 1498 1512 } 1499 1513 done: 1514 + spin_unlock_irqrestore(&gpio_lock, flags); 1515 + return status; 1516 + } 1517 + 1518 + static int gpiod_request(struct gpio_desc *desc, const char *label) 1519 + { 1520 + int status = -EPROBE_DEFER; 1521 + struct gpio_chip *chip; 1522 + 1523 + if (!desc) { 1524 + pr_warn("%s: invalid GPIO\n", __func__); 1525 + return -EINVAL; 1526 + } 1527 + 1528 + chip = desc->chip; 1529 + if (!chip) 1530 + goto done; 1531 + 1532 + if (try_module_get(chip->owner)) { 1533 + status = __gpiod_request(desc, label); 1534 + if (status < 0) 1535 + module_put(chip->owner); 1536 + } 1537 + 1538 + done: 1500 1539 if (status) 1501 1540 gpiod_dbg(desc, "%s: status %d\n", __func__, status); 1502 - spin_unlock_irqrestore(&gpio_lock, flags); 1541 + 1503 1542 return status; 1504 1543 } 1505 1544 ··· 1534 1523 } 1535 1524 EXPORT_SYMBOL_GPL(gpio_request); 1536 1525 1537 - static void gpiod_free(struct gpio_desc *desc) 1526 + static bool __gpiod_free(struct gpio_desc *desc) 1538 1527 { 1528 + bool ret = false; 1539 1529 unsigned long flags; 1540 1530 struct gpio_chip *chip; 1541 1531 1542 1532 might_sleep(); 1543 - 1544 - if (!desc) { 1545 - WARN_ON(extra_checks); 1546 - return; 1547 - } 1548 1533 1549 1534 gpiod_unexport(desc); 1550 1535 ··· 1555 1548 spin_lock_irqsave(&gpio_lock, flags); 1556 1549 } 1557 1550 desc_set_label(desc, NULL); 1558 - module_put(desc->chip->owner); 1559 1551 clear_bit(FLAG_ACTIVE_LOW, &desc->flags); 1560 1552 clear_bit(FLAG_REQUESTED, &desc->flags); 1561 1553 clear_bit(FLAG_OPEN_DRAIN, &desc->flags); 1562 1554 clear_bit(FLAG_OPEN_SOURCE, &desc->flags); 1563 - } else 1564 - WARN_ON(extra_checks); 1555 + ret = true; 1556 + } 1565 1557 1566 1558 spin_unlock_irqrestore(&gpio_lock, flags); 1559 + return ret; 1560 + } 1561 + 1562 + static void gpiod_free(struct gpio_desc *desc) 1563 + { 1564 + if (desc && __gpiod_free(desc)) 1565 + module_put(desc->chip->owner); 1566 + else 1567 + WARN_ON(extra_checks); 1567 1568 } 1568 1569 1569 1570 void gpio_free(unsigned gpio) ··· 1693 1678 } 1694 1679 EXPORT_SYMBOL_GPL(gpiochip_is_requested); 1695 1680 1681 + /** 1682 + * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor 1683 + * @desc: GPIO descriptor to request 1684 + * @label: label for the GPIO 1685 + * 1686 + * Function allows GPIO chip drivers to request and use their own GPIO 1687 + * descriptors via gpiolib API. Difference to gpiod_request() is that this 1688 + * function will not increase reference count of the GPIO chip module. This 1689 + * allows the GPIO chip module to be unloaded as needed (we assume that the 1690 + * GPIO chip driver handles freeing the GPIOs it has requested). 1691 + */ 1692 + int gpiochip_request_own_desc(struct gpio_desc *desc, const char *label) 1693 + { 1694 + if (!desc || !desc->chip) 1695 + return -EINVAL; 1696 + 1697 + return __gpiod_request(desc, label); 1698 + } 1699 + 1700 + /** 1701 + * gpiochip_free_own_desc - Free GPIO requested by the chip driver 1702 + * @desc: GPIO descriptor to free 1703 + * 1704 + * Function frees the given GPIO requested previously with 1705 + * gpiochip_request_own_desc(). 1706 + */ 1707 + void gpiochip_free_own_desc(struct gpio_desc *desc) 1708 + { 1709 + if (desc) 1710 + __gpiod_free(desc); 1711 + } 1696 1712 1697 1713 /* Drivers MUST set GPIO direction before making get/set calls. In 1698 1714 * some cases this is done in early boot, before IRQs are enabled.
+3
drivers/gpio/gpiolib.h
··· 43 43 } 44 44 #endif 45 45 46 + int gpiochip_request_own_desc(struct gpio_desc *desc, const char *label); 47 + void gpiochip_free_own_desc(struct gpio_desc *desc); 48 + 46 49 #endif /* GPIOLIB_H */