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