Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1 | #ifndef __LINUX_GPIO_CONSUMER_H |
| 2 | #define __LINUX_GPIO_CONSUMER_H |
| 3 | |
| 4 | #include <linux/err.h> |
| 5 | #include <linux/kernel.h> |
| 6 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 7 | struct device; |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 8 | |
| 9 | /** |
| 10 | * Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are |
| 11 | * preferable to the old integer-based handles. |
| 12 | * |
| 13 | * Contrary to integers, a pointer to a gpio_desc is guaranteed to be valid |
| 14 | * until the GPIO is released. |
| 15 | */ |
| 16 | struct gpio_desc; |
| 17 | |
Lars-Peter Clausen | a3485d0 | 2014-02-04 13:42:10 +0100 | [diff] [blame] | 18 | #ifdef CONFIG_GPIOLIB |
| 19 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 20 | /* Acquire and dispose GPIOs */ |
| 21 | struct gpio_desc *__must_check gpiod_get(struct device *dev, |
| 22 | const char *con_id); |
| 23 | struct gpio_desc *__must_check gpiod_get_index(struct device *dev, |
| 24 | const char *con_id, |
| 25 | unsigned int idx); |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame^] | 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 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 32 | void gpiod_put(struct gpio_desc *desc); |
| 33 | |
| 34 | struct gpio_desc *__must_check devm_gpiod_get(struct device *dev, |
| 35 | const char *con_id); |
| 36 | struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, |
| 37 | const char *con_id, |
| 38 | unsigned int idx); |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame^] | 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 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 45 | void devm_gpiod_put(struct device *dev, struct gpio_desc *desc); |
| 46 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 47 | int gpiod_get_direction(const struct gpio_desc *desc); |
| 48 | int gpiod_direction_input(struct gpio_desc *desc); |
| 49 | int gpiod_direction_output(struct gpio_desc *desc, int value); |
Philipp Zabel | ef70bbe | 2014-01-07 12:34:11 +0100 | [diff] [blame] | 50 | int gpiod_direction_output_raw(struct gpio_desc *desc, int value); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 51 | |
| 52 | /* Value get/set from non-sleeping context */ |
| 53 | int gpiod_get_value(const struct gpio_desc *desc); |
| 54 | void gpiod_set_value(struct gpio_desc *desc, int value); |
| 55 | int gpiod_get_raw_value(const struct gpio_desc *desc); |
| 56 | void gpiod_set_raw_value(struct gpio_desc *desc, int value); |
| 57 | |
| 58 | /* Value get/set from sleeping context */ |
| 59 | int gpiod_get_value_cansleep(const struct gpio_desc *desc); |
| 60 | void gpiod_set_value_cansleep(struct gpio_desc *desc, int value); |
| 61 | int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc); |
| 62 | void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value); |
| 63 | |
| 64 | int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce); |
| 65 | |
| 66 | int gpiod_is_active_low(const struct gpio_desc *desc); |
| 67 | int gpiod_cansleep(const struct gpio_desc *desc); |
| 68 | |
| 69 | int gpiod_to_irq(const struct gpio_desc *desc); |
| 70 | |
| 71 | /* Convert between the old gpio_ and new gpiod_ interfaces */ |
| 72 | struct gpio_desc *gpio_to_desc(unsigned gpio); |
| 73 | int desc_to_gpio(const struct gpio_desc *desc); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 74 | |
| 75 | #else /* CONFIG_GPIOLIB */ |
| 76 | |
| 77 | static inline struct gpio_desc *__must_check gpiod_get(struct device *dev, |
| 78 | const char *con_id) |
| 79 | { |
| 80 | return ERR_PTR(-ENOSYS); |
| 81 | } |
| 82 | static inline struct gpio_desc *__must_check gpiod_get_index(struct device *dev, |
| 83 | const char *con_id, |
| 84 | unsigned int idx) |
| 85 | { |
| 86 | return ERR_PTR(-ENOSYS); |
| 87 | } |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame^] | 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 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 102 | static inline void gpiod_put(struct gpio_desc *desc) |
| 103 | { |
| 104 | might_sleep(); |
| 105 | |
| 106 | /* GPIO can never have been requested */ |
| 107 | WARN_ON(1); |
| 108 | } |
| 109 | |
| 110 | static inline struct gpio_desc *__must_check devm_gpiod_get(struct device *dev, |
| 111 | const char *con_id) |
| 112 | { |
| 113 | return ERR_PTR(-ENOSYS); |
| 114 | } |
| 115 | static inline |
| 116 | struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, |
| 117 | const char *con_id, |
| 118 | unsigned int idx) |
| 119 | { |
| 120 | return ERR_PTR(-ENOSYS); |
| 121 | } |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame^] | 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 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 136 | static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc) |
| 137 | { |
| 138 | might_sleep(); |
| 139 | |
| 140 | /* GPIO can never have been requested */ |
| 141 | WARN_ON(1); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | static inline int gpiod_get_direction(const struct gpio_desc *desc) |
| 146 | { |
| 147 | /* GPIO can never have been requested */ |
| 148 | WARN_ON(1); |
| 149 | return -ENOSYS; |
| 150 | } |
| 151 | static inline int gpiod_direction_input(struct gpio_desc *desc) |
| 152 | { |
| 153 | /* GPIO can never have been requested */ |
| 154 | WARN_ON(1); |
| 155 | return -ENOSYS; |
| 156 | } |
| 157 | static inline int gpiod_direction_output(struct gpio_desc *desc, int value) |
| 158 | { |
| 159 | /* GPIO can never have been requested */ |
| 160 | WARN_ON(1); |
| 161 | return -ENOSYS; |
| 162 | } |
Philipp Zabel | ef70bbe | 2014-01-07 12:34:11 +0100 | [diff] [blame] | 163 | static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value) |
| 164 | { |
| 165 | /* GPIO can never have been requested */ |
| 166 | WARN_ON(1); |
| 167 | return -ENOSYS; |
| 168 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 169 | |
| 170 | |
| 171 | static inline int gpiod_get_value(const struct gpio_desc *desc) |
| 172 | { |
| 173 | /* GPIO can never have been requested */ |
| 174 | WARN_ON(1); |
| 175 | return 0; |
| 176 | } |
| 177 | static inline void gpiod_set_value(struct gpio_desc *desc, int value) |
| 178 | { |
| 179 | /* GPIO can never have been requested */ |
| 180 | WARN_ON(1); |
| 181 | } |
| 182 | static inline int gpiod_get_raw_value(const struct gpio_desc *desc) |
| 183 | { |
| 184 | /* GPIO can never have been requested */ |
| 185 | WARN_ON(1); |
| 186 | return 0; |
| 187 | } |
| 188 | static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value) |
| 189 | { |
| 190 | /* GPIO can never have been requested */ |
| 191 | WARN_ON(1); |
| 192 | } |
| 193 | |
| 194 | static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc) |
| 195 | { |
| 196 | /* GPIO can never have been requested */ |
| 197 | WARN_ON(1); |
| 198 | return 0; |
| 199 | } |
| 200 | static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) |
| 201 | { |
| 202 | /* GPIO can never have been requested */ |
| 203 | WARN_ON(1); |
| 204 | } |
| 205 | static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) |
| 206 | { |
| 207 | /* GPIO can never have been requested */ |
| 208 | WARN_ON(1); |
| 209 | return 0; |
| 210 | } |
| 211 | static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, |
| 212 | int value) |
| 213 | { |
| 214 | /* GPIO can never have been requested */ |
| 215 | WARN_ON(1); |
| 216 | } |
| 217 | |
| 218 | static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) |
| 219 | { |
| 220 | /* GPIO can never have been requested */ |
| 221 | WARN_ON(1); |
| 222 | return -ENOSYS; |
| 223 | } |
| 224 | |
| 225 | static inline int gpiod_is_active_low(const struct gpio_desc *desc) |
| 226 | { |
| 227 | /* GPIO can never have been requested */ |
| 228 | WARN_ON(1); |
| 229 | return 0; |
| 230 | } |
| 231 | static inline int gpiod_cansleep(const struct gpio_desc *desc) |
| 232 | { |
| 233 | /* GPIO can never have been requested */ |
| 234 | WARN_ON(1); |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static inline int gpiod_to_irq(const struct gpio_desc *desc) |
| 239 | { |
| 240 | /* GPIO can never have been requested */ |
| 241 | WARN_ON(1); |
| 242 | return -EINVAL; |
| 243 | } |
| 244 | |
| 245 | static inline struct gpio_desc *gpio_to_desc(unsigned gpio) |
| 246 | { |
| 247 | return ERR_PTR(-EINVAL); |
| 248 | } |
| 249 | static inline int desc_to_gpio(const struct gpio_desc *desc) |
| 250 | { |
| 251 | /* GPIO can never have been requested */ |
| 252 | WARN_ON(1); |
| 253 | return -EINVAL; |
| 254 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 255 | |
| 256 | |
| 257 | #endif /* CONFIG_GPIOLIB */ |
| 258 | |
| 259 | #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS) |
| 260 | |
| 261 | int gpiod_export(struct gpio_desc *desc, bool direction_may_change); |
| 262 | int gpiod_export_link(struct device *dev, const char *name, |
| 263 | struct gpio_desc *desc); |
| 264 | int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value); |
| 265 | void gpiod_unexport(struct gpio_desc *desc); |
| 266 | |
| 267 | #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ |
| 268 | |
| 269 | static inline int gpiod_export(struct gpio_desc *desc, |
| 270 | bool direction_may_change) |
| 271 | { |
| 272 | return -ENOSYS; |
| 273 | } |
| 274 | |
| 275 | static inline int gpiod_export_link(struct device *dev, const char *name, |
| 276 | struct gpio_desc *desc) |
| 277 | { |
| 278 | return -ENOSYS; |
| 279 | } |
| 280 | |
| 281 | static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) |
| 282 | { |
| 283 | return -ENOSYS; |
| 284 | } |
| 285 | |
| 286 | static inline void gpiod_unexport(struct gpio_desc *desc) |
| 287 | { |
| 288 | } |
| 289 | |
| 290 | #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ |
| 291 | |
| 292 | #endif |