Skip to content

Commit 39b2bbe

Browse files
Gnuroulinusw
authored andcommitted
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 <[email protected]> Reviewed-by: Mark Brown <[email protected]> Signed-off-by: Linus Walleij <[email protected]>
1 parent 9b5b33f commit 39b2bbe

File tree

4 files changed

+155
-59
lines changed

4 files changed

+155
-59
lines changed

Documentation/gpio/consumer.txt

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,24 @@ gpiod_get() functions. Like many other kernel subsystems, gpiod_get() takes the
2929
device that will use the GPIO and the function the requested GPIO is supposed to
3030
fulfill:
3131

32-
struct gpio_desc *gpiod_get(struct device *dev, const char *con_id)
32+
struct gpio_desc *gpiod_get(struct device *dev, const char *con_id,
33+
enum gpiod_flags flags)
3334

3435
If a function is implemented by using several GPIOs together (e.g. a simple LED
3536
device that displays digits), an additional index argument can be specified:
3637

3738
struct gpio_desc *gpiod_get_index(struct device *dev,
38-
const char *con_id, unsigned int idx)
39+
const char *con_id, unsigned int idx,
40+
enum gpiod_flags flags)
41+
42+
The flags parameter is used to optionally specify a direction and initial value
43+
for the GPIO. Values can be:
44+
45+
* GPIOD_ASIS or 0 to not initialize the GPIO at all. The direction must be set
46+
later with one of the dedicated functions.
47+
* GPIOD_IN to initialize the GPIO as input.
48+
* GPIOD_OUT_LOW to initialize the GPIO as output with a value of 0.
49+
* GPIOD_OUT_HIGH to initialize the GPIO as output with a value of 1.
3950

4051
Both functions return either a valid GPIO descriptor, or an error code checkable
4152
with IS_ERR() (they will never return a NULL pointer). -ENOENT will be returned
@@ -46,11 +57,13 @@ errors and an absence of GPIO for optional GPIO parameters.
4657

4758
Device-managed variants of these functions are also defined:
4859

49-
struct gpio_desc *devm_gpiod_get(struct device *dev, const char *con_id)
60+
struct gpio_desc *devm_gpiod_get(struct device *dev, const char *con_id,
61+
enum gpiod_flags flags)
5062

5163
struct gpio_desc *devm_gpiod_get_index(struct device *dev,
5264
const char *con_id,
53-
unsigned int idx)
65+
unsigned int idx,
66+
enum gpiod_flags flags)
5467

5568
A GPIO descriptor can be disposed of using the gpiod_put() function:
5669

@@ -67,8 +80,9 @@ Using GPIOs
6780

6881
Setting Direction
6982
-----------------
70-
The first thing a driver must do with a GPIO is setting its direction. This is
71-
done by invoking one of the gpiod_direction_*() functions:
83+
The first thing a driver must do with a GPIO is setting its direction. If no
84+
direction-setting flags have been given to gpiod_get*(), this is done by
85+
invoking one of the gpiod_direction_*() functions:
7286

7387
int gpiod_direction_input(struct gpio_desc *desc)
7488
int gpiod_direction_output(struct gpio_desc *desc, int value)

drivers/gpio/devres.c

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,47 +39,53 @@ static int devm_gpiod_match(struct device *dev, void *res, void *data)
3939
* devm_gpiod_get - Resource-managed gpiod_get()
4040
* @dev: GPIO consumer
4141
* @con_id: function within the GPIO consumer
42+
* @flags: optional GPIO initialization flags
4243
*
4344
* Managed gpiod_get(). GPIO descriptors returned from this function are
4445
* automatically disposed on driver detach. See gpiod_get() for detailed
4546
* information about behavior and return values.
4647
*/
47-
struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
48-
const char *con_id)
48+
struct gpio_desc *__must_check __devm_gpiod_get(struct device *dev,
49+
const char *con_id,
50+
enum gpiod_flags flags)
4951
{
50-
return devm_gpiod_get_index(dev, con_id, 0);
52+
return devm_gpiod_get_index(dev, con_id, 0, flags);
5153
}
52-
EXPORT_SYMBOL(devm_gpiod_get);
54+
EXPORT_SYMBOL(__devm_gpiod_get);
5355

5456
/**
5557
* devm_gpiod_get_optional - Resource-managed gpiod_get_optional()
5658
* @dev: GPIO consumer
5759
* @con_id: function within the GPIO consumer
60+
* @flags: optional GPIO initialization flags
5861
*
5962
* Managed gpiod_get_optional(). GPIO descriptors returned from this function
6063
* are automatically disposed on driver detach. See gpiod_get_optional() for
6164
* detailed information about behavior and return values.
6265
*/
63-
struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
64-
const char *con_id)
66+
struct gpio_desc *__must_check __devm_gpiod_get_optional(struct device *dev,
67+
const char *con_id,
68+
enum gpiod_flags flags)
6569
{
66-
return devm_gpiod_get_index_optional(dev, con_id, 0);
70+
return devm_gpiod_get_index_optional(dev, con_id, 0, flags);
6771
}
68-
EXPORT_SYMBOL(devm_gpiod_get_optional);
72+
EXPORT_SYMBOL(__devm_gpiod_get_optional);
6973

7074
/**
7175
* devm_gpiod_get_index - Resource-managed gpiod_get_index()
7276
* @dev: GPIO consumer
7377
* @con_id: function within the GPIO consumer
7478
* @idx: index of the GPIO to obtain in the consumer
79+
* @flags: optional GPIO initialization flags
7580
*
7681
* Managed gpiod_get_index(). GPIO descriptors returned from this function are
7782
* automatically disposed on driver detach. See gpiod_get_index() for detailed
7883
* information about behavior and return values.
7984
*/
80-
struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
85+
struct gpio_desc *__must_check __devm_gpiod_get_index(struct device *dev,
8186
const char *con_id,
82-
unsigned int idx)
87+
unsigned int idx,
88+
enum gpiod_flags flags)
8389
{
8490
struct gpio_desc **dr;
8591
struct gpio_desc *desc;
@@ -89,7 +95,7 @@ struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
8995
if (!dr)
9096
return ERR_PTR(-ENOMEM);
9197

92-
desc = gpiod_get_index(dev, con_id, idx);
98+
desc = gpiod_get_index(dev, con_id, idx, flags);
9399
if (IS_ERR(desc)) {
94100
devres_free(dr);
95101
return desc;
@@ -100,34 +106,36 @@ struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
100106

101107
return desc;
102108
}
103-
EXPORT_SYMBOL(devm_gpiod_get_index);
109+
EXPORT_SYMBOL(__devm_gpiod_get_index);
104110

105111
/**
106112
* devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional()
107113
* @dev: GPIO consumer
108114
* @con_id: function within the GPIO consumer
109115
* @index: index of the GPIO to obtain in the consumer
116+
* @flags: optional GPIO initialization flags
110117
*
111118
* Managed gpiod_get_index_optional(). GPIO descriptors returned from this
112119
* function are automatically disposed on driver detach. See
113120
* gpiod_get_index_optional() for detailed information about behavior and
114121
* return values.
115122
*/
116-
struct gpio_desc *__must_check devm_gpiod_get_index_optional(struct device *dev,
123+
struct gpio_desc *__must_check __devm_gpiod_get_index_optional(struct device *dev,
117124
const char *con_id,
118-
unsigned int index)
125+
unsigned int index,
126+
enum gpiod_flags flags)
119127
{
120128
struct gpio_desc *desc;
121129

122-
desc = devm_gpiod_get_index(dev, con_id, index);
130+
desc = devm_gpiod_get_index(dev, con_id, index, flags);
123131
if (IS_ERR(desc)) {
124132
if (PTR_ERR(desc) == -ENOENT)
125133
return NULL;
126134
}
127135

128136
return desc;
129137
}
130-
EXPORT_SYMBOL(devm_gpiod_get_index_optional);
138+
EXPORT_SYMBOL(__devm_gpiod_get_index_optional);
131139

132140
/**
133141
* devm_gpiod_put - Resource-managed gpiod_put()

drivers/gpio/gpiolib.c

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,38 +1582,43 @@ static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
15821582
* gpiod_get - obtain a GPIO for a given GPIO function
15831583
* @dev: GPIO consumer, can be NULL for system-global GPIOs
15841584
* @con_id: function within the GPIO consumer
1585+
* @flags: optional GPIO initialization flags
15851586
*
15861587
* Return the GPIO descriptor corresponding to the function con_id of device
15871588
* dev, -ENOENT if no GPIO has been assigned to the requested function, or
15881589
* another IS_ERR() code if an error occured while trying to acquire the GPIO.
15891590
*/
1590-
struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id)
1591+
struct gpio_desc *__must_check __gpiod_get(struct device *dev, const char *con_id,
1592+
enum gpiod_flags flags)
15911593
{
1592-
return gpiod_get_index(dev, con_id, 0);
1594+
return gpiod_get_index(dev, con_id, 0, flags);
15931595
}
1594-
EXPORT_SYMBOL_GPL(gpiod_get);
1596+
EXPORT_SYMBOL_GPL(__gpiod_get);
15951597

15961598
/**
15971599
* gpiod_get_optional - obtain an optional GPIO for a given GPIO function
15981600
* @dev: GPIO consumer, can be NULL for system-global GPIOs
15991601
* @con_id: function within the GPIO consumer
1602+
* @flags: optional GPIO initialization flags
16001603
*
16011604
* This is equivalent to gpiod_get(), except that when no GPIO was assigned to
16021605
* the requested function it will return NULL. This is convenient for drivers
16031606
* that need to handle optional GPIOs.
16041607
*/
1605-
struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
1606-
const char *con_id)
1608+
struct gpio_desc *__must_check __gpiod_get_optional(struct device *dev,
1609+
const char *con_id,
1610+
enum gpiod_flags flags)
16071611
{
1608-
return gpiod_get_index_optional(dev, con_id, 0);
1612+
return gpiod_get_index_optional(dev, con_id, 0, flags);
16091613
}
1610-
EXPORT_SYMBOL_GPL(gpiod_get_optional);
1614+
EXPORT_SYMBOL_GPL(__gpiod_get_optional);
16111615

16121616
/**
16131617
* gpiod_get_index - obtain a GPIO from a multi-index GPIO function
16141618
* @dev: GPIO consumer, can be NULL for system-global GPIOs
16151619
* @con_id: function within the GPIO consumer
16161620
* @idx: index of the GPIO to obtain in the consumer
1621+
* @flags: optional GPIO initialization flags
16171622
*
16181623
* This variant of gpiod_get() allows to access GPIOs other than the first
16191624
* defined one for functions that define several GPIOs.
@@ -1622,23 +1627,24 @@ EXPORT_SYMBOL_GPL(gpiod_get_optional);
16221627
* requested function and/or index, or another IS_ERR() code if an error
16231628
* occured while trying to acquire the GPIO.
16241629
*/
1625-
struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
1630+
struct gpio_desc *__must_check __gpiod_get_index(struct device *dev,
16261631
const char *con_id,
1627-
unsigned int idx)
1632+
unsigned int idx,
1633+
enum gpiod_flags flags)
16281634
{
16291635
struct gpio_desc *desc = NULL;
16301636
int status;
1631-
enum gpio_lookup_flags flags = 0;
1637+
enum gpio_lookup_flags lookupflags = 0;
16321638

16331639
dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
16341640

16351641
/* Using device tree? */
16361642
if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
16371643
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);
16391645
} else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
16401646
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);
16421648
}
16431649

16441650
/*
@@ -1647,7 +1653,7 @@ struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
16471653
*/
16481654
if (!desc || desc == ERR_PTR(-ENOENT)) {
16491655
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);
16511657
}
16521658

16531659
if (IS_ERR(desc)) {
@@ -1660,43 +1666,62 @@ struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
16601666
if (status < 0)
16611667
return ERR_PTR(status);
16621668

1663-
if (flags & GPIO_ACTIVE_LOW)
1669+
if (lookupflags & GPIO_ACTIVE_LOW)
16641670
set_bit(FLAG_ACTIVE_LOW, &desc->flags);
1665-
if (flags & GPIO_OPEN_DRAIN)
1671+
if (lookupflags & GPIO_OPEN_DRAIN)
16661672
set_bit(FLAG_OPEN_DRAIN, &desc->flags);
1667-
if (flags & GPIO_OPEN_SOURCE)
1673+
if (lookupflags & GPIO_OPEN_SOURCE)
16681674
set_bit(FLAG_OPEN_SOURCE, &desc->flags);
16691675

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+
16701693
return desc;
16711694
}
1672-
EXPORT_SYMBOL_GPL(gpiod_get_index);
1695+
EXPORT_SYMBOL_GPL(__gpiod_get_index);
16731696

16741697
/**
16751698
* gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
16761699
* function
16771700
* @dev: GPIO consumer, can be NULL for system-global GPIOs
16781701
* @con_id: function within the GPIO consumer
16791702
* @index: index of the GPIO to obtain in the consumer
1703+
* @flags: optional GPIO initialization flags
16801704
*
16811705
* This is equivalent to gpiod_get_index(), except that when no GPIO with the
16821706
* specified index was assigned to the requested function it will return NULL.
16831707
* This is convenient for drivers that need to handle optional GPIOs.
16841708
*/
1685-
struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
1709+
struct gpio_desc *__must_check __gpiod_get_index_optional(struct device *dev,
16861710
const char *con_id,
1687-
unsigned int index)
1711+
unsigned int index,
1712+
enum gpiod_flags flags)
16881713
{
16891714
struct gpio_desc *desc;
16901715

1691-
desc = gpiod_get_index(dev, con_id, index);
1716+
desc = gpiod_get_index(dev, con_id, index, flags);
16921717
if (IS_ERR(desc)) {
16931718
if (PTR_ERR(desc) == -ENOENT)
16941719
return NULL;
16951720
}
16961721

16971722
return desc;
16981723
}
1699-
EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
1724+
EXPORT_SYMBOL_GPL(__gpiod_get_index_optional);
17001725

17011726
/**
17021727
* gpiod_put - dispose of a GPIO descriptor

0 commit comments

Comments
 (0)