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 | |
Arnd Bergmann | cdf86cd2 | 2014-05-08 15:42:25 +0200 | [diff] [blame] | 4 | #include <linux/bug.h> |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 5 | #include <linux/err.h> |
| 6 | #include <linux/kernel.h> |
| 7 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 8 | struct device; |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 9 | |
| 10 | /** |
| 11 | * Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are |
| 12 | * preferable to the old integer-based handles. |
| 13 | * |
| 14 | * Contrary to integers, a pointer to a gpio_desc is guaranteed to be valid |
| 15 | * until the GPIO is released. |
| 16 | */ |
| 17 | struct gpio_desc; |
| 18 | |
Lars-Peter Clausen | a3485d0 | 2014-02-04 13:42:10 +0100 | [diff] [blame] | 19 | #ifdef CONFIG_GPIOLIB |
| 20 | |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 21 | #define GPIOD_FLAGS_BIT_DIR_SET BIT(0) |
| 22 | #define GPIOD_FLAGS_BIT_DIR_OUT BIT(1) |
| 23 | #define GPIOD_FLAGS_BIT_DIR_VAL BIT(2) |
| 24 | |
| 25 | /** |
| 26 | * Optional flags that can be passed to one of gpiod_* to configure direction |
| 27 | * and output value. These values cannot be OR'd. |
| 28 | */ |
| 29 | enum gpiod_flags { |
| 30 | GPIOD_ASIS = 0, |
| 31 | GPIOD_IN = GPIOD_FLAGS_BIT_DIR_SET, |
| 32 | GPIOD_OUT_LOW = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT, |
| 33 | GPIOD_OUT_HIGH = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT | |
| 34 | GPIOD_FLAGS_BIT_DIR_VAL, |
| 35 | }; |
| 36 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 37 | /* Acquire and dispose GPIOs */ |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 38 | struct gpio_desc *__must_check __gpiod_get(struct device *dev, |
| 39 | const char *con_id, |
| 40 | enum gpiod_flags flags); |
| 41 | #define __gpiod_get(dev, con_id, flags, ...) __gpiod_get(dev, con_id, flags) |
| 42 | #define gpiod_get(varargs...) __gpiod_get(varargs, 0) |
| 43 | struct gpio_desc *__must_check __gpiod_get_index(struct device *dev, |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 44 | const char *con_id, |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 45 | unsigned int idx, |
| 46 | enum gpiod_flags flags); |
| 47 | #define __gpiod_get_index(dev, con_id, index, flags, ...) \ |
| 48 | __gpiod_get_index(dev, con_id, index, flags) |
| 49 | #define gpiod_get_index(varargs...) __gpiod_get_index(varargs, 0) |
| 50 | struct gpio_desc *__must_check __gpiod_get_optional(struct device *dev, |
| 51 | const char *con_id, |
| 52 | enum gpiod_flags flags); |
| 53 | #define __gpiod_get_optional(dev, con_id, flags, ...) \ |
| 54 | __gpiod_get_optional(dev, con_id, flags) |
| 55 | #define gpiod_get_optional(varargs...) __gpiod_get_optional(varargs, 0) |
| 56 | struct gpio_desc *__must_check __gpiod_get_index_optional(struct device *dev, |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 57 | const char *con_id, |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 58 | unsigned int index, |
| 59 | enum gpiod_flags flags); |
| 60 | #define __gpiod_get_index_optional(dev, con_id, index, flags, ...) \ |
| 61 | __gpiod_get_index_optional(dev, con_id, index, flags) |
| 62 | #define gpiod_get_index_optional(varargs...) \ |
| 63 | __gpiod_get_index_optional(varargs, 0) |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 64 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 65 | void gpiod_put(struct gpio_desc *desc); |
| 66 | |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 67 | struct gpio_desc *__must_check __devm_gpiod_get(struct device *dev, |
| 68 | const char *con_id, |
| 69 | enum gpiod_flags flags); |
| 70 | #define __devm_gpiod_get(dev, con_id, flags, ...) \ |
| 71 | __devm_gpiod_get(dev, con_id, flags) |
| 72 | #define devm_gpiod_get(varargs...) __devm_gpiod_get(varargs, 0) |
| 73 | struct gpio_desc *__must_check __devm_gpiod_get_index(struct device *dev, |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 74 | const char *con_id, |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 75 | unsigned int idx, |
| 76 | enum gpiod_flags flags); |
| 77 | #define __devm_gpiod_get_index(dev, con_id, index, flags, ...) \ |
| 78 | __devm_gpiod_get_index(dev, con_id, index, flags) |
| 79 | #define devm_gpiod_get_index(varargs...) __devm_gpiod_get_index(varargs, 0) |
| 80 | struct gpio_desc *__must_check __devm_gpiod_get_optional(struct device *dev, |
| 81 | const char *con_id, |
| 82 | enum gpiod_flags flags); |
| 83 | #define __devm_gpiod_get_optional(dev, con_id, flags, ...) \ |
| 84 | __devm_gpiod_get_optional(dev, con_id, flags) |
| 85 | #define devm_gpiod_get_optional(varargs...) \ |
| 86 | __devm_gpiod_get_optional(varargs, 0) |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 87 | struct gpio_desc *__must_check |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 88 | __devm_gpiod_get_index_optional(struct device *dev, const char *con_id, |
| 89 | unsigned int index, enum gpiod_flags flags); |
| 90 | #define __devm_gpiod_get_index_optional(dev, con_id, index, flags, ...) \ |
| 91 | __devm_gpiod_get_index_optional(dev, con_id, index, flags) |
| 92 | #define devm_gpiod_get_index_optional(varargs...) \ |
| 93 | __devm_gpiod_get_index_optional(varargs, 0) |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 94 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 95 | void devm_gpiod_put(struct device *dev, struct gpio_desc *desc); |
| 96 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 97 | int gpiod_get_direction(const struct gpio_desc *desc); |
| 98 | int gpiod_direction_input(struct gpio_desc *desc); |
| 99 | int gpiod_direction_output(struct gpio_desc *desc, int value); |
Philipp Zabel | ef70bbe | 2014-01-07 12:34:11 +0100 | [diff] [blame] | 100 | int gpiod_direction_output_raw(struct gpio_desc *desc, int value); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 101 | |
| 102 | /* Value get/set from non-sleeping context */ |
| 103 | int gpiod_get_value(const struct gpio_desc *desc); |
| 104 | void gpiod_set_value(struct gpio_desc *desc, int value); |
| 105 | int gpiod_get_raw_value(const struct gpio_desc *desc); |
| 106 | void gpiod_set_raw_value(struct gpio_desc *desc, int value); |
| 107 | |
| 108 | /* Value get/set from sleeping context */ |
| 109 | int gpiod_get_value_cansleep(const struct gpio_desc *desc); |
| 110 | void gpiod_set_value_cansleep(struct gpio_desc *desc, int value); |
| 111 | int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc); |
| 112 | void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value); |
| 113 | |
| 114 | int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce); |
| 115 | |
| 116 | int gpiod_is_active_low(const struct gpio_desc *desc); |
| 117 | int gpiod_cansleep(const struct gpio_desc *desc); |
| 118 | |
| 119 | int gpiod_to_irq(const struct gpio_desc *desc); |
| 120 | |
| 121 | /* Convert between the old gpio_ and new gpiod_ interfaces */ |
| 122 | struct gpio_desc *gpio_to_desc(unsigned gpio); |
| 123 | int desc_to_gpio(const struct gpio_desc *desc); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 124 | |
| 125 | #else /* CONFIG_GPIOLIB */ |
| 126 | |
| 127 | static inline struct gpio_desc *__must_check gpiod_get(struct device *dev, |
| 128 | const char *con_id) |
| 129 | { |
| 130 | return ERR_PTR(-ENOSYS); |
| 131 | } |
| 132 | static inline struct gpio_desc *__must_check gpiod_get_index(struct device *dev, |
| 133 | const char *con_id, |
| 134 | unsigned int idx) |
| 135 | { |
| 136 | return ERR_PTR(-ENOSYS); |
| 137 | } |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 138 | |
| 139 | static inline struct gpio_desc *__must_check |
| 140 | gpiod_get_optional(struct device *dev, const char *con_id) |
| 141 | { |
| 142 | return ERR_PTR(-ENOSYS); |
| 143 | } |
| 144 | |
| 145 | static inline struct gpio_desc *__must_check |
| 146 | gpiod_get_index_optional(struct device *dev, const char *con_id, |
| 147 | unsigned int index) |
| 148 | { |
| 149 | return ERR_PTR(-ENOSYS); |
| 150 | } |
| 151 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 152 | static inline void gpiod_put(struct gpio_desc *desc) |
| 153 | { |
| 154 | might_sleep(); |
| 155 | |
| 156 | /* GPIO can never have been requested */ |
| 157 | WARN_ON(1); |
| 158 | } |
| 159 | |
| 160 | static inline struct gpio_desc *__must_check devm_gpiod_get(struct device *dev, |
| 161 | const char *con_id) |
| 162 | { |
| 163 | return ERR_PTR(-ENOSYS); |
| 164 | } |
| 165 | static inline |
| 166 | struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, |
| 167 | const char *con_id, |
| 168 | unsigned int idx) |
| 169 | { |
| 170 | return ERR_PTR(-ENOSYS); |
| 171 | } |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 172 | |
| 173 | static inline struct gpio_desc *__must_check |
| 174 | devm_gpiod_get_optional(struct device *dev, const char *con_id) |
| 175 | { |
| 176 | return ERR_PTR(-ENOSYS); |
| 177 | } |
| 178 | |
| 179 | static inline struct gpio_desc *__must_check |
| 180 | devm_gpiod_get_index_optional(struct device *dev, const char *con_id, |
| 181 | unsigned int index) |
| 182 | { |
| 183 | return ERR_PTR(-ENOSYS); |
| 184 | } |
| 185 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 186 | static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc) |
| 187 | { |
| 188 | might_sleep(); |
| 189 | |
| 190 | /* GPIO can never have been requested */ |
| 191 | WARN_ON(1); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | static inline int gpiod_get_direction(const struct gpio_desc *desc) |
| 196 | { |
| 197 | /* GPIO can never have been requested */ |
| 198 | WARN_ON(1); |
| 199 | return -ENOSYS; |
| 200 | } |
| 201 | static inline int gpiod_direction_input(struct gpio_desc *desc) |
| 202 | { |
| 203 | /* GPIO can never have been requested */ |
| 204 | WARN_ON(1); |
| 205 | return -ENOSYS; |
| 206 | } |
| 207 | static inline int gpiod_direction_output(struct gpio_desc *desc, int value) |
| 208 | { |
| 209 | /* GPIO can never have been requested */ |
| 210 | WARN_ON(1); |
| 211 | return -ENOSYS; |
| 212 | } |
Philipp Zabel | ef70bbe | 2014-01-07 12:34:11 +0100 | [diff] [blame] | 213 | static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value) |
| 214 | { |
| 215 | /* GPIO can never have been requested */ |
| 216 | WARN_ON(1); |
| 217 | return -ENOSYS; |
| 218 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 219 | |
| 220 | |
| 221 | static inline int gpiod_get_value(const struct gpio_desc *desc) |
| 222 | { |
| 223 | /* GPIO can never have been requested */ |
| 224 | WARN_ON(1); |
| 225 | return 0; |
| 226 | } |
| 227 | static inline void gpiod_set_value(struct gpio_desc *desc, int value) |
| 228 | { |
| 229 | /* GPIO can never have been requested */ |
| 230 | WARN_ON(1); |
| 231 | } |
| 232 | static inline int gpiod_get_raw_value(const struct gpio_desc *desc) |
| 233 | { |
| 234 | /* GPIO can never have been requested */ |
| 235 | WARN_ON(1); |
| 236 | return 0; |
| 237 | } |
| 238 | static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value) |
| 239 | { |
| 240 | /* GPIO can never have been requested */ |
| 241 | WARN_ON(1); |
| 242 | } |
| 243 | |
| 244 | static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc) |
| 245 | { |
| 246 | /* GPIO can never have been requested */ |
| 247 | WARN_ON(1); |
| 248 | return 0; |
| 249 | } |
| 250 | static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) |
| 251 | { |
| 252 | /* GPIO can never have been requested */ |
| 253 | WARN_ON(1); |
| 254 | } |
| 255 | static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) |
| 256 | { |
| 257 | /* GPIO can never have been requested */ |
| 258 | WARN_ON(1); |
| 259 | return 0; |
| 260 | } |
| 261 | static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, |
| 262 | int value) |
| 263 | { |
| 264 | /* GPIO can never have been requested */ |
| 265 | WARN_ON(1); |
| 266 | } |
| 267 | |
| 268 | static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) |
| 269 | { |
| 270 | /* GPIO can never have been requested */ |
| 271 | WARN_ON(1); |
| 272 | return -ENOSYS; |
| 273 | } |
| 274 | |
| 275 | static inline int gpiod_is_active_low(const struct gpio_desc *desc) |
| 276 | { |
| 277 | /* GPIO can never have been requested */ |
| 278 | WARN_ON(1); |
| 279 | return 0; |
| 280 | } |
| 281 | static inline int gpiod_cansleep(const struct gpio_desc *desc) |
| 282 | { |
| 283 | /* GPIO can never have been requested */ |
| 284 | WARN_ON(1); |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | static inline int gpiod_to_irq(const struct gpio_desc *desc) |
| 289 | { |
| 290 | /* GPIO can never have been requested */ |
| 291 | WARN_ON(1); |
| 292 | return -EINVAL; |
| 293 | } |
| 294 | |
| 295 | static inline struct gpio_desc *gpio_to_desc(unsigned gpio) |
| 296 | { |
| 297 | return ERR_PTR(-EINVAL); |
| 298 | } |
| 299 | static inline int desc_to_gpio(const struct gpio_desc *desc) |
| 300 | { |
| 301 | /* GPIO can never have been requested */ |
| 302 | WARN_ON(1); |
| 303 | return -EINVAL; |
| 304 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 305 | |
| 306 | |
| 307 | #endif /* CONFIG_GPIOLIB */ |
| 308 | |
| 309 | #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS) |
| 310 | |
| 311 | int gpiod_export(struct gpio_desc *desc, bool direction_may_change); |
| 312 | int gpiod_export_link(struct device *dev, const char *name, |
| 313 | struct gpio_desc *desc); |
| 314 | int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value); |
| 315 | void gpiod_unexport(struct gpio_desc *desc); |
| 316 | |
| 317 | #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ |
| 318 | |
| 319 | static inline int gpiod_export(struct gpio_desc *desc, |
| 320 | bool direction_may_change) |
| 321 | { |
| 322 | return -ENOSYS; |
| 323 | } |
| 324 | |
| 325 | static inline int gpiod_export_link(struct device *dev, const char *name, |
| 326 | struct gpio_desc *desc) |
| 327 | { |
| 328 | return -ENOSYS; |
| 329 | } |
| 330 | |
| 331 | static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) |
| 332 | { |
| 333 | return -ENOSYS; |
| 334 | } |
| 335 | |
| 336 | static inline void gpiod_unexport(struct gpio_desc *desc) |
| 337 | { |
| 338 | } |
| 339 | |
| 340 | #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ |
| 341 | |
| 342 | #endif |