diff options
| author | Thierry Reding <treding@nvidia.com> | 2014-04-25 11:10:06 -0400 |
|---|---|---|
| committer | Linus Walleij <linus.walleij@linaro.org> | 2014-05-09 07:48:30 -0400 |
| commit | 29a1f2333e07bbbecb920cc78fd035fe8f53207a (patch) | |
| tree | b2cb50d4c41ff4d1337cca888da3840f6f0d4d84 /include/linux/gpio | |
| parent | 91329132c909bbeb27699556b98b7f745ddb2ed9 (diff) | |
gpio: Add helpers for optional GPIOs
Introduce gpiod_get_optional() and gpiod_get_index_optional() helpers
that make it easier for drivers to handle optional GPIOs.
Currently in order to handle optional GPIOs, a driver needs to special
case error handling for -ENOENT, such as this:
gpio = gpiod_get(dev, "foo");
if (IS_ERR(gpio)) {
if (PTR_ERR(gpio) != -ENOENT)
return PTR_ERR(gpio);
gpio = NULL;
}
if (gpio) {
/* set up GPIO */
}
With these new helpers the above is reduced to:
gpio = gpiod_get_optional(dev, "foo");
if (IS_ERR(gpio))
return PTR_ERR(gpio);
if (gpio) {
/* set up GPIO */
}
While at it, device-managed variants of these functions are also
provided.
Signed-off-by: Thierry Reding <treding@nvidia.com>
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Diffstat (limited to 'include/linux/gpio')
| -rw-r--r-- | include/linux/gpio/consumer.h | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/include/linux/gpio/consumer.h b/include/linux/gpio/consumer.h index bed128e8f4b1..6a37ef0dc59c 100644 --- a/include/linux/gpio/consumer.h +++ b/include/linux/gpio/consumer.h | |||
| @@ -23,6 +23,12 @@ struct gpio_desc *__must_check gpiod_get(struct device *dev, | |||
| 23 | struct gpio_desc *__must_check gpiod_get_index(struct device *dev, | 23 | struct gpio_desc *__must_check gpiod_get_index(struct device *dev, |
| 24 | const char *con_id, | 24 | const char *con_id, |
| 25 | unsigned int idx); | 25 | unsigned int idx); |
| 26 | struct gpio_desc *__must_check gpiod_get_optional(struct device *dev, | ||
| 27 | const char *con_id); | ||
| 28 | struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev, | ||
| 29 | const char *con_id, | ||
| 30 | unsigned int index); | ||
| 31 | |||
| 26 | void gpiod_put(struct gpio_desc *desc); | 32 | void gpiod_put(struct gpio_desc *desc); |
| 27 | 33 | ||
| 28 | struct gpio_desc *__must_check devm_gpiod_get(struct device *dev, | 34 | struct gpio_desc *__must_check devm_gpiod_get(struct device *dev, |
| @@ -30,6 +36,12 @@ struct gpio_desc *__must_check devm_gpiod_get(struct device *dev, | |||
| 30 | struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, | 36 | struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, |
| 31 | const char *con_id, | 37 | const char *con_id, |
| 32 | unsigned int idx); | 38 | unsigned int idx); |
| 39 | struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev, | ||
| 40 | const char *con_id); | ||
| 41 | struct gpio_desc *__must_check | ||
| 42 | devm_gpiod_get_index_optional(struct device *dev, const char *con_id, | ||
| 43 | unsigned int index); | ||
| 44 | |||
| 33 | void devm_gpiod_put(struct device *dev, struct gpio_desc *desc); | 45 | void devm_gpiod_put(struct device *dev, struct gpio_desc *desc); |
| 34 | 46 | ||
| 35 | int gpiod_get_direction(const struct gpio_desc *desc); | 47 | int gpiod_get_direction(const struct gpio_desc *desc); |
| @@ -73,6 +85,20 @@ static inline struct gpio_desc *__must_check gpiod_get_index(struct device *dev, | |||
| 73 | { | 85 | { |
| 74 | return ERR_PTR(-ENOSYS); | 86 | return ERR_PTR(-ENOSYS); |
| 75 | } | 87 | } |
| 88 | |||
| 89 | static inline struct gpio_desc *__must_check | ||
| 90 | gpiod_get_optional(struct device *dev, const char *con_id) | ||
| 91 | { | ||
| 92 | return ERR_PTR(-ENOSYS); | ||
| 93 | } | ||
| 94 | |||
| 95 | static inline struct gpio_desc *__must_check | ||
| 96 | gpiod_get_index_optional(struct device *dev, const char *con_id, | ||
| 97 | unsigned int index) | ||
| 98 | { | ||
| 99 | return ERR_PTR(-ENOSYS); | ||
| 100 | } | ||
| 101 | |||
| 76 | static inline void gpiod_put(struct gpio_desc *desc) | 102 | static inline void gpiod_put(struct gpio_desc *desc) |
| 77 | { | 103 | { |
| 78 | might_sleep(); | 104 | might_sleep(); |
| @@ -93,6 +119,20 @@ struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, | |||
| 93 | { | 119 | { |
| 94 | return ERR_PTR(-ENOSYS); | 120 | return ERR_PTR(-ENOSYS); |
| 95 | } | 121 | } |
| 122 | |||
| 123 | static inline struct gpio_desc *__must_check | ||
| 124 | devm_gpiod_get_optional(struct device *dev, const char *con_id) | ||
| 125 | { | ||
| 126 | return ERR_PTR(-ENOSYS); | ||
| 127 | } | ||
| 128 | |||
| 129 | static inline struct gpio_desc *__must_check | ||
| 130 | devm_gpiod_get_index_optional(struct device *dev, const char *con_id, | ||
| 131 | unsigned int index) | ||
| 132 | { | ||
| 133 | return ERR_PTR(-ENOSYS); | ||
| 134 | } | ||
| 135 | |||
| 96 | static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc) | 136 | static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc) |
| 97 | { | 137 | { |
| 98 | might_sleep(); | 138 | might_sleep(); |
