at v4.17 553 lines 15 kB view raw
1/* SPDX-License-Identifier: GPL-2.0 */ 2#ifndef __LINUX_GPIO_CONSUMER_H 3#define __LINUX_GPIO_CONSUMER_H 4 5#include <linux/bug.h> 6#include <linux/err.h> 7#include <linux/kernel.h> 8 9struct device; 10 11/** 12 * Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are 13 * preferable to the old integer-based handles. 14 * 15 * Contrary to integers, a pointer to a gpio_desc is guaranteed to be valid 16 * until the GPIO is released. 17 */ 18struct gpio_desc; 19 20/** 21 * Struct containing an array of descriptors that can be obtained using 22 * gpiod_get_array(). 23 */ 24struct gpio_descs { 25 unsigned int ndescs; 26 struct gpio_desc *desc[]; 27}; 28 29#define GPIOD_FLAGS_BIT_DIR_SET BIT(0) 30#define GPIOD_FLAGS_BIT_DIR_OUT BIT(1) 31#define GPIOD_FLAGS_BIT_DIR_VAL BIT(2) 32#define GPIOD_FLAGS_BIT_OPEN_DRAIN BIT(3) 33 34/** 35 * Optional flags that can be passed to one of gpiod_* to configure direction 36 * and output value. These values cannot be OR'd. 37 */ 38enum gpiod_flags { 39 GPIOD_ASIS = 0, 40 GPIOD_IN = GPIOD_FLAGS_BIT_DIR_SET, 41 GPIOD_OUT_LOW = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT, 42 GPIOD_OUT_HIGH = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT | 43 GPIOD_FLAGS_BIT_DIR_VAL, 44 GPIOD_OUT_LOW_OPEN_DRAIN = GPIOD_FLAGS_BIT_DIR_SET | 45 GPIOD_FLAGS_BIT_DIR_OUT | GPIOD_FLAGS_BIT_OPEN_DRAIN, 46 GPIOD_OUT_HIGH_OPEN_DRAIN = GPIOD_FLAGS_BIT_DIR_SET | 47 GPIOD_FLAGS_BIT_DIR_OUT | GPIOD_FLAGS_BIT_DIR_VAL | 48 GPIOD_FLAGS_BIT_OPEN_DRAIN, 49}; 50 51#ifdef CONFIG_GPIOLIB 52 53/* Return the number of GPIOs associated with a device / function */ 54int gpiod_count(struct device *dev, const char *con_id); 55 56/* Acquire and dispose GPIOs */ 57struct gpio_desc *__must_check gpiod_get(struct device *dev, 58 const char *con_id, 59 enum gpiod_flags flags); 60struct gpio_desc *__must_check gpiod_get_index(struct device *dev, 61 const char *con_id, 62 unsigned int idx, 63 enum gpiod_flags flags); 64struct gpio_desc *__must_check gpiod_get_optional(struct device *dev, 65 const char *con_id, 66 enum gpiod_flags flags); 67struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev, 68 const char *con_id, 69 unsigned int index, 70 enum gpiod_flags flags); 71struct gpio_descs *__must_check gpiod_get_array(struct device *dev, 72 const char *con_id, 73 enum gpiod_flags flags); 74struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev, 75 const char *con_id, 76 enum gpiod_flags flags); 77void gpiod_put(struct gpio_desc *desc); 78void gpiod_put_array(struct gpio_descs *descs); 79 80struct gpio_desc *__must_check devm_gpiod_get(struct device *dev, 81 const char *con_id, 82 enum gpiod_flags flags); 83struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, 84 const char *con_id, 85 unsigned int idx, 86 enum gpiod_flags flags); 87struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev, 88 const char *con_id, 89 enum gpiod_flags flags); 90struct gpio_desc *__must_check 91devm_gpiod_get_index_optional(struct device *dev, const char *con_id, 92 unsigned int index, enum gpiod_flags flags); 93struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev, 94 const char *con_id, 95 enum gpiod_flags flags); 96struct gpio_descs *__must_check 97devm_gpiod_get_array_optional(struct device *dev, const char *con_id, 98 enum gpiod_flags flags); 99void devm_gpiod_put(struct device *dev, struct gpio_desc *desc); 100void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs); 101 102int gpiod_get_direction(struct gpio_desc *desc); 103int gpiod_direction_input(struct gpio_desc *desc); 104int gpiod_direction_output(struct gpio_desc *desc, int value); 105int gpiod_direction_output_raw(struct gpio_desc *desc, int value); 106 107/* Value get/set from non-sleeping context */ 108int gpiod_get_value(const struct gpio_desc *desc); 109int gpiod_get_array_value(unsigned int array_size, 110 struct gpio_desc **desc_array, int *value_array); 111void gpiod_set_value(struct gpio_desc *desc, int value); 112void gpiod_set_array_value(unsigned int array_size, 113 struct gpio_desc **desc_array, int *value_array); 114int gpiod_get_raw_value(const struct gpio_desc *desc); 115int gpiod_get_raw_array_value(unsigned int array_size, 116 struct gpio_desc **desc_array, 117 int *value_array); 118void gpiod_set_raw_value(struct gpio_desc *desc, int value); 119void gpiod_set_raw_array_value(unsigned int array_size, 120 struct gpio_desc **desc_array, 121 int *value_array); 122 123/* Value get/set from sleeping context */ 124int gpiod_get_value_cansleep(const struct gpio_desc *desc); 125int gpiod_get_array_value_cansleep(unsigned int array_size, 126 struct gpio_desc **desc_array, 127 int *value_array); 128void gpiod_set_value_cansleep(struct gpio_desc *desc, int value); 129void gpiod_set_array_value_cansleep(unsigned int array_size, 130 struct gpio_desc **desc_array, 131 int *value_array); 132int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc); 133int gpiod_get_raw_array_value_cansleep(unsigned int array_size, 134 struct gpio_desc **desc_array, 135 int *value_array); 136void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value); 137void gpiod_set_raw_array_value_cansleep(unsigned int array_size, 138 struct gpio_desc **desc_array, 139 int *value_array); 140 141int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce); 142int gpiod_set_transitory(struct gpio_desc *desc, bool transitory); 143 144int gpiod_is_active_low(const struct gpio_desc *desc); 145int gpiod_cansleep(const struct gpio_desc *desc); 146 147int gpiod_to_irq(const struct gpio_desc *desc); 148 149/* Convert between the old gpio_ and new gpiod_ interfaces */ 150struct gpio_desc *gpio_to_desc(unsigned gpio); 151int desc_to_gpio(const struct gpio_desc *desc); 152 153/* Child properties interface */ 154struct device_node; 155struct fwnode_handle; 156 157struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev, 158 struct device_node *node, 159 const char *propname, int index, 160 enum gpiod_flags dflags, 161 const char *label); 162struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode, 163 const char *propname, int index, 164 enum gpiod_flags dflags, 165 const char *label); 166struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev, 167 const char *con_id, int index, 168 struct fwnode_handle *child, 169 enum gpiod_flags flags, 170 const char *label); 171 172#else /* CONFIG_GPIOLIB */ 173 174static inline int gpiod_count(struct device *dev, const char *con_id) 175{ 176 return 0; 177} 178 179static inline struct gpio_desc *__must_check gpiod_get(struct device *dev, 180 const char *con_id, 181 enum gpiod_flags flags) 182{ 183 return ERR_PTR(-ENOSYS); 184} 185static inline struct gpio_desc *__must_check 186gpiod_get_index(struct device *dev, 187 const char *con_id, 188 unsigned int idx, 189 enum gpiod_flags flags) 190{ 191 return ERR_PTR(-ENOSYS); 192} 193 194static inline struct gpio_desc *__must_check 195gpiod_get_optional(struct device *dev, const char *con_id, 196 enum gpiod_flags flags) 197{ 198 return NULL; 199} 200 201static inline struct gpio_desc *__must_check 202gpiod_get_index_optional(struct device *dev, const char *con_id, 203 unsigned int index, enum gpiod_flags flags) 204{ 205 return NULL; 206} 207 208static inline struct gpio_descs *__must_check 209gpiod_get_array(struct device *dev, const char *con_id, 210 enum gpiod_flags flags) 211{ 212 return ERR_PTR(-ENOSYS); 213} 214 215static inline struct gpio_descs *__must_check 216gpiod_get_array_optional(struct device *dev, const char *con_id, 217 enum gpiod_flags flags) 218{ 219 return NULL; 220} 221 222static inline void gpiod_put(struct gpio_desc *desc) 223{ 224 might_sleep(); 225 226 /* GPIO can never have been requested */ 227 WARN_ON(1); 228} 229 230static inline void gpiod_put_array(struct gpio_descs *descs) 231{ 232 might_sleep(); 233 234 /* GPIO can never have been requested */ 235 WARN_ON(1); 236} 237 238static inline struct gpio_desc *__must_check 239devm_gpiod_get(struct device *dev, 240 const char *con_id, 241 enum gpiod_flags flags) 242{ 243 return ERR_PTR(-ENOSYS); 244} 245static inline 246struct gpio_desc *__must_check 247devm_gpiod_get_index(struct device *dev, 248 const char *con_id, 249 unsigned int idx, 250 enum gpiod_flags flags) 251{ 252 return ERR_PTR(-ENOSYS); 253} 254 255static inline struct gpio_desc *__must_check 256devm_gpiod_get_optional(struct device *dev, const char *con_id, 257 enum gpiod_flags flags) 258{ 259 return NULL; 260} 261 262static inline struct gpio_desc *__must_check 263devm_gpiod_get_index_optional(struct device *dev, const char *con_id, 264 unsigned int index, enum gpiod_flags flags) 265{ 266 return NULL; 267} 268 269static inline struct gpio_descs *__must_check 270devm_gpiod_get_array(struct device *dev, const char *con_id, 271 enum gpiod_flags flags) 272{ 273 return ERR_PTR(-ENOSYS); 274} 275 276static inline struct gpio_descs *__must_check 277devm_gpiod_get_array_optional(struct device *dev, const char *con_id, 278 enum gpiod_flags flags) 279{ 280 return NULL; 281} 282 283static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc) 284{ 285 might_sleep(); 286 287 /* GPIO can never have been requested */ 288 WARN_ON(1); 289} 290 291static inline void devm_gpiod_put_array(struct device *dev, 292 struct gpio_descs *descs) 293{ 294 might_sleep(); 295 296 /* GPIO can never have been requested */ 297 WARN_ON(1); 298} 299 300 301static inline int gpiod_get_direction(const struct gpio_desc *desc) 302{ 303 /* GPIO can never have been requested */ 304 WARN_ON(1); 305 return -ENOSYS; 306} 307static inline int gpiod_direction_input(struct gpio_desc *desc) 308{ 309 /* GPIO can never have been requested */ 310 WARN_ON(1); 311 return -ENOSYS; 312} 313static inline int gpiod_direction_output(struct gpio_desc *desc, int value) 314{ 315 /* GPIO can never have been requested */ 316 WARN_ON(1); 317 return -ENOSYS; 318} 319static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value) 320{ 321 /* GPIO can never have been requested */ 322 WARN_ON(1); 323 return -ENOSYS; 324} 325 326 327static inline int gpiod_get_value(const struct gpio_desc *desc) 328{ 329 /* GPIO can never have been requested */ 330 WARN_ON(1); 331 return 0; 332} 333static inline int gpiod_get_array_value(unsigned int array_size, 334 struct gpio_desc **desc_array, 335 int *value_array) 336{ 337 /* GPIO can never have been requested */ 338 WARN_ON(1); 339 return 0; 340} 341static inline void gpiod_set_value(struct gpio_desc *desc, int value) 342{ 343 /* GPIO can never have been requested */ 344 WARN_ON(1); 345} 346static inline void gpiod_set_array_value(unsigned int array_size, 347 struct gpio_desc **desc_array, 348 int *value_array) 349{ 350 /* GPIO can never have been requested */ 351 WARN_ON(1); 352} 353static inline int gpiod_get_raw_value(const struct gpio_desc *desc) 354{ 355 /* GPIO can never have been requested */ 356 WARN_ON(1); 357 return 0; 358} 359static inline int gpiod_get_raw_array_value(unsigned int array_size, 360 struct gpio_desc **desc_array, 361 int *value_array) 362{ 363 /* GPIO can never have been requested */ 364 WARN_ON(1); 365 return 0; 366} 367static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value) 368{ 369 /* GPIO can never have been requested */ 370 WARN_ON(1); 371} 372static inline void gpiod_set_raw_array_value(unsigned int array_size, 373 struct gpio_desc **desc_array, 374 int *value_array) 375{ 376 /* GPIO can never have been requested */ 377 WARN_ON(1); 378} 379 380static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc) 381{ 382 /* GPIO can never have been requested */ 383 WARN_ON(1); 384 return 0; 385} 386static inline int gpiod_get_array_value_cansleep(unsigned int array_size, 387 struct gpio_desc **desc_array, 388 int *value_array) 389{ 390 /* GPIO can never have been requested */ 391 WARN_ON(1); 392 return 0; 393} 394static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) 395{ 396 /* GPIO can never have been requested */ 397 WARN_ON(1); 398} 399static inline void gpiod_set_array_value_cansleep(unsigned int array_size, 400 struct gpio_desc **desc_array, 401 int *value_array) 402{ 403 /* GPIO can never have been requested */ 404 WARN_ON(1); 405} 406static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) 407{ 408 /* GPIO can never have been requested */ 409 WARN_ON(1); 410 return 0; 411} 412static inline int gpiod_get_raw_array_value_cansleep(unsigned int array_size, 413 struct gpio_desc **desc_array, 414 int *value_array) 415{ 416 /* GPIO can never have been requested */ 417 WARN_ON(1); 418 return 0; 419} 420static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, 421 int value) 422{ 423 /* GPIO can never have been requested */ 424 WARN_ON(1); 425} 426static inline void gpiod_set_raw_array_value_cansleep(unsigned int array_size, 427 struct gpio_desc **desc_array, 428 int *value_array) 429{ 430 /* GPIO can never have been requested */ 431 WARN_ON(1); 432} 433 434static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) 435{ 436 /* GPIO can never have been requested */ 437 WARN_ON(1); 438 return -ENOSYS; 439} 440 441static inline int gpiod_set_transitory(struct gpio_desc *desc, bool transitory) 442{ 443 /* GPIO can never have been requested */ 444 WARN_ON(1); 445 return -ENOSYS; 446} 447 448static inline int gpiod_is_active_low(const struct gpio_desc *desc) 449{ 450 /* GPIO can never have been requested */ 451 WARN_ON(1); 452 return 0; 453} 454static inline int gpiod_cansleep(const struct gpio_desc *desc) 455{ 456 /* GPIO can never have been requested */ 457 WARN_ON(1); 458 return 0; 459} 460 461static inline int gpiod_to_irq(const struct gpio_desc *desc) 462{ 463 /* GPIO can never have been requested */ 464 WARN_ON(1); 465 return -EINVAL; 466} 467 468static inline struct gpio_desc *gpio_to_desc(unsigned gpio) 469{ 470 return ERR_PTR(-EINVAL); 471} 472 473static inline int desc_to_gpio(const struct gpio_desc *desc) 474{ 475 /* GPIO can never have been requested */ 476 WARN_ON(1); 477 return -EINVAL; 478} 479 480/* Child properties interface */ 481struct device_node; 482struct fwnode_handle; 483 484static inline 485struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev, 486 struct device_node *node, 487 const char *propname, int index, 488 enum gpiod_flags dflags, 489 const char *label) 490{ 491 return ERR_PTR(-ENOSYS); 492} 493 494static inline 495struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode, 496 const char *propname, int index, 497 enum gpiod_flags dflags, 498 const char *label) 499{ 500 return ERR_PTR(-ENOSYS); 501} 502 503static inline 504struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev, 505 const char *con_id, int index, 506 struct fwnode_handle *child, 507 enum gpiod_flags flags, 508 const char *label) 509{ 510 return ERR_PTR(-ENOSYS); 511} 512 513#endif /* CONFIG_GPIOLIB */ 514 515static inline 516struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev, 517 const char *con_id, 518 struct fwnode_handle *child, 519 enum gpiod_flags flags, 520 const char *label) 521{ 522 return devm_fwnode_get_index_gpiod_from_child(dev, con_id, 0, child, 523 flags, label); 524} 525 526#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS) 527 528int gpiod_export(struct gpio_desc *desc, bool direction_may_change); 529int gpiod_export_link(struct device *dev, const char *name, 530 struct gpio_desc *desc); 531void gpiod_unexport(struct gpio_desc *desc); 532 533#else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ 534 535static inline int gpiod_export(struct gpio_desc *desc, 536 bool direction_may_change) 537{ 538 return -ENOSYS; 539} 540 541static inline int gpiod_export_link(struct device *dev, const char *name, 542 struct gpio_desc *desc) 543{ 544 return -ENOSYS; 545} 546 547static inline void gpiod_unexport(struct gpio_desc *desc) 548{ 549} 550 551#endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ 552 553#endif