aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorAlexandre Courbot <acourbot@nvidia.com>2014-07-25 10:38:36 -0400
committerLinus Walleij <linus.walleij@linaro.org>2014-07-28 06:28:05 -0400
commit39b2bbe3d715cf5013b5c48695ccdd25bd3bf120 (patch)
tree019129dbc7b1cc10200342602fe7eac47230219e /drivers
parent9b5b33f6256a941d9d34f219b508ba7140a39dba (diff)
gpio: add flags argument to gpiod_get*() functions
The huge majority of GPIOs have their direction and initial value set right after being obtained by one of the gpiod_get() functions. The integer GPIO API had gpio_request_one() that took a convenience flags parameter allowing to specify an direction and value applied to the returned GPIO. This feature greatly simplifies client code and ensures errors are always handled properly. A similar feature has been requested for the gpiod API. Since setting the direction of a GPIO is so often the very next action done after obtaining its descriptor, we prefer to extend the existing functions instead of introducing new functions that would raise the number of gpiod getters to 16 (!). The drawback of this approach is that all gpiod clients need to be updated. To limit the pain, temporary macros are introduced that allow gpiod_get*() to be called with or without the extra flags argument. They will be removed once all consumer code has been updated. Signed-off-by: Alexandre Courbot <acourbot@nvidia.com> Reviewed-by: Mark Brown <broonie@linaro.org> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/gpio/devres.c40
-rw-r--r--drivers/gpio/gpiolib.c67
2 files changed, 70 insertions, 37 deletions
diff --git a/drivers/gpio/devres.c b/drivers/gpio/devres.c
index 65978cf85f79..41b2f40578d5 100644
--- a/drivers/gpio/devres.c
+++ b/drivers/gpio/devres.c
@@ -39,47 +39,53 @@ static int devm_gpiod_match(struct device *dev, void *res, void *data)
39 * devm_gpiod_get - Resource-managed gpiod_get() 39 * devm_gpiod_get - Resource-managed gpiod_get()
40 * @dev: GPIO consumer 40 * @dev: GPIO consumer
41 * @con_id: function within the GPIO consumer 41 * @con_id: function within the GPIO consumer
42 * @flags: optional GPIO initialization flags
42 * 43 *
43 * Managed gpiod_get(). GPIO descriptors returned from this function are 44 * Managed gpiod_get(). GPIO descriptors returned from this function are
44 * automatically disposed on driver detach. See gpiod_get() for detailed 45 * automatically disposed on driver detach. See gpiod_get() for detailed
45 * information about behavior and return values. 46 * information about behavior and return values.
46 */ 47 */
47struct gpio_desc *__must_check devm_gpiod_get(struct device *dev, 48struct gpio_desc *__must_check __devm_gpiod_get(struct device *dev,
48 const char *con_id) 49 const char *con_id,
50 enum gpiod_flags flags)
49{ 51{
50 return devm_gpiod_get_index(dev, con_id, 0); 52 return devm_gpiod_get_index(dev, con_id, 0, flags);
51} 53}
52EXPORT_SYMBOL(devm_gpiod_get); 54EXPORT_SYMBOL(__devm_gpiod_get);
53 55
54/** 56/**
55 * devm_gpiod_get_optional - Resource-managed gpiod_get_optional() 57 * devm_gpiod_get_optional - Resource-managed gpiod_get_optional()
56 * @dev: GPIO consumer 58 * @dev: GPIO consumer
57 * @con_id: function within the GPIO consumer 59 * @con_id: function within the GPIO consumer
60 * @flags: optional GPIO initialization flags
58 * 61 *
59 * Managed gpiod_get_optional(). GPIO descriptors returned from this function 62 * Managed gpiod_get_optional(). GPIO descriptors returned from this function
60 * are automatically disposed on driver detach. See gpiod_get_optional() for 63 * are automatically disposed on driver detach. See gpiod_get_optional() for
61 * detailed information about behavior and return values. 64 * detailed information about behavior and return values.
62 */ 65 */
63struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev, 66struct gpio_desc *__must_check __devm_gpiod_get_optional(struct device *dev,
64 const char *con_id) 67 const char *con_id,
68 enum gpiod_flags flags)
65{ 69{
66 return devm_gpiod_get_index_optional(dev, con_id, 0); 70 return devm_gpiod_get_index_optional(dev, con_id, 0, flags);
67} 71}
68EXPORT_SYMBOL(devm_gpiod_get_optional); 72EXPORT_SYMBOL(__devm_gpiod_get_optional);
69 73
70/** 74/**
71 * devm_gpiod_get_index - Resource-managed gpiod_get_index() 75 * devm_gpiod_get_index - Resource-managed gpiod_get_index()
72 * @dev: GPIO consumer 76 * @dev: GPIO consumer
73 * @con_id: function within the GPIO consumer 77 * @con_id: function within the GPIO consumer
74 * @idx: index of the GPIO to obtain in the consumer 78 * @idx: index of the GPIO to obtain in the consumer
79 * @flags: optional GPIO initialization flags
75 * 80 *
76 * Managed gpiod_get_index(). GPIO descriptors returned from this function are 81 * Managed gpiod_get_index(). GPIO descriptors returned from this function are
77 * automatically disposed on driver detach. See gpiod_get_index() for detailed 82 * automatically disposed on driver detach. See gpiod_get_index() for detailed
78 * information about behavior and return values. 83 * information about behavior and return values.
79 */ 84 */
80struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, 85struct gpio_desc *__must_check __devm_gpiod_get_index(struct device *dev,
81 const char *con_id, 86 const char *con_id,
82 unsigned int idx) 87 unsigned int idx,
88 enum gpiod_flags flags)
83{ 89{
84 struct gpio_desc **dr; 90 struct gpio_desc **dr;
85 struct gpio_desc *desc; 91 struct gpio_desc *desc;
@@ -89,7 +95,7 @@ struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
89 if (!dr) 95 if (!dr)
90 return ERR_PTR(-ENOMEM); 96 return ERR_PTR(-ENOMEM);
91 97
92 desc = gpiod_get_index(dev, con_id, idx); 98 desc = gpiod_get_index(dev, con_id, idx, flags);
93 if (IS_ERR(desc)) { 99 if (IS_ERR(desc)) {
94 devres_free(dr); 100 devres_free(dr);
95 return desc; 101 return desc;
@@ -100,26 +106,28 @@ struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
100 106
101 return desc; 107 return desc;
102} 108}
103EXPORT_SYMBOL(devm_gpiod_get_index); 109EXPORT_SYMBOL(__devm_gpiod_get_index);
104 110
105/** 111/**
106 * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional() 112 * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional()
107 * @dev: GPIO consumer 113 * @dev: GPIO consumer
108 * @con_id: function within the GPIO consumer 114 * @con_id: function within the GPIO consumer
109 * @index: index of the GPIO to obtain in the consumer 115 * @index: index of the GPIO to obtain in the consumer
116 * @flags: optional GPIO initialization flags
110 * 117 *
111 * Managed gpiod_get_index_optional(). GPIO descriptors returned from this 118 * Managed gpiod_get_index_optional(). GPIO descriptors returned from this
112 * function are automatically disposed on driver detach. See 119 * function are automatically disposed on driver detach. See
113 * gpiod_get_index_optional() for detailed information about behavior and 120 * gpiod_get_index_optional() for detailed information about behavior and
114 * return values. 121 * return values.
115 */ 122 */
116struct gpio_desc *__must_check devm_gpiod_get_index_optional(struct device *dev, 123struct gpio_desc *__must_check __devm_gpiod_get_index_optional(struct device *dev,
117 const char *con_id, 124 const char *con_id,
118 unsigned int index) 125 unsigned int index,
126 enum gpiod_flags flags)
119{ 127{
120 struct gpio_desc *desc; 128 struct gpio_desc *desc;
121 129
122 desc = devm_gpiod_get_index(dev, con_id, index); 130 desc = devm_gpiod_get_index(dev, con_id, index, flags);
123 if (IS_ERR(desc)) { 131 if (IS_ERR(desc)) {
124 if (PTR_ERR(desc) == -ENOENT) 132 if (PTR_ERR(desc) == -ENOENT)
125 return NULL; 133 return NULL;
@@ -127,7 +135,7 @@ struct gpio_desc *__must_check devm_gpiod_get_index_optional(struct device *dev,
127 135
128 return desc; 136 return desc;
129} 137}
130EXPORT_SYMBOL(devm_gpiod_get_index_optional); 138EXPORT_SYMBOL(__devm_gpiod_get_index_optional);
131 139
132/** 140/**
133 * devm_gpiod_put - Resource-managed gpiod_put() 141 * devm_gpiod_put - Resource-managed gpiod_put()
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 330227581a25..15cc0bb65dda 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -1582,38 +1582,43 @@ static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
1582 * gpiod_get - obtain a GPIO for a given GPIO function 1582 * gpiod_get - obtain a GPIO for a given GPIO function
1583 * @dev: GPIO consumer, can be NULL for system-global GPIOs 1583 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1584 * @con_id: function within the GPIO consumer 1584 * @con_id: function within the GPIO consumer
1585 * @flags: optional GPIO initialization flags
1585 * 1586 *
1586 * Return the GPIO descriptor corresponding to the function con_id of device 1587 * Return the GPIO descriptor corresponding to the function con_id of device
1587 * dev, -ENOENT if no GPIO has been assigned to the requested function, or 1588 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
1588 * another IS_ERR() code if an error occured while trying to acquire the GPIO. 1589 * another IS_ERR() code if an error occured while trying to acquire the GPIO.
1589 */ 1590 */
1590struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id) 1591struct gpio_desc *__must_check __gpiod_get(struct device *dev, const char *con_id,
1592 enum gpiod_flags flags)
1591{ 1593{
1592 return gpiod_get_index(dev, con_id, 0); 1594 return gpiod_get_index(dev, con_id, 0, flags);
1593} 1595}
1594EXPORT_SYMBOL_GPL(gpiod_get); 1596EXPORT_SYMBOL_GPL(__gpiod_get);
1595 1597
1596/** 1598/**
1597 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function 1599 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
1598 * @dev: GPIO consumer, can be NULL for system-global GPIOs 1600 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1599 * @con_id: function within the GPIO consumer 1601 * @con_id: function within the GPIO consumer
1602 * @flags: optional GPIO initialization flags
1600 * 1603 *
1601 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to 1604 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
1602 * the requested function it will return NULL. This is convenient for drivers 1605 * the requested function it will return NULL. This is convenient for drivers
1603 * that need to handle optional GPIOs. 1606 * that need to handle optional GPIOs.
1604 */ 1607 */
1605struct gpio_desc *__must_check gpiod_get_optional(struct device *dev, 1608struct gpio_desc *__must_check __gpiod_get_optional(struct device *dev,
1606 const char *con_id) 1609 const char *con_id,
1610 enum gpiod_flags flags)
1607{ 1611{
1608 return gpiod_get_index_optional(dev, con_id, 0); 1612 return gpiod_get_index_optional(dev, con_id, 0, flags);
1609} 1613}
1610EXPORT_SYMBOL_GPL(gpiod_get_optional); 1614EXPORT_SYMBOL_GPL(__gpiod_get_optional);
1611 1615
1612/** 1616/**
1613 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function 1617 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
1614 * @dev: GPIO consumer, can be NULL for system-global GPIOs 1618 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1615 * @con_id: function within the GPIO consumer 1619 * @con_id: function within the GPIO consumer
1616 * @idx: index of the GPIO to obtain in the consumer 1620 * @idx: index of the GPIO to obtain in the consumer
1621 * @flags: optional GPIO initialization flags
1617 * 1622 *
1618 * This variant of gpiod_get() allows to access GPIOs other than the first 1623 * This variant of gpiod_get() allows to access GPIOs other than the first
1619 * defined one for functions that define several GPIOs. 1624 * defined one for functions that define several GPIOs.
@@ -1622,23 +1627,24 @@ EXPORT_SYMBOL_GPL(gpiod_get_optional);
1622 * requested function and/or index, or another IS_ERR() code if an error 1627 * requested function and/or index, or another IS_ERR() code if an error
1623 * occured while trying to acquire the GPIO. 1628 * occured while trying to acquire the GPIO.
1624 */ 1629 */
1625struct gpio_desc *__must_check gpiod_get_index(struct device *dev, 1630struct gpio_desc *__must_check __gpiod_get_index(struct device *dev,
1626 const char *con_id, 1631 const char *con_id,
1627 unsigned int idx) 1632 unsigned int idx,
1633 enum gpiod_flags flags)
1628{ 1634{
1629 struct gpio_desc *desc = NULL; 1635 struct gpio_desc *desc = NULL;
1630 int status; 1636 int status;
1631 enum gpio_lookup_flags flags = 0; 1637 enum gpio_lookup_flags lookupflags = 0;
1632 1638
1633 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id); 1639 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
1634 1640
1635 /* Using device tree? */ 1641 /* Using device tree? */
1636 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) { 1642 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
1637 dev_dbg(dev, "using device tree for GPIO lookup\n"); 1643 dev_dbg(dev, "using device tree for GPIO lookup\n");
1638 desc = of_find_gpio(dev, con_id, idx, &flags); 1644 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
1639 } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) { 1645 } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
1640 dev_dbg(dev, "using ACPI for GPIO lookup\n"); 1646 dev_dbg(dev, "using ACPI for GPIO lookup\n");
1641 desc = acpi_find_gpio(dev, con_id, idx, &flags); 1647 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
1642 } 1648 }
1643 1649
1644 /* 1650 /*
@@ -1647,7 +1653,7 @@ struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
1647 */ 1653 */
1648 if (!desc || desc == ERR_PTR(-ENOENT)) { 1654 if (!desc || desc == ERR_PTR(-ENOENT)) {
1649 dev_dbg(dev, "using lookup tables for GPIO lookup"); 1655 dev_dbg(dev, "using lookup tables for GPIO lookup");
1650 desc = gpiod_find(dev, con_id, idx, &flags); 1656 desc = gpiod_find(dev, con_id, idx, &lookupflags);
1651 } 1657 }
1652 1658
1653 if (IS_ERR(desc)) { 1659 if (IS_ERR(desc)) {
@@ -1660,16 +1666,33 @@ struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
1660 if (status < 0) 1666 if (status < 0)
1661 return ERR_PTR(status); 1667 return ERR_PTR(status);
1662 1668
1663 if (flags & GPIO_ACTIVE_LOW) 1669 if (lookupflags & GPIO_ACTIVE_LOW)
1664 set_bit(FLAG_ACTIVE_LOW, &desc->flags); 1670 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
1665 if (flags & GPIO_OPEN_DRAIN) 1671 if (lookupflags & GPIO_OPEN_DRAIN)
1666 set_bit(FLAG_OPEN_DRAIN, &desc->flags); 1672 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
1667 if (flags & GPIO_OPEN_SOURCE) 1673 if (lookupflags & GPIO_OPEN_SOURCE)
1668 set_bit(FLAG_OPEN_SOURCE, &desc->flags); 1674 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
1669 1675
1676 /* No particular flag request, return here... */
1677 if (flags & GPIOD_FLAGS_BIT_DIR_SET)
1678 return desc;
1679
1680 /* Process flags */
1681 if (flags & GPIOD_FLAGS_BIT_DIR_OUT)
1682 status = gpiod_direction_output(desc,
1683 flags & GPIOD_FLAGS_BIT_DIR_VAL);
1684 else
1685 status = gpiod_direction_input(desc);
1686
1687 if (status < 0) {
1688 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
1689 gpiod_put(desc);
1690 return ERR_PTR(status);
1691 }
1692
1670 return desc; 1693 return desc;
1671} 1694}
1672EXPORT_SYMBOL_GPL(gpiod_get_index); 1695EXPORT_SYMBOL_GPL(__gpiod_get_index);
1673 1696
1674/** 1697/**
1675 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO 1698 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
@@ -1677,18 +1700,20 @@ EXPORT_SYMBOL_GPL(gpiod_get_index);
1677 * @dev: GPIO consumer, can be NULL for system-global GPIOs 1700 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1678 * @con_id: function within the GPIO consumer 1701 * @con_id: function within the GPIO consumer
1679 * @index: index of the GPIO to obtain in the consumer 1702 * @index: index of the GPIO to obtain in the consumer
1703 * @flags: optional GPIO initialization flags
1680 * 1704 *
1681 * This is equivalent to gpiod_get_index(), except that when no GPIO with the 1705 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
1682 * specified index was assigned to the requested function it will return NULL. 1706 * specified index was assigned to the requested function it will return NULL.
1683 * This is convenient for drivers that need to handle optional GPIOs. 1707 * This is convenient for drivers that need to handle optional GPIOs.
1684 */ 1708 */
1685struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev, 1709struct gpio_desc *__must_check __gpiod_get_index_optional(struct device *dev,
1686 const char *con_id, 1710 const char *con_id,
1687 unsigned int index) 1711 unsigned int index,
1712 enum gpiod_flags flags)
1688{ 1713{
1689 struct gpio_desc *desc; 1714 struct gpio_desc *desc;
1690 1715
1691 desc = gpiod_get_index(dev, con_id, index); 1716 desc = gpiod_get_index(dev, con_id, index, flags);
1692 if (IS_ERR(desc)) { 1717 if (IS_ERR(desc)) {
1693 if (PTR_ERR(desc) == -ENOENT) 1718 if (PTR_ERR(desc) == -ENOENT)
1694 return NULL; 1719 return NULL;
@@ -1696,7 +1721,7 @@ struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
1696 1721
1697 return desc; 1722 return desc;
1698} 1723}
1699EXPORT_SYMBOL_GPL(gpiod_get_index_optional); 1724EXPORT_SYMBOL_GPL(__gpiod_get_index_optional);
1700 1725
1701/** 1726/**
1702 * gpiod_put - dispose of a GPIO descriptor 1727 * gpiod_put - dispose of a GPIO descriptor