diff options
Diffstat (limited to 'drivers/gpio/gpiolib.c')
-rw-r--r-- | drivers/gpio/gpiolib.c | 582 |
1 files changed, 423 insertions, 159 deletions
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index eb80dac4e26a..86dcd02cf602 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c | |||
@@ -72,6 +72,8 @@ static LIST_HEAD(gpio_lookup_list); | |||
72 | LIST_HEAD(gpio_devices); | 72 | LIST_HEAD(gpio_devices); |
73 | 73 | ||
74 | static void gpiochip_free_hogs(struct gpio_chip *chip); | 74 | static void gpiochip_free_hogs(struct gpio_chip *chip); |
75 | static int gpiochip_add_irqchip(struct gpio_chip *gpiochip, | ||
76 | struct lock_class_key *key); | ||
75 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip); | 77 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip); |
76 | static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip); | 78 | static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip); |
77 | static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip); | 79 | static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip); |
@@ -365,28 +367,28 @@ static long linehandle_ioctl(struct file *filep, unsigned int cmd, | |||
365 | struct linehandle_state *lh = filep->private_data; | 367 | struct linehandle_state *lh = filep->private_data; |
366 | void __user *ip = (void __user *)arg; | 368 | void __user *ip = (void __user *)arg; |
367 | struct gpiohandle_data ghd; | 369 | struct gpiohandle_data ghd; |
370 | int vals[GPIOHANDLES_MAX]; | ||
368 | int i; | 371 | int i; |
369 | 372 | ||
370 | if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { | 373 | if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { |
371 | int val; | 374 | /* TODO: check if descriptors are really input */ |
375 | int ret = gpiod_get_array_value_complex(false, | ||
376 | true, | ||
377 | lh->numdescs, | ||
378 | lh->descs, | ||
379 | vals); | ||
380 | if (ret) | ||
381 | return ret; | ||
372 | 382 | ||
373 | memset(&ghd, 0, sizeof(ghd)); | 383 | memset(&ghd, 0, sizeof(ghd)); |
374 | 384 | for (i = 0; i < lh->numdescs; i++) | |
375 | /* TODO: check if descriptors are really input */ | 385 | ghd.values[i] = vals[i]; |
376 | for (i = 0; i < lh->numdescs; i++) { | ||
377 | val = gpiod_get_value_cansleep(lh->descs[i]); | ||
378 | if (val < 0) | ||
379 | return val; | ||
380 | ghd.values[i] = val; | ||
381 | } | ||
382 | 386 | ||
383 | if (copy_to_user(ip, &ghd, sizeof(ghd))) | 387 | if (copy_to_user(ip, &ghd, sizeof(ghd))) |
384 | return -EFAULT; | 388 | return -EFAULT; |
385 | 389 | ||
386 | return 0; | 390 | return 0; |
387 | } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) { | 391 | } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) { |
388 | int vals[GPIOHANDLES_MAX]; | ||
389 | |||
390 | /* TODO: check if descriptors are really output */ | 392 | /* TODO: check if descriptors are really output */ |
391 | if (copy_from_user(&ghd, ip, sizeof(ghd))) | 393 | if (copy_from_user(&ghd, ip, sizeof(ghd))) |
392 | return -EFAULT; | 394 | return -EFAULT; |
@@ -444,12 +446,25 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) | |||
444 | struct linehandle_state *lh; | 446 | struct linehandle_state *lh; |
445 | struct file *file; | 447 | struct file *file; |
446 | int fd, i, ret; | 448 | int fd, i, ret; |
449 | u32 lflags; | ||
447 | 450 | ||
448 | if (copy_from_user(&handlereq, ip, sizeof(handlereq))) | 451 | if (copy_from_user(&handlereq, ip, sizeof(handlereq))) |
449 | return -EFAULT; | 452 | return -EFAULT; |
450 | if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX)) | 453 | if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX)) |
451 | return -EINVAL; | 454 | return -EINVAL; |
452 | 455 | ||
456 | lflags = handlereq.flags; | ||
457 | |||
458 | /* Return an error if an unknown flag is set */ | ||
459 | if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) | ||
460 | return -EINVAL; | ||
461 | |||
462 | /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */ | ||
463 | if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) && | ||
464 | ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || | ||
465 | (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))) | ||
466 | return -EINVAL; | ||
467 | |||
453 | lh = kzalloc(sizeof(*lh), GFP_KERNEL); | 468 | lh = kzalloc(sizeof(*lh), GFP_KERNEL); |
454 | if (!lh) | 469 | if (!lh) |
455 | return -ENOMEM; | 470 | return -ENOMEM; |
@@ -470,7 +485,6 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) | |||
470 | /* Request each GPIO */ | 485 | /* Request each GPIO */ |
471 | for (i = 0; i < handlereq.lines; i++) { | 486 | for (i = 0; i < handlereq.lines; i++) { |
472 | u32 offset = handlereq.lineoffsets[i]; | 487 | u32 offset = handlereq.lineoffsets[i]; |
473 | u32 lflags = handlereq.flags; | ||
474 | struct gpio_desc *desc; | 488 | struct gpio_desc *desc; |
475 | 489 | ||
476 | if (offset >= gdev->ngpio) { | 490 | if (offset >= gdev->ngpio) { |
@@ -478,12 +492,6 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) | |||
478 | goto out_free_descs; | 492 | goto out_free_descs; |
479 | } | 493 | } |
480 | 494 | ||
481 | /* Return an error if a unknown flag is set */ | ||
482 | if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) { | ||
483 | ret = -EINVAL; | ||
484 | goto out_free_descs; | ||
485 | } | ||
486 | |||
487 | desc = &gdev->descs[offset]; | 495 | desc = &gdev->descs[offset]; |
488 | ret = gpiod_request(desc, lh->label); | 496 | ret = gpiod_request(desc, lh->label); |
489 | if (ret) | 497 | if (ret) |
@@ -1091,30 +1099,8 @@ static void gpiochip_setup_devs(void) | |||
1091 | } | 1099 | } |
1092 | } | 1100 | } |
1093 | 1101 | ||
1094 | /** | 1102 | int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data, |
1095 | * gpiochip_add_data() - register a gpio_chip | 1103 | struct lock_class_key *key) |
1096 | * @chip: the chip to register, with chip->base initialized | ||
1097 | * @data: driver-private data associated with this chip | ||
1098 | * | ||
1099 | * Context: potentially before irqs will work | ||
1100 | * | ||
1101 | * When gpiochip_add_data() is called very early during boot, so that GPIOs | ||
1102 | * can be freely used, the chip->parent device must be registered before | ||
1103 | * the gpio framework's arch_initcall(). Otherwise sysfs initialization | ||
1104 | * for GPIOs will fail rudely. | ||
1105 | * | ||
1106 | * gpiochip_add_data() must only be called after gpiolib initialization, | ||
1107 | * ie after core_initcall(). | ||
1108 | * | ||
1109 | * If chip->base is negative, this requests dynamic assignment of | ||
1110 | * a range of valid GPIOs. | ||
1111 | * | ||
1112 | * Returns: | ||
1113 | * A negative errno if the chip can't be registered, such as because the | ||
1114 | * chip->base is invalid or already associated with a different chip. | ||
1115 | * Otherwise it returns zero as a success code. | ||
1116 | */ | ||
1117 | int gpiochip_add_data(struct gpio_chip *chip, void *data) | ||
1118 | { | 1104 | { |
1119 | unsigned long flags; | 1105 | unsigned long flags; |
1120 | int status = 0; | 1106 | int status = 0; |
@@ -1260,6 +1246,10 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data) | |||
1260 | if (status) | 1246 | if (status) |
1261 | goto err_remove_from_list; | 1247 | goto err_remove_from_list; |
1262 | 1248 | ||
1249 | status = gpiochip_add_irqchip(chip, key); | ||
1250 | if (status) | ||
1251 | goto err_remove_chip; | ||
1252 | |||
1263 | status = of_gpiochip_add(chip); | 1253 | status = of_gpiochip_add(chip); |
1264 | if (status) | 1254 | if (status) |
1265 | goto err_remove_chip; | 1255 | goto err_remove_chip; |
@@ -1303,7 +1293,7 @@ err_free_gdev: | |||
1303 | kfree(gdev); | 1293 | kfree(gdev); |
1304 | return status; | 1294 | return status; |
1305 | } | 1295 | } |
1306 | EXPORT_SYMBOL_GPL(gpiochip_add_data); | 1296 | EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key); |
1307 | 1297 | ||
1308 | /** | 1298 | /** |
1309 | * gpiochip_get_data() - get per-subdriver data for the chip | 1299 | * gpiochip_get_data() - get per-subdriver data for the chip |
@@ -1498,33 +1488,33 @@ static struct gpio_chip *find_chip_by_name(const char *name) | |||
1498 | 1488 | ||
1499 | static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip) | 1489 | static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip) |
1500 | { | 1490 | { |
1501 | if (!gpiochip->irq_need_valid_mask) | 1491 | if (!gpiochip->irq.need_valid_mask) |
1502 | return 0; | 1492 | return 0; |
1503 | 1493 | ||
1504 | gpiochip->irq_valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio), | 1494 | gpiochip->irq.valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio), |
1505 | sizeof(long), GFP_KERNEL); | 1495 | sizeof(long), GFP_KERNEL); |
1506 | if (!gpiochip->irq_valid_mask) | 1496 | if (!gpiochip->irq.valid_mask) |
1507 | return -ENOMEM; | 1497 | return -ENOMEM; |
1508 | 1498 | ||
1509 | /* Assume by default all GPIOs are valid */ | 1499 | /* Assume by default all GPIOs are valid */ |
1510 | bitmap_fill(gpiochip->irq_valid_mask, gpiochip->ngpio); | 1500 | bitmap_fill(gpiochip->irq.valid_mask, gpiochip->ngpio); |
1511 | 1501 | ||
1512 | return 0; | 1502 | return 0; |
1513 | } | 1503 | } |
1514 | 1504 | ||
1515 | static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip) | 1505 | static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip) |
1516 | { | 1506 | { |
1517 | kfree(gpiochip->irq_valid_mask); | 1507 | kfree(gpiochip->irq.valid_mask); |
1518 | gpiochip->irq_valid_mask = NULL; | 1508 | gpiochip->irq.valid_mask = NULL; |
1519 | } | 1509 | } |
1520 | 1510 | ||
1521 | static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip, | 1511 | static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip, |
1522 | unsigned int offset) | 1512 | unsigned int offset) |
1523 | { | 1513 | { |
1524 | /* No mask means all valid */ | 1514 | /* No mask means all valid */ |
1525 | if (likely(!gpiochip->irq_valid_mask)) | 1515 | if (likely(!gpiochip->irq.valid_mask)) |
1526 | return true; | 1516 | return true; |
1527 | return test_bit(offset, gpiochip->irq_valid_mask); | 1517 | return test_bit(offset, gpiochip->irq.valid_mask); |
1528 | } | 1518 | } |
1529 | 1519 | ||
1530 | /** | 1520 | /** |
@@ -1544,7 +1534,7 @@ static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip, | |||
1544 | { | 1534 | { |
1545 | unsigned int offset; | 1535 | unsigned int offset; |
1546 | 1536 | ||
1547 | if (!gpiochip->irqdomain) { | 1537 | if (!gpiochip->irq.domain) { |
1548 | chip_err(gpiochip, "called %s before setting up irqchip\n", | 1538 | chip_err(gpiochip, "called %s before setting up irqchip\n", |
1549 | __func__); | 1539 | __func__); |
1550 | return; | 1540 | return; |
@@ -1564,14 +1554,15 @@ static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip, | |||
1564 | irq_set_chained_handler_and_data(parent_irq, parent_handler, | 1554 | irq_set_chained_handler_and_data(parent_irq, parent_handler, |
1565 | gpiochip); | 1555 | gpiochip); |
1566 | 1556 | ||
1567 | gpiochip->irq_chained_parent = parent_irq; | 1557 | gpiochip->irq.parents = &parent_irq; |
1558 | gpiochip->irq.num_parents = 1; | ||
1568 | } | 1559 | } |
1569 | 1560 | ||
1570 | /* Set the parent IRQ for all affected IRQs */ | 1561 | /* Set the parent IRQ for all affected IRQs */ |
1571 | for (offset = 0; offset < gpiochip->ngpio; offset++) { | 1562 | for (offset = 0; offset < gpiochip->ngpio; offset++) { |
1572 | if (!gpiochip_irqchip_irq_valid(gpiochip, offset)) | 1563 | if (!gpiochip_irqchip_irq_valid(gpiochip, offset)) |
1573 | continue; | 1564 | continue; |
1574 | irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset), | 1565 | irq_set_parent(irq_find_mapping(gpiochip->irq.domain, offset), |
1575 | parent_irq); | 1566 | parent_irq); |
1576 | } | 1567 | } |
1577 | } | 1568 | } |
@@ -1591,6 +1582,11 @@ void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip, | |||
1591 | unsigned int parent_irq, | 1582 | unsigned int parent_irq, |
1592 | irq_flow_handler_t parent_handler) | 1583 | irq_flow_handler_t parent_handler) |
1593 | { | 1584 | { |
1585 | if (gpiochip->irq.threaded) { | ||
1586 | chip_err(gpiochip, "tried to chain a threaded gpiochip\n"); | ||
1587 | return; | ||
1588 | } | ||
1589 | |||
1594 | gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq, | 1590 | gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq, |
1595 | parent_handler); | 1591 | parent_handler); |
1596 | } | 1592 | } |
@@ -1607,10 +1603,6 @@ void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip, | |||
1607 | struct irq_chip *irqchip, | 1603 | struct irq_chip *irqchip, |
1608 | unsigned int parent_irq) | 1604 | unsigned int parent_irq) |
1609 | { | 1605 | { |
1610 | if (!gpiochip->irq_nested) { | ||
1611 | chip_err(gpiochip, "tried to nest a chained gpiochip\n"); | ||
1612 | return; | ||
1613 | } | ||
1614 | gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq, | 1606 | gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq, |
1615 | NULL); | 1607 | NULL); |
1616 | } | 1608 | } |
@@ -1626,10 +1618,11 @@ EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip); | |||
1626 | * gpiochip by assigning the gpiochip as chip data, and using the irqchip | 1618 | * gpiochip by assigning the gpiochip as chip data, and using the irqchip |
1627 | * stored inside the gpiochip. | 1619 | * stored inside the gpiochip. |
1628 | */ | 1620 | */ |
1629 | static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq, | 1621 | int gpiochip_irq_map(struct irq_domain *d, unsigned int irq, |
1630 | irq_hw_number_t hwirq) | 1622 | irq_hw_number_t hwirq) |
1631 | { | 1623 | { |
1632 | struct gpio_chip *chip = d->host_data; | 1624 | struct gpio_chip *chip = d->host_data; |
1625 | int err = 0; | ||
1633 | 1626 | ||
1634 | if (!gpiochip_irqchip_irq_valid(chip, hwirq)) | 1627 | if (!gpiochip_irqchip_irq_valid(chip, hwirq)) |
1635 | return -ENXIO; | 1628 | return -ENXIO; |
@@ -1639,32 +1632,42 @@ static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq, | |||
1639 | * This lock class tells lockdep that GPIO irqs are in a different | 1632 | * This lock class tells lockdep that GPIO irqs are in a different |
1640 | * category than their parents, so it won't report false recursion. | 1633 | * category than their parents, so it won't report false recursion. |
1641 | */ | 1634 | */ |
1642 | irq_set_lockdep_class(irq, chip->lock_key); | 1635 | irq_set_lockdep_class(irq, chip->irq.lock_key); |
1643 | irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler); | 1636 | irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler); |
1644 | /* Chips that use nested thread handlers have them marked */ | 1637 | /* Chips that use nested thread handlers have them marked */ |
1645 | if (chip->irq_nested) | 1638 | if (chip->irq.threaded) |
1646 | irq_set_nested_thread(irq, 1); | 1639 | irq_set_nested_thread(irq, 1); |
1647 | irq_set_noprobe(irq); | 1640 | irq_set_noprobe(irq); |
1648 | 1641 | ||
1642 | if (chip->irq.num_parents == 1) | ||
1643 | err = irq_set_parent(irq, chip->irq.parents[0]); | ||
1644 | else if (chip->irq.map) | ||
1645 | err = irq_set_parent(irq, chip->irq.map[hwirq]); | ||
1646 | |||
1647 | if (err < 0) | ||
1648 | return err; | ||
1649 | |||
1649 | /* | 1650 | /* |
1650 | * No set-up of the hardware will happen if IRQ_TYPE_NONE | 1651 | * No set-up of the hardware will happen if IRQ_TYPE_NONE |
1651 | * is passed as default type. | 1652 | * is passed as default type. |
1652 | */ | 1653 | */ |
1653 | if (chip->irq_default_type != IRQ_TYPE_NONE) | 1654 | if (chip->irq.default_type != IRQ_TYPE_NONE) |
1654 | irq_set_irq_type(irq, chip->irq_default_type); | 1655 | irq_set_irq_type(irq, chip->irq.default_type); |
1655 | 1656 | ||
1656 | return 0; | 1657 | return 0; |
1657 | } | 1658 | } |
1659 | EXPORT_SYMBOL_GPL(gpiochip_irq_map); | ||
1658 | 1660 | ||
1659 | static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq) | 1661 | void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq) |
1660 | { | 1662 | { |
1661 | struct gpio_chip *chip = d->host_data; | 1663 | struct gpio_chip *chip = d->host_data; |
1662 | 1664 | ||
1663 | if (chip->irq_nested) | 1665 | if (chip->irq.threaded) |
1664 | irq_set_nested_thread(irq, 0); | 1666 | irq_set_nested_thread(irq, 0); |
1665 | irq_set_chip_and_handler(irq, NULL, NULL); | 1667 | irq_set_chip_and_handler(irq, NULL, NULL); |
1666 | irq_set_chip_data(irq, NULL); | 1668 | irq_set_chip_data(irq, NULL); |
1667 | } | 1669 | } |
1670 | EXPORT_SYMBOL_GPL(gpiochip_irq_unmap); | ||
1668 | 1671 | ||
1669 | static const struct irq_domain_ops gpiochip_domain_ops = { | 1672 | static const struct irq_domain_ops gpiochip_domain_ops = { |
1670 | .map = gpiochip_irq_map, | 1673 | .map = gpiochip_irq_map, |
@@ -1702,7 +1705,94 @@ static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset) | |||
1702 | { | 1705 | { |
1703 | if (!gpiochip_irqchip_irq_valid(chip, offset)) | 1706 | if (!gpiochip_irqchip_irq_valid(chip, offset)) |
1704 | return -ENXIO; | 1707 | return -ENXIO; |
1705 | return irq_create_mapping(chip->irqdomain, offset); | 1708 | |
1709 | return irq_create_mapping(chip->irq.domain, offset); | ||
1710 | } | ||
1711 | |||
1712 | /** | ||
1713 | * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip | ||
1714 | * @gpiochip: the GPIO chip to add the IRQ chip to | ||
1715 | * @lock_key: lockdep class | ||
1716 | */ | ||
1717 | static int gpiochip_add_irqchip(struct gpio_chip *gpiochip, | ||
1718 | struct lock_class_key *lock_key) | ||
1719 | { | ||
1720 | struct irq_chip *irqchip = gpiochip->irq.chip; | ||
1721 | const struct irq_domain_ops *ops; | ||
1722 | struct device_node *np; | ||
1723 | unsigned int type; | ||
1724 | unsigned int i; | ||
1725 | |||
1726 | if (!irqchip) | ||
1727 | return 0; | ||
1728 | |||
1729 | if (gpiochip->irq.parent_handler && gpiochip->can_sleep) { | ||
1730 | chip_err(gpiochip, "you cannot have chained interrupts on a " | ||
1731 | "chip that may sleep\n"); | ||
1732 | return -EINVAL; | ||
1733 | } | ||
1734 | |||
1735 | np = gpiochip->gpiodev->dev.of_node; | ||
1736 | type = gpiochip->irq.default_type; | ||
1737 | |||
1738 | /* | ||
1739 | * Specifying a default trigger is a terrible idea if DT or ACPI is | ||
1740 | * used to configure the interrupts, as you may end up with | ||
1741 | * conflicting triggers. Tell the user, and reset to NONE. | ||
1742 | */ | ||
1743 | if (WARN(np && type != IRQ_TYPE_NONE, | ||
1744 | "%s: Ignoring %u default trigger\n", np->full_name, type)) | ||
1745 | type = IRQ_TYPE_NONE; | ||
1746 | |||
1747 | if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) { | ||
1748 | acpi_handle_warn(ACPI_HANDLE(gpiochip->parent), | ||
1749 | "Ignoring %u default trigger\n", type); | ||
1750 | type = IRQ_TYPE_NONE; | ||
1751 | } | ||
1752 | |||
1753 | gpiochip->to_irq = gpiochip_to_irq; | ||
1754 | gpiochip->irq.default_type = type; | ||
1755 | gpiochip->irq.lock_key = lock_key; | ||
1756 | |||
1757 | if (gpiochip->irq.domain_ops) | ||
1758 | ops = gpiochip->irq.domain_ops; | ||
1759 | else | ||
1760 | ops = &gpiochip_domain_ops; | ||
1761 | |||
1762 | gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio, | ||
1763 | gpiochip->irq.first, | ||
1764 | ops, gpiochip); | ||
1765 | if (!gpiochip->irq.domain) | ||
1766 | return -EINVAL; | ||
1767 | |||
1768 | /* | ||
1769 | * It is possible for a driver to override this, but only if the | ||
1770 | * alternative functions are both implemented. | ||
1771 | */ | ||
1772 | if (!irqchip->irq_request_resources && | ||
1773 | !irqchip->irq_release_resources) { | ||
1774 | irqchip->irq_request_resources = gpiochip_irq_reqres; | ||
1775 | irqchip->irq_release_resources = gpiochip_irq_relres; | ||
1776 | } | ||
1777 | |||
1778 | if (gpiochip->irq.parent_handler) { | ||
1779 | void *data = gpiochip->irq.parent_handler_data ?: gpiochip; | ||
1780 | |||
1781 | for (i = 0; i < gpiochip->irq.num_parents; i++) { | ||
1782 | /* | ||
1783 | * The parent IRQ chip is already using the chip_data | ||
1784 | * for this IRQ chip, so our callbacks simply use the | ||
1785 | * handler_data. | ||
1786 | */ | ||
1787 | irq_set_chained_handler_and_data(gpiochip->irq.parents[i], | ||
1788 | gpiochip->irq.parent_handler, | ||
1789 | data); | ||
1790 | } | ||
1791 | } | ||
1792 | |||
1793 | acpi_gpiochip_request_interrupts(gpiochip); | ||
1794 | |||
1795 | return 0; | ||
1706 | } | 1796 | } |
1707 | 1797 | ||
1708 | /** | 1798 | /** |
@@ -1717,26 +1807,34 @@ static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) | |||
1717 | 1807 | ||
1718 | acpi_gpiochip_free_interrupts(gpiochip); | 1808 | acpi_gpiochip_free_interrupts(gpiochip); |
1719 | 1809 | ||
1720 | if (gpiochip->irq_chained_parent) { | 1810 | if (gpiochip->irq.chip && gpiochip->irq.parent_handler) { |
1721 | irq_set_chained_handler(gpiochip->irq_chained_parent, NULL); | 1811 | struct gpio_irq_chip *irq = &gpiochip->irq; |
1722 | irq_set_handler_data(gpiochip->irq_chained_parent, NULL); | 1812 | unsigned int i; |
1813 | |||
1814 | for (i = 0; i < irq->num_parents; i++) | ||
1815 | irq_set_chained_handler_and_data(irq->parents[i], | ||
1816 | NULL, NULL); | ||
1723 | } | 1817 | } |
1724 | 1818 | ||
1725 | /* Remove all IRQ mappings and delete the domain */ | 1819 | /* Remove all IRQ mappings and delete the domain */ |
1726 | if (gpiochip->irqdomain) { | 1820 | if (gpiochip->irq.domain) { |
1821 | unsigned int irq; | ||
1822 | |||
1727 | for (offset = 0; offset < gpiochip->ngpio; offset++) { | 1823 | for (offset = 0; offset < gpiochip->ngpio; offset++) { |
1728 | if (!gpiochip_irqchip_irq_valid(gpiochip, offset)) | 1824 | if (!gpiochip_irqchip_irq_valid(gpiochip, offset)) |
1729 | continue; | 1825 | continue; |
1730 | irq_dispose_mapping( | 1826 | |
1731 | irq_find_mapping(gpiochip->irqdomain, offset)); | 1827 | irq = irq_find_mapping(gpiochip->irq.domain, offset); |
1828 | irq_dispose_mapping(irq); | ||
1732 | } | 1829 | } |
1733 | irq_domain_remove(gpiochip->irqdomain); | 1830 | |
1831 | irq_domain_remove(gpiochip->irq.domain); | ||
1734 | } | 1832 | } |
1735 | 1833 | ||
1736 | if (gpiochip->irqchip) { | 1834 | if (gpiochip->irq.chip) { |
1737 | gpiochip->irqchip->irq_request_resources = NULL; | 1835 | gpiochip->irq.chip->irq_request_resources = NULL; |
1738 | gpiochip->irqchip->irq_release_resources = NULL; | 1836 | gpiochip->irq.chip->irq_release_resources = NULL; |
1739 | gpiochip->irqchip = NULL; | 1837 | gpiochip->irq.chip = NULL; |
1740 | } | 1838 | } |
1741 | 1839 | ||
1742 | gpiochip_irqchip_free_valid_mask(gpiochip); | 1840 | gpiochip_irqchip_free_valid_mask(gpiochip); |
@@ -1751,8 +1849,7 @@ static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) | |||
1751 | * @handler: the irq handler to use (often a predefined irq core function) | 1849 | * @handler: the irq handler to use (often a predefined irq core function) |
1752 | * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE | 1850 | * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE |
1753 | * to have the core avoid setting up any default type in the hardware. | 1851 | * to have the core avoid setting up any default type in the hardware. |
1754 | * @nested: whether this is a nested irqchip calling handle_nested_irq() | 1852 | * @threaded: whether this irqchip uses a nested thread handler |
1755 | * in its IRQ handler | ||
1756 | * @lock_key: lockdep class | 1853 | * @lock_key: lockdep class |
1757 | * | 1854 | * |
1758 | * This function closely associates a certain irqchip with a certain | 1855 | * This function closely associates a certain irqchip with a certain |
@@ -1774,7 +1871,7 @@ int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip, | |||
1774 | unsigned int first_irq, | 1871 | unsigned int first_irq, |
1775 | irq_flow_handler_t handler, | 1872 | irq_flow_handler_t handler, |
1776 | unsigned int type, | 1873 | unsigned int type, |
1777 | bool nested, | 1874 | bool threaded, |
1778 | struct lock_class_key *lock_key) | 1875 | struct lock_class_key *lock_key) |
1779 | { | 1876 | { |
1780 | struct device_node *of_node; | 1877 | struct device_node *of_node; |
@@ -1786,7 +1883,7 @@ int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip, | |||
1786 | pr_err("missing gpiochip .dev parent pointer\n"); | 1883 | pr_err("missing gpiochip .dev parent pointer\n"); |
1787 | return -EINVAL; | 1884 | return -EINVAL; |
1788 | } | 1885 | } |
1789 | gpiochip->irq_nested = nested; | 1886 | gpiochip->irq.threaded = threaded; |
1790 | of_node = gpiochip->parent->of_node; | 1887 | of_node = gpiochip->parent->of_node; |
1791 | #ifdef CONFIG_OF_GPIO | 1888 | #ifdef CONFIG_OF_GPIO |
1792 | /* | 1889 | /* |
@@ -1811,16 +1908,16 @@ int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip, | |||
1811 | type = IRQ_TYPE_NONE; | 1908 | type = IRQ_TYPE_NONE; |
1812 | } | 1909 | } |
1813 | 1910 | ||
1814 | gpiochip->irqchip = irqchip; | 1911 | gpiochip->irq.chip = irqchip; |
1815 | gpiochip->irq_handler = handler; | 1912 | gpiochip->irq.handler = handler; |
1816 | gpiochip->irq_default_type = type; | 1913 | gpiochip->irq.default_type = type; |
1817 | gpiochip->to_irq = gpiochip_to_irq; | 1914 | gpiochip->to_irq = gpiochip_to_irq; |
1818 | gpiochip->lock_key = lock_key; | 1915 | gpiochip->irq.lock_key = lock_key; |
1819 | gpiochip->irqdomain = irq_domain_add_simple(of_node, | 1916 | gpiochip->irq.domain = irq_domain_add_simple(of_node, |
1820 | gpiochip->ngpio, first_irq, | 1917 | gpiochip->ngpio, first_irq, |
1821 | &gpiochip_domain_ops, gpiochip); | 1918 | &gpiochip_domain_ops, gpiochip); |
1822 | if (!gpiochip->irqdomain) { | 1919 | if (!gpiochip->irq.domain) { |
1823 | gpiochip->irqchip = NULL; | 1920 | gpiochip->irq.chip = NULL; |
1824 | return -EINVAL; | 1921 | return -EINVAL; |
1825 | } | 1922 | } |
1826 | 1923 | ||
@@ -1842,6 +1939,12 @@ EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key); | |||
1842 | 1939 | ||
1843 | #else /* CONFIG_GPIOLIB_IRQCHIP */ | 1940 | #else /* CONFIG_GPIOLIB_IRQCHIP */ |
1844 | 1941 | ||
1942 | static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip, | ||
1943 | struct lock_class_key *key) | ||
1944 | { | ||
1945 | return 0; | ||
1946 | } | ||
1947 | |||
1845 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {} | 1948 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {} |
1846 | static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip) | 1949 | static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip) |
1847 | { | 1950 | { |
@@ -2013,7 +2116,7 @@ EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges); | |||
2013 | * on each other, and help provide better diagnostics in debugfs. | 2116 | * on each other, and help provide better diagnostics in debugfs. |
2014 | * They're called even less than the "set direction" calls. | 2117 | * They're called even less than the "set direction" calls. |
2015 | */ | 2118 | */ |
2016 | static int __gpiod_request(struct gpio_desc *desc, const char *label) | 2119 | static int gpiod_request_commit(struct gpio_desc *desc, const char *label) |
2017 | { | 2120 | { |
2018 | struct gpio_chip *chip = desc->gdev->chip; | 2121 | struct gpio_chip *chip = desc->gdev->chip; |
2019 | int status; | 2122 | int status; |
@@ -2106,7 +2209,7 @@ int gpiod_request(struct gpio_desc *desc, const char *label) | |||
2106 | gdev = desc->gdev; | 2209 | gdev = desc->gdev; |
2107 | 2210 | ||
2108 | if (try_module_get(gdev->owner)) { | 2211 | if (try_module_get(gdev->owner)) { |
2109 | status = __gpiod_request(desc, label); | 2212 | status = gpiod_request_commit(desc, label); |
2110 | if (status < 0) | 2213 | if (status < 0) |
2111 | module_put(gdev->owner); | 2214 | module_put(gdev->owner); |
2112 | else | 2215 | else |
@@ -2119,7 +2222,7 @@ int gpiod_request(struct gpio_desc *desc, const char *label) | |||
2119 | return status; | 2222 | return status; |
2120 | } | 2223 | } |
2121 | 2224 | ||
2122 | static bool __gpiod_free(struct gpio_desc *desc) | 2225 | static bool gpiod_free_commit(struct gpio_desc *desc) |
2123 | { | 2226 | { |
2124 | bool ret = false; | 2227 | bool ret = false; |
2125 | unsigned long flags; | 2228 | unsigned long flags; |
@@ -2154,7 +2257,7 @@ static bool __gpiod_free(struct gpio_desc *desc) | |||
2154 | 2257 | ||
2155 | void gpiod_free(struct gpio_desc *desc) | 2258 | void gpiod_free(struct gpio_desc *desc) |
2156 | { | 2259 | { |
2157 | if (desc && desc->gdev && __gpiod_free(desc)) { | 2260 | if (desc && desc->gdev && gpiod_free_commit(desc)) { |
2158 | module_put(desc->gdev->owner); | 2261 | module_put(desc->gdev->owner); |
2159 | put_device(&desc->gdev->dev); | 2262 | put_device(&desc->gdev->dev); |
2160 | } else { | 2263 | } else { |
@@ -2217,7 +2320,7 @@ struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum, | |||
2217 | return desc; | 2320 | return desc; |
2218 | } | 2321 | } |
2219 | 2322 | ||
2220 | err = __gpiod_request(desc, label); | 2323 | err = gpiod_request_commit(desc, label); |
2221 | if (err < 0) | 2324 | if (err < 0) |
2222 | return ERR_PTR(err); | 2325 | return ERR_PTR(err); |
2223 | 2326 | ||
@@ -2235,7 +2338,7 @@ EXPORT_SYMBOL_GPL(gpiochip_request_own_desc); | |||
2235 | void gpiochip_free_own_desc(struct gpio_desc *desc) | 2338 | void gpiochip_free_own_desc(struct gpio_desc *desc) |
2236 | { | 2339 | { |
2237 | if (desc) | 2340 | if (desc) |
2238 | __gpiod_free(desc); | 2341 | gpiod_free_commit(desc); |
2239 | } | 2342 | } |
2240 | EXPORT_SYMBOL_GPL(gpiochip_free_own_desc); | 2343 | EXPORT_SYMBOL_GPL(gpiochip_free_own_desc); |
2241 | 2344 | ||
@@ -2291,44 +2394,12 @@ static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset, | |||
2291 | return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP; | 2394 | return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP; |
2292 | } | 2395 | } |
2293 | 2396 | ||
2294 | static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value) | 2397 | static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value) |
2295 | { | 2398 | { |
2296 | struct gpio_chip *gc = desc->gdev->chip; | 2399 | struct gpio_chip *gc = desc->gdev->chip; |
2297 | int val = !!value; | 2400 | int val = !!value; |
2298 | int ret; | 2401 | int ret; |
2299 | 2402 | ||
2300 | /* GPIOs used for IRQs shall not be set as output */ | ||
2301 | if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) { | ||
2302 | gpiod_err(desc, | ||
2303 | "%s: tried to set a GPIO tied to an IRQ as output\n", | ||
2304 | __func__); | ||
2305 | return -EIO; | ||
2306 | } | ||
2307 | |||
2308 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { | ||
2309 | /* First see if we can enable open drain in hardware */ | ||
2310 | ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc), | ||
2311 | PIN_CONFIG_DRIVE_OPEN_DRAIN); | ||
2312 | if (!ret) | ||
2313 | goto set_output_value; | ||
2314 | /* Emulate open drain by not actively driving the line high */ | ||
2315 | if (val) | ||
2316 | return gpiod_direction_input(desc); | ||
2317 | } | ||
2318 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) { | ||
2319 | ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc), | ||
2320 | PIN_CONFIG_DRIVE_OPEN_SOURCE); | ||
2321 | if (!ret) | ||
2322 | goto set_output_value; | ||
2323 | /* Emulate open source by not actively driving the line low */ | ||
2324 | if (!val) | ||
2325 | return gpiod_direction_input(desc); | ||
2326 | } else { | ||
2327 | gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc), | ||
2328 | PIN_CONFIG_DRIVE_PUSH_PULL); | ||
2329 | } | ||
2330 | |||
2331 | set_output_value: | ||
2332 | if (!gc->set || !gc->direction_output) { | 2403 | if (!gc->set || !gc->direction_output) { |
2333 | gpiod_warn(desc, | 2404 | gpiod_warn(desc, |
2334 | "%s: missing set() or direction_output() operations\n", | 2405 | "%s: missing set() or direction_output() operations\n", |
@@ -2358,7 +2429,7 @@ set_output_value: | |||
2358 | int gpiod_direction_output_raw(struct gpio_desc *desc, int value) | 2429 | int gpiod_direction_output_raw(struct gpio_desc *desc, int value) |
2359 | { | 2430 | { |
2360 | VALIDATE_DESC(desc); | 2431 | VALIDATE_DESC(desc); |
2361 | return _gpiod_direction_output_raw(desc, value); | 2432 | return gpiod_direction_output_raw_commit(desc, value); |
2362 | } | 2433 | } |
2363 | EXPORT_SYMBOL_GPL(gpiod_direction_output_raw); | 2434 | EXPORT_SYMBOL_GPL(gpiod_direction_output_raw); |
2364 | 2435 | ||
@@ -2376,12 +2447,48 @@ EXPORT_SYMBOL_GPL(gpiod_direction_output_raw); | |||
2376 | */ | 2447 | */ |
2377 | int gpiod_direction_output(struct gpio_desc *desc, int value) | 2448 | int gpiod_direction_output(struct gpio_desc *desc, int value) |
2378 | { | 2449 | { |
2450 | struct gpio_chip *gc = desc->gdev->chip; | ||
2451 | int ret; | ||
2452 | |||
2379 | VALIDATE_DESC(desc); | 2453 | VALIDATE_DESC(desc); |
2380 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | 2454 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
2381 | value = !value; | 2455 | value = !value; |
2382 | else | 2456 | else |
2383 | value = !!value; | 2457 | value = !!value; |
2384 | return _gpiod_direction_output_raw(desc, value); | 2458 | |
2459 | /* GPIOs used for IRQs shall not be set as output */ | ||
2460 | if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) { | ||
2461 | gpiod_err(desc, | ||
2462 | "%s: tried to set a GPIO tied to an IRQ as output\n", | ||
2463 | __func__); | ||
2464 | return -EIO; | ||
2465 | } | ||
2466 | |||
2467 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { | ||
2468 | /* First see if we can enable open drain in hardware */ | ||
2469 | ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc), | ||
2470 | PIN_CONFIG_DRIVE_OPEN_DRAIN); | ||
2471 | if (!ret) | ||
2472 | goto set_output_value; | ||
2473 | /* Emulate open drain by not actively driving the line high */ | ||
2474 | if (value) | ||
2475 | return gpiod_direction_input(desc); | ||
2476 | } | ||
2477 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) { | ||
2478 | ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc), | ||
2479 | PIN_CONFIG_DRIVE_OPEN_SOURCE); | ||
2480 | if (!ret) | ||
2481 | goto set_output_value; | ||
2482 | /* Emulate open source by not actively driving the line low */ | ||
2483 | if (!value) | ||
2484 | return gpiod_direction_input(desc); | ||
2485 | } else { | ||
2486 | gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc), | ||
2487 | PIN_CONFIG_DRIVE_PUSH_PULL); | ||
2488 | } | ||
2489 | |||
2490 | set_output_value: | ||
2491 | return gpiod_direction_output_raw_commit(desc, value); | ||
2385 | } | 2492 | } |
2386 | EXPORT_SYMBOL_GPL(gpiod_direction_output); | 2493 | EXPORT_SYMBOL_GPL(gpiod_direction_output); |
2387 | 2494 | ||
@@ -2448,7 +2555,7 @@ EXPORT_SYMBOL_GPL(gpiod_is_active_low); | |||
2448 | * that the GPIO was actually requested. | 2555 | * that the GPIO was actually requested. |
2449 | */ | 2556 | */ |
2450 | 2557 | ||
2451 | static int _gpiod_get_raw_value(const struct gpio_desc *desc) | 2558 | static int gpiod_get_raw_value_commit(const struct gpio_desc *desc) |
2452 | { | 2559 | { |
2453 | struct gpio_chip *chip; | 2560 | struct gpio_chip *chip; |
2454 | int offset; | 2561 | int offset; |
@@ -2462,6 +2569,71 @@ static int _gpiod_get_raw_value(const struct gpio_desc *desc) | |||
2462 | return value; | 2569 | return value; |
2463 | } | 2570 | } |
2464 | 2571 | ||
2572 | static int gpio_chip_get_multiple(struct gpio_chip *chip, | ||
2573 | unsigned long *mask, unsigned long *bits) | ||
2574 | { | ||
2575 | if (chip->get_multiple) { | ||
2576 | return chip->get_multiple(chip, mask, bits); | ||
2577 | } else if (chip->get) { | ||
2578 | int i, value; | ||
2579 | |||
2580 | for_each_set_bit(i, mask, chip->ngpio) { | ||
2581 | value = chip->get(chip, i); | ||
2582 | if (value < 0) | ||
2583 | return value; | ||
2584 | __assign_bit(i, bits, value); | ||
2585 | } | ||
2586 | return 0; | ||
2587 | } | ||
2588 | return -EIO; | ||
2589 | } | ||
2590 | |||
2591 | int gpiod_get_array_value_complex(bool raw, bool can_sleep, | ||
2592 | unsigned int array_size, | ||
2593 | struct gpio_desc **desc_array, | ||
2594 | int *value_array) | ||
2595 | { | ||
2596 | int i = 0; | ||
2597 | |||
2598 | while (i < array_size) { | ||
2599 | struct gpio_chip *chip = desc_array[i]->gdev->chip; | ||
2600 | unsigned long mask[BITS_TO_LONGS(chip->ngpio)]; | ||
2601 | unsigned long bits[BITS_TO_LONGS(chip->ngpio)]; | ||
2602 | int first, j, ret; | ||
2603 | |||
2604 | if (!can_sleep) | ||
2605 | WARN_ON(chip->can_sleep); | ||
2606 | |||
2607 | /* collect all inputs belonging to the same chip */ | ||
2608 | first = i; | ||
2609 | memset(mask, 0, sizeof(mask)); | ||
2610 | do { | ||
2611 | const struct gpio_desc *desc = desc_array[i]; | ||
2612 | int hwgpio = gpio_chip_hwgpio(desc); | ||
2613 | |||
2614 | __set_bit(hwgpio, mask); | ||
2615 | i++; | ||
2616 | } while ((i < array_size) && | ||
2617 | (desc_array[i]->gdev->chip == chip)); | ||
2618 | |||
2619 | ret = gpio_chip_get_multiple(chip, mask, bits); | ||
2620 | if (ret) | ||
2621 | return ret; | ||
2622 | |||
2623 | for (j = first; j < i; j++) { | ||
2624 | const struct gpio_desc *desc = desc_array[j]; | ||
2625 | int hwgpio = gpio_chip_hwgpio(desc); | ||
2626 | int value = test_bit(hwgpio, bits); | ||
2627 | |||
2628 | if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | ||
2629 | value = !value; | ||
2630 | value_array[j] = value; | ||
2631 | trace_gpio_value(desc_to_gpio(desc), 1, value); | ||
2632 | } | ||
2633 | } | ||
2634 | return 0; | ||
2635 | } | ||
2636 | |||
2465 | /** | 2637 | /** |
2466 | * gpiod_get_raw_value() - return a gpio's raw value | 2638 | * gpiod_get_raw_value() - return a gpio's raw value |
2467 | * @desc: gpio whose value will be returned | 2639 | * @desc: gpio whose value will be returned |
@@ -2477,7 +2649,7 @@ int gpiod_get_raw_value(const struct gpio_desc *desc) | |||
2477 | VALIDATE_DESC(desc); | 2649 | VALIDATE_DESC(desc); |
2478 | /* Should be using gpio_get_value_cansleep() */ | 2650 | /* Should be using gpio_get_value_cansleep() */ |
2479 | WARN_ON(desc->gdev->chip->can_sleep); | 2651 | WARN_ON(desc->gdev->chip->can_sleep); |
2480 | return _gpiod_get_raw_value(desc); | 2652 | return gpiod_get_raw_value_commit(desc); |
2481 | } | 2653 | } |
2482 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value); | 2654 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value); |
2483 | 2655 | ||
@@ -2499,7 +2671,7 @@ int gpiod_get_value(const struct gpio_desc *desc) | |||
2499 | /* Should be using gpio_get_value_cansleep() */ | 2671 | /* Should be using gpio_get_value_cansleep() */ |
2500 | WARN_ON(desc->gdev->chip->can_sleep); | 2672 | WARN_ON(desc->gdev->chip->can_sleep); |
2501 | 2673 | ||
2502 | value = _gpiod_get_raw_value(desc); | 2674 | value = gpiod_get_raw_value_commit(desc); |
2503 | if (value < 0) | 2675 | if (value < 0) |
2504 | return value; | 2676 | return value; |
2505 | 2677 | ||
@@ -2510,12 +2682,57 @@ int gpiod_get_value(const struct gpio_desc *desc) | |||
2510 | } | 2682 | } |
2511 | EXPORT_SYMBOL_GPL(gpiod_get_value); | 2683 | EXPORT_SYMBOL_GPL(gpiod_get_value); |
2512 | 2684 | ||
2685 | /** | ||
2686 | * gpiod_get_raw_array_value() - read raw values from an array of GPIOs | ||
2687 | * @array_size: number of elements in the descriptor / value arrays | ||
2688 | * @desc_array: array of GPIO descriptors whose values will be read | ||
2689 | * @value_array: array to store the read values | ||
2690 | * | ||
2691 | * Read the raw values of the GPIOs, i.e. the values of the physical lines | ||
2692 | * without regard for their ACTIVE_LOW status. Return 0 in case of success, | ||
2693 | * else an error code. | ||
2694 | * | ||
2695 | * This function should be called from contexts where we cannot sleep, | ||
2696 | * and it will complain if the GPIO chip functions potentially sleep. | ||
2697 | */ | ||
2698 | int gpiod_get_raw_array_value(unsigned int array_size, | ||
2699 | struct gpio_desc **desc_array, int *value_array) | ||
2700 | { | ||
2701 | if (!desc_array) | ||
2702 | return -EINVAL; | ||
2703 | return gpiod_get_array_value_complex(true, false, array_size, | ||
2704 | desc_array, value_array); | ||
2705 | } | ||
2706 | EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value); | ||
2707 | |||
2708 | /** | ||
2709 | * gpiod_get_array_value() - read values from an array of GPIOs | ||
2710 | * @array_size: number of elements in the descriptor / value arrays | ||
2711 | * @desc_array: array of GPIO descriptors whose values will be read | ||
2712 | * @value_array: array to store the read values | ||
2713 | * | ||
2714 | * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status | ||
2715 | * into account. Return 0 in case of success, else an error code. | ||
2716 | * | ||
2717 | * This function should be called from contexts where we cannot sleep, | ||
2718 | * and it will complain if the GPIO chip functions potentially sleep. | ||
2719 | */ | ||
2720 | int gpiod_get_array_value(unsigned int array_size, | ||
2721 | struct gpio_desc **desc_array, int *value_array) | ||
2722 | { | ||
2723 | if (!desc_array) | ||
2724 | return -EINVAL; | ||
2725 | return gpiod_get_array_value_complex(false, false, array_size, | ||
2726 | desc_array, value_array); | ||
2727 | } | ||
2728 | EXPORT_SYMBOL_GPL(gpiod_get_array_value); | ||
2729 | |||
2513 | /* | 2730 | /* |
2514 | * _gpio_set_open_drain_value() - Set the open drain gpio's value. | 2731 | * gpio_set_open_drain_value_commit() - Set the open drain gpio's value. |
2515 | * @desc: gpio descriptor whose state need to be set. | 2732 | * @desc: gpio descriptor whose state need to be set. |
2516 | * @value: Non-zero for setting it HIGH otherwise it will set to LOW. | 2733 | * @value: Non-zero for setting it HIGH otherwise it will set to LOW. |
2517 | */ | 2734 | */ |
2518 | static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value) | 2735 | static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value) |
2519 | { | 2736 | { |
2520 | int err = 0; | 2737 | int err = 0; |
2521 | struct gpio_chip *chip = desc->gdev->chip; | 2738 | struct gpio_chip *chip = desc->gdev->chip; |
@@ -2542,7 +2759,7 @@ static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value) | |||
2542 | * @desc: gpio descriptor whose state need to be set. | 2759 | * @desc: gpio descriptor whose state need to be set. |
2543 | * @value: Non-zero for setting it HIGH otherwise it will set to LOW. | 2760 | * @value: Non-zero for setting it HIGH otherwise it will set to LOW. |
2544 | */ | 2761 | */ |
2545 | static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value) | 2762 | static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value) |
2546 | { | 2763 | { |
2547 | int err = 0; | 2764 | int err = 0; |
2548 | struct gpio_chip *chip = desc->gdev->chip; | 2765 | struct gpio_chip *chip = desc->gdev->chip; |
@@ -2564,18 +2781,13 @@ static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value) | |||
2564 | __func__, err); | 2781 | __func__, err); |
2565 | } | 2782 | } |
2566 | 2783 | ||
2567 | static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value) | 2784 | static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value) |
2568 | { | 2785 | { |
2569 | struct gpio_chip *chip; | 2786 | struct gpio_chip *chip; |
2570 | 2787 | ||
2571 | chip = desc->gdev->chip; | 2788 | chip = desc->gdev->chip; |
2572 | trace_gpio_value(desc_to_gpio(desc), 0, value); | 2789 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
2573 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) | 2790 | chip->set(chip, gpio_chip_hwgpio(desc), value); |
2574 | _gpio_set_open_drain_value(desc, value); | ||
2575 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) | ||
2576 | _gpio_set_open_source_value(desc, value); | ||
2577 | else | ||
2578 | chip->set(chip, gpio_chip_hwgpio(desc), value); | ||
2579 | } | 2791 | } |
2580 | 2792 | ||
2581 | /* | 2793 | /* |
@@ -2630,10 +2842,10 @@ void gpiod_set_array_value_complex(bool raw, bool can_sleep, | |||
2630 | * collect all normal outputs belonging to the same chip | 2842 | * collect all normal outputs belonging to the same chip |
2631 | * open drain and open source outputs are set individually | 2843 | * open drain and open source outputs are set individually |
2632 | */ | 2844 | */ |
2633 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { | 2845 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) { |
2634 | _gpio_set_open_drain_value(desc, value); | 2846 | gpio_set_open_drain_value_commit(desc, value); |
2635 | } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) { | 2847 | } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) { |
2636 | _gpio_set_open_source_value(desc, value); | 2848 | gpio_set_open_source_value_commit(desc, value); |
2637 | } else { | 2849 | } else { |
2638 | __set_bit(hwgpio, mask); | 2850 | __set_bit(hwgpio, mask); |
2639 | if (value) | 2851 | if (value) |
@@ -2667,7 +2879,7 @@ void gpiod_set_raw_value(struct gpio_desc *desc, int value) | |||
2667 | VALIDATE_DESC_VOID(desc); | 2879 | VALIDATE_DESC_VOID(desc); |
2668 | /* Should be using gpiod_set_value_cansleep() */ | 2880 | /* Should be using gpiod_set_value_cansleep() */ |
2669 | WARN_ON(desc->gdev->chip->can_sleep); | 2881 | WARN_ON(desc->gdev->chip->can_sleep); |
2670 | _gpiod_set_raw_value(desc, value); | 2882 | gpiod_set_raw_value_commit(desc, value); |
2671 | } | 2883 | } |
2672 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value); | 2884 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value); |
2673 | 2885 | ||
@@ -2676,8 +2888,8 @@ EXPORT_SYMBOL_GPL(gpiod_set_raw_value); | |||
2676 | * @desc: gpio whose value will be assigned | 2888 | * @desc: gpio whose value will be assigned |
2677 | * @value: value to assign | 2889 | * @value: value to assign |
2678 | * | 2890 | * |
2679 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into | 2891 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW, |
2680 | * account | 2892 | * OPEN_DRAIN and OPEN_SOURCE flags into account. |
2681 | * | 2893 | * |
2682 | * This function should be called from contexts where we cannot sleep, and will | 2894 | * This function should be called from contexts where we cannot sleep, and will |
2683 | * complain if the GPIO chip functions potentially sleep. | 2895 | * complain if the GPIO chip functions potentially sleep. |
@@ -2689,7 +2901,12 @@ void gpiod_set_value(struct gpio_desc *desc, int value) | |||
2689 | WARN_ON(desc->gdev->chip->can_sleep); | 2901 | WARN_ON(desc->gdev->chip->can_sleep); |
2690 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | 2902 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
2691 | value = !value; | 2903 | value = !value; |
2692 | _gpiod_set_raw_value(desc, value); | 2904 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
2905 | gpio_set_open_drain_value_commit(desc, value); | ||
2906 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) | ||
2907 | gpio_set_open_source_value_commit(desc, value); | ||
2908 | else | ||
2909 | gpiod_set_raw_value_commit(desc, value); | ||
2693 | } | 2910 | } |
2694 | EXPORT_SYMBOL_GPL(gpiod_set_value); | 2911 | EXPORT_SYMBOL_GPL(gpiod_set_value); |
2695 | 2912 | ||
@@ -2890,7 +3107,7 @@ bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset) | |||
2890 | if (offset >= chip->ngpio) | 3107 | if (offset >= chip->ngpio) |
2891 | return false; | 3108 | return false; |
2892 | 3109 | ||
2893 | return !test_bit(FLAG_SLEEP_MAY_LOOSE_VALUE, | 3110 | return !test_bit(FLAG_SLEEP_MAY_LOSE_VALUE, |
2894 | &chip->gpiodev->descs[offset].flags); | 3111 | &chip->gpiodev->descs[offset].flags); |
2895 | } | 3112 | } |
2896 | EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent); | 3113 | EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent); |
@@ -2908,7 +3125,7 @@ int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) | |||
2908 | { | 3125 | { |
2909 | might_sleep_if(extra_checks); | 3126 | might_sleep_if(extra_checks); |
2910 | VALIDATE_DESC(desc); | 3127 | VALIDATE_DESC(desc); |
2911 | return _gpiod_get_raw_value(desc); | 3128 | return gpiod_get_raw_value_commit(desc); |
2912 | } | 3129 | } |
2913 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep); | 3130 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep); |
2914 | 3131 | ||
@@ -2927,7 +3144,7 @@ int gpiod_get_value_cansleep(const struct gpio_desc *desc) | |||
2927 | 3144 | ||
2928 | might_sleep_if(extra_checks); | 3145 | might_sleep_if(extra_checks); |
2929 | VALIDATE_DESC(desc); | 3146 | VALIDATE_DESC(desc); |
2930 | value = _gpiod_get_raw_value(desc); | 3147 | value = gpiod_get_raw_value_commit(desc); |
2931 | if (value < 0) | 3148 | if (value < 0) |
2932 | return value; | 3149 | return value; |
2933 | 3150 | ||
@@ -2939,6 +3156,53 @@ int gpiod_get_value_cansleep(const struct gpio_desc *desc) | |||
2939 | EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep); | 3156 | EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep); |
2940 | 3157 | ||
2941 | /** | 3158 | /** |
3159 | * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs | ||
3160 | * @array_size: number of elements in the descriptor / value arrays | ||
3161 | * @desc_array: array of GPIO descriptors whose values will be read | ||
3162 | * @value_array: array to store the read values | ||
3163 | * | ||
3164 | * Read the raw values of the GPIOs, i.e. the values of the physical lines | ||
3165 | * without regard for their ACTIVE_LOW status. Return 0 in case of success, | ||
3166 | * else an error code. | ||
3167 | * | ||
3168 | * This function is to be called from contexts that can sleep. | ||
3169 | */ | ||
3170 | int gpiod_get_raw_array_value_cansleep(unsigned int array_size, | ||
3171 | struct gpio_desc **desc_array, | ||
3172 | int *value_array) | ||
3173 | { | ||
3174 | might_sleep_if(extra_checks); | ||
3175 | if (!desc_array) | ||
3176 | return -EINVAL; | ||
3177 | return gpiod_get_array_value_complex(true, true, array_size, | ||
3178 | desc_array, value_array); | ||
3179 | } | ||
3180 | EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep); | ||
3181 | |||
3182 | /** | ||
3183 | * gpiod_get_array_value_cansleep() - read values from an array of GPIOs | ||
3184 | * @array_size: number of elements in the descriptor / value arrays | ||
3185 | * @desc_array: array of GPIO descriptors whose values will be read | ||
3186 | * @value_array: array to store the read values | ||
3187 | * | ||
3188 | * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status | ||
3189 | * into account. Return 0 in case of success, else an error code. | ||
3190 | * | ||
3191 | * This function is to be called from contexts that can sleep. | ||
3192 | */ | ||
3193 | int gpiod_get_array_value_cansleep(unsigned int array_size, | ||
3194 | struct gpio_desc **desc_array, | ||
3195 | int *value_array) | ||
3196 | { | ||
3197 | might_sleep_if(extra_checks); | ||
3198 | if (!desc_array) | ||
3199 | return -EINVAL; | ||
3200 | return gpiod_get_array_value_complex(false, true, array_size, | ||
3201 | desc_array, value_array); | ||
3202 | } | ||
3203 | EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep); | ||
3204 | |||
3205 | /** | ||
2942 | * gpiod_set_raw_value_cansleep() - assign a gpio's raw value | 3206 | * gpiod_set_raw_value_cansleep() - assign a gpio's raw value |
2943 | * @desc: gpio whose value will be assigned | 3207 | * @desc: gpio whose value will be assigned |
2944 | * @value: value to assign | 3208 | * @value: value to assign |
@@ -2952,7 +3216,7 @@ void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value) | |||
2952 | { | 3216 | { |
2953 | might_sleep_if(extra_checks); | 3217 | might_sleep_if(extra_checks); |
2954 | VALIDATE_DESC_VOID(desc); | 3218 | VALIDATE_DESC_VOID(desc); |
2955 | _gpiod_set_raw_value(desc, value); | 3219 | gpiod_set_raw_value_commit(desc, value); |
2956 | } | 3220 | } |
2957 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep); | 3221 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep); |
2958 | 3222 | ||
@@ -2972,7 +3236,7 @@ void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) | |||
2972 | VALIDATE_DESC_VOID(desc); | 3236 | VALIDATE_DESC_VOID(desc); |
2973 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | 3237 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
2974 | value = !value; | 3238 | value = !value; |
2975 | _gpiod_set_raw_value(desc, value); | 3239 | gpiod_set_raw_value_commit(desc, value); |
2976 | } | 3240 | } |
2977 | EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep); | 3241 | EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep); |
2978 | 3242 | ||
@@ -3268,8 +3532,8 @@ int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, | |||
3268 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); | 3532 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); |
3269 | if (lflags & GPIO_OPEN_SOURCE) | 3533 | if (lflags & GPIO_OPEN_SOURCE) |
3270 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); | 3534 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); |
3271 | if (lflags & GPIO_SLEEP_MAY_LOOSE_VALUE) | 3535 | if (lflags & GPIO_SLEEP_MAY_LOSE_VALUE) |
3272 | set_bit(FLAG_SLEEP_MAY_LOOSE_VALUE, &desc->flags); | 3536 | set_bit(FLAG_SLEEP_MAY_LOSE_VALUE, &desc->flags); |
3273 | 3537 | ||
3274 | /* No particular flag request, return here... */ | 3538 | /* No particular flag request, return here... */ |
3275 | if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) { | 3539 | if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) { |