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 9 struct 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 */ 18 struct gpio_desc; 19 20 /** 21 * Opaque descriptor for a structure of GPIO array attributes. This structure 22 * is attached to struct gpiod_descs obtained from gpiod_get_array() and can be 23 * passed back to get/set array functions in order to activate fast processing 24 * path if applicable. 25 */ 26 struct gpio_array; 27 28 /** 29 * Struct containing an array of descriptors that can be obtained using 30 * gpiod_get_array(). 31 */ 32 struct gpio_descs { 33 struct gpio_array *info; 34 unsigned int ndescs; 35 struct gpio_desc *desc[]; 36 }; 37 38 #define GPIOD_FLAGS_BIT_DIR_SET BIT(0) 39 #define GPIOD_FLAGS_BIT_DIR_OUT BIT(1) 40 #define GPIOD_FLAGS_BIT_DIR_VAL BIT(2) 41 #define GPIOD_FLAGS_BIT_OPEN_DRAIN BIT(3) 42 #define GPIOD_FLAGS_BIT_NONEXCLUSIVE BIT(4) 43 44 /** 45 * Optional flags that can be passed to one of gpiod_* to configure direction 46 * and output value. These values cannot be OR'd. 47 */ 48 enum gpiod_flags { 49 GPIOD_ASIS = 0, 50 GPIOD_IN = GPIOD_FLAGS_BIT_DIR_SET, 51 GPIOD_OUT_LOW = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT, 52 GPIOD_OUT_HIGH = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT | 53 GPIOD_FLAGS_BIT_DIR_VAL, 54 GPIOD_OUT_LOW_OPEN_DRAIN = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_OPEN_DRAIN, 55 GPIOD_OUT_HIGH_OPEN_DRAIN = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_OPEN_DRAIN, 56 }; 57 58 #ifdef CONFIG_GPIOLIB 59 60 /* Return the number of GPIOs associated with a device / function */ 61 int gpiod_count(struct device *dev, const char *con_id); 62 63 /* Acquire and dispose GPIOs */ 64 struct gpio_desc *__must_check gpiod_get(struct device *dev, 65 const char *con_id, 66 enum gpiod_flags flags); 67 struct gpio_desc *__must_check gpiod_get_index(struct device *dev, 68 const char *con_id, 69 unsigned int idx, 70 enum gpiod_flags flags); 71 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev, 72 const char *con_id, 73 enum gpiod_flags flags); 74 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev, 75 const char *con_id, 76 unsigned int index, 77 enum gpiod_flags flags); 78 struct gpio_descs *__must_check gpiod_get_array(struct device *dev, 79 const char *con_id, 80 enum gpiod_flags flags); 81 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev, 82 const char *con_id, 83 enum gpiod_flags flags); 84 void gpiod_put(struct gpio_desc *desc); 85 void gpiod_put_array(struct gpio_descs *descs); 86 87 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev, 88 const char *con_id, 89 enum gpiod_flags flags); 90 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, 91 const char *con_id, 92 unsigned int idx, 93 enum gpiod_flags flags); 94 struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev, 95 const char *con_id, 96 enum gpiod_flags flags); 97 struct gpio_desc *__must_check 98 devm_gpiod_get_index_optional(struct device *dev, const char *con_id, 99 unsigned int index, enum gpiod_flags flags); 100 struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev, 101 const char *con_id, 102 enum gpiod_flags flags); 103 struct gpio_descs *__must_check 104 devm_gpiod_get_array_optional(struct device *dev, const char *con_id, 105 enum gpiod_flags flags); 106 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc); 107 void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc); 108 void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs); 109 110 int gpiod_get_direction(struct gpio_desc *desc); 111 int gpiod_direction_input(struct gpio_desc *desc); 112 int gpiod_direction_output(struct gpio_desc *desc, int value); 113 int gpiod_direction_output_raw(struct gpio_desc *desc, int value); 114 115 /* Value get/set from non-sleeping context */ 116 int gpiod_get_value(const struct gpio_desc *desc); 117 int gpiod_get_array_value(unsigned int array_size, 118 struct gpio_desc **desc_array, 119 struct gpio_array *array_info, 120 unsigned long *value_bitmap); 121 void gpiod_set_value(struct gpio_desc *desc, int value); 122 int gpiod_set_array_value(unsigned int array_size, 123 struct gpio_desc **desc_array, 124 struct gpio_array *array_info, 125 unsigned long *value_bitmap); 126 int gpiod_get_raw_value(const struct gpio_desc *desc); 127 int gpiod_get_raw_array_value(unsigned int array_size, 128 struct gpio_desc **desc_array, 129 struct gpio_array *array_info, 130 unsigned long *value_bitmap); 131 void gpiod_set_raw_value(struct gpio_desc *desc, int value); 132 int gpiod_set_raw_array_value(unsigned int array_size, 133 struct gpio_desc **desc_array, 134 struct gpio_array *array_info, 135 unsigned long *value_bitmap); 136 137 /* Value get/set from sleeping context */ 138 int gpiod_get_value_cansleep(const struct gpio_desc *desc); 139 int gpiod_get_array_value_cansleep(unsigned int array_size, 140 struct gpio_desc **desc_array, 141 struct gpio_array *array_info, 142 unsigned long *value_bitmap); 143 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value); 144 int gpiod_set_array_value_cansleep(unsigned int array_size, 145 struct gpio_desc **desc_array, 146 struct gpio_array *array_info, 147 unsigned long *value_bitmap); 148 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc); 149 int gpiod_get_raw_array_value_cansleep(unsigned int array_size, 150 struct gpio_desc **desc_array, 151 struct gpio_array *array_info, 152 unsigned long *value_bitmap); 153 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value); 154 int gpiod_set_raw_array_value_cansleep(unsigned int array_size, 155 struct gpio_desc **desc_array, 156 struct gpio_array *array_info, 157 unsigned long *value_bitmap); 158 159 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce); 160 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory); 161 162 int gpiod_is_active_low(const struct gpio_desc *desc); 163 int gpiod_cansleep(const struct gpio_desc *desc); 164 165 int gpiod_to_irq(const struct gpio_desc *desc); 166 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name); 167 168 /* Convert between the old gpio_ and new gpiod_ interfaces */ 169 struct gpio_desc *gpio_to_desc(unsigned gpio); 170 int desc_to_gpio(const struct gpio_desc *desc); 171 172 /* Child properties interface */ 173 struct fwnode_handle; 174 175 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode, 176 const char *propname, int index, 177 enum gpiod_flags dflags, 178 const char *label); 179 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev, 180 const char *con_id, int index, 181 struct fwnode_handle *child, 182 enum gpiod_flags flags, 183 const char *label); 184 185 #else /* CONFIG_GPIOLIB */ 186 187 static inline int gpiod_count(struct device *dev, const char *con_id) 188 { 189 return 0; 190 } 191 192 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev, 193 const char *con_id, 194 enum gpiod_flags flags) 195 { 196 return ERR_PTR(-ENOSYS); 197 } 198 static inline struct gpio_desc *__must_check 199 gpiod_get_index(struct device *dev, 200 const char *con_id, 201 unsigned int idx, 202 enum gpiod_flags flags) 203 { 204 return ERR_PTR(-ENOSYS); 205 } 206 207 static inline struct gpio_desc *__must_check 208 gpiod_get_optional(struct device *dev, const char *con_id, 209 enum gpiod_flags flags) 210 { 211 return NULL; 212 } 213 214 static inline struct gpio_desc *__must_check 215 gpiod_get_index_optional(struct device *dev, const char *con_id, 216 unsigned int index, enum gpiod_flags flags) 217 { 218 return NULL; 219 } 220 221 static inline struct gpio_descs *__must_check 222 gpiod_get_array(struct device *dev, const char *con_id, 223 enum gpiod_flags flags) 224 { 225 return ERR_PTR(-ENOSYS); 226 } 227 228 static inline struct gpio_descs *__must_check 229 gpiod_get_array_optional(struct device *dev, const char *con_id, 230 enum gpiod_flags flags) 231 { 232 return NULL; 233 } 234 235 static inline void gpiod_put(struct gpio_desc *desc) 236 { 237 might_sleep(); 238 239 /* GPIO can never have been requested */ 240 WARN_ON(desc); 241 } 242 243 static inline void devm_gpiod_unhinge(struct device *dev, 244 struct gpio_desc *desc) 245 { 246 might_sleep(); 247 248 /* GPIO can never have been requested */ 249 WARN_ON(desc); 250 } 251 252 static inline void gpiod_put_array(struct gpio_descs *descs) 253 { 254 might_sleep(); 255 256 /* GPIO can never have been requested */ 257 WARN_ON(descs); 258 } 259 260 static inline struct gpio_desc *__must_check 261 devm_gpiod_get(struct device *dev, 262 const char *con_id, 263 enum gpiod_flags flags) 264 { 265 return ERR_PTR(-ENOSYS); 266 } 267 static inline 268 struct gpio_desc *__must_check 269 devm_gpiod_get_index(struct device *dev, 270 const char *con_id, 271 unsigned int idx, 272 enum gpiod_flags flags) 273 { 274 return ERR_PTR(-ENOSYS); 275 } 276 277 static inline struct gpio_desc *__must_check 278 devm_gpiod_get_optional(struct device *dev, const char *con_id, 279 enum gpiod_flags flags) 280 { 281 return NULL; 282 } 283 284 static inline struct gpio_desc *__must_check 285 devm_gpiod_get_index_optional(struct device *dev, const char *con_id, 286 unsigned int index, enum gpiod_flags flags) 287 { 288 return NULL; 289 } 290 291 static inline struct gpio_descs *__must_check 292 devm_gpiod_get_array(struct device *dev, const char *con_id, 293 enum gpiod_flags flags) 294 { 295 return ERR_PTR(-ENOSYS); 296 } 297 298 static inline struct gpio_descs *__must_check 299 devm_gpiod_get_array_optional(struct device *dev, const char *con_id, 300 enum gpiod_flags flags) 301 { 302 return NULL; 303 } 304 305 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc) 306 { 307 might_sleep(); 308 309 /* GPIO can never have been requested */ 310 WARN_ON(desc); 311 } 312 313 static inline void devm_gpiod_put_array(struct device *dev, 314 struct gpio_descs *descs) 315 { 316 might_sleep(); 317 318 /* GPIO can never have been requested */ 319 WARN_ON(descs); 320 } 321 322 323 static inline int gpiod_get_direction(const struct gpio_desc *desc) 324 { 325 /* GPIO can never have been requested */ 326 WARN_ON(desc); 327 return -ENOSYS; 328 } 329 static inline int gpiod_direction_input(struct gpio_desc *desc) 330 { 331 /* GPIO can never have been requested */ 332 WARN_ON(desc); 333 return -ENOSYS; 334 } 335 static inline int gpiod_direction_output(struct gpio_desc *desc, int value) 336 { 337 /* GPIO can never have been requested */ 338 WARN_ON(desc); 339 return -ENOSYS; 340 } 341 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value) 342 { 343 /* GPIO can never have been requested */ 344 WARN_ON(desc); 345 return -ENOSYS; 346 } 347 348 349 static inline int gpiod_get_value(const struct gpio_desc *desc) 350 { 351 /* GPIO can never have been requested */ 352 WARN_ON(desc); 353 return 0; 354 } 355 static inline int gpiod_get_array_value(unsigned int array_size, 356 struct gpio_desc **desc_array, 357 struct gpio_array *array_info, 358 unsigned long *value_bitmap) 359 { 360 /* GPIO can never have been requested */ 361 WARN_ON(desc_array); 362 return 0; 363 } 364 static inline void gpiod_set_value(struct gpio_desc *desc, int value) 365 { 366 /* GPIO can never have been requested */ 367 WARN_ON(desc); 368 } 369 static inline int gpiod_set_array_value(unsigned int array_size, 370 struct gpio_desc **desc_array, 371 struct gpio_array *array_info, 372 unsigned long *value_bitmap) 373 { 374 /* GPIO can never have been requested */ 375 WARN_ON(desc_array); 376 return 0; 377 } 378 static inline int gpiod_get_raw_value(const struct gpio_desc *desc) 379 { 380 /* GPIO can never have been requested */ 381 WARN_ON(desc); 382 return 0; 383 } 384 static inline int gpiod_get_raw_array_value(unsigned int array_size, 385 struct gpio_desc **desc_array, 386 struct gpio_array *array_info, 387 unsigned long *value_bitmap) 388 { 389 /* GPIO can never have been requested */ 390 WARN_ON(desc_array); 391 return 0; 392 } 393 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value) 394 { 395 /* GPIO can never have been requested */ 396 WARN_ON(desc); 397 } 398 static inline int gpiod_set_raw_array_value(unsigned int array_size, 399 struct gpio_desc **desc_array, 400 struct gpio_array *array_info, 401 unsigned long *value_bitmap) 402 { 403 /* GPIO can never have been requested */ 404 WARN_ON(desc_array); 405 return 0; 406 } 407 408 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc) 409 { 410 /* GPIO can never have been requested */ 411 WARN_ON(desc); 412 return 0; 413 } 414 static inline int gpiod_get_array_value_cansleep(unsigned int array_size, 415 struct gpio_desc **desc_array, 416 struct gpio_array *array_info, 417 unsigned long *value_bitmap) 418 { 419 /* GPIO can never have been requested */ 420 WARN_ON(desc_array); 421 return 0; 422 } 423 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) 424 { 425 /* GPIO can never have been requested */ 426 WARN_ON(desc); 427 } 428 static inline int gpiod_set_array_value_cansleep(unsigned int array_size, 429 struct gpio_desc **desc_array, 430 struct gpio_array *array_info, 431 unsigned long *value_bitmap) 432 { 433 /* GPIO can never have been requested */ 434 WARN_ON(desc_array); 435 return 0; 436 } 437 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) 438 { 439 /* GPIO can never have been requested */ 440 WARN_ON(desc); 441 return 0; 442 } 443 static inline int gpiod_get_raw_array_value_cansleep(unsigned int array_size, 444 struct gpio_desc **desc_array, 445 struct gpio_array *array_info, 446 unsigned long *value_bitmap) 447 { 448 /* GPIO can never have been requested */ 449 WARN_ON(desc_array); 450 return 0; 451 } 452 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, 453 int value) 454 { 455 /* GPIO can never have been requested */ 456 WARN_ON(desc); 457 } 458 static inline int gpiod_set_raw_array_value_cansleep(unsigned int array_size, 459 struct gpio_desc **desc_array, 460 struct gpio_array *array_info, 461 unsigned long *value_bitmap) 462 { 463 /* GPIO can never have been requested */ 464 WARN_ON(desc_array); 465 return 0; 466 } 467 468 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) 469 { 470 /* GPIO can never have been requested */ 471 WARN_ON(desc); 472 return -ENOSYS; 473 } 474 475 static inline int gpiod_set_transitory(struct gpio_desc *desc, bool transitory) 476 { 477 /* GPIO can never have been requested */ 478 WARN_ON(desc); 479 return -ENOSYS; 480 } 481 482 static inline int gpiod_is_active_low(const struct gpio_desc *desc) 483 { 484 /* GPIO can never have been requested */ 485 WARN_ON(desc); 486 return 0; 487 } 488 static inline int gpiod_cansleep(const struct gpio_desc *desc) 489 { 490 /* GPIO can never have been requested */ 491 WARN_ON(desc); 492 return 0; 493 } 494 495 static inline int gpiod_to_irq(const struct gpio_desc *desc) 496 { 497 /* GPIO can never have been requested */ 498 WARN_ON(desc); 499 return -EINVAL; 500 } 501 502 static inline int gpiod_set_consumer_name(struct gpio_desc *desc, 503 const char *name) 504 { 505 /* GPIO can never have been requested */ 506 WARN_ON(desc); 507 return -EINVAL; 508 } 509 510 static inline struct gpio_desc *gpio_to_desc(unsigned gpio) 511 { 512 return NULL; 513 } 514 515 static inline int desc_to_gpio(const struct gpio_desc *desc) 516 { 517 /* GPIO can never have been requested */ 518 WARN_ON(desc); 519 return -EINVAL; 520 } 521 522 /* Child properties interface */ 523 struct fwnode_handle; 524 525 static inline 526 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode, 527 const char *propname, int index, 528 enum gpiod_flags dflags, 529 const char *label) 530 { 531 return ERR_PTR(-ENOSYS); 532 } 533 534 static inline 535 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev, 536 const char *con_id, int index, 537 struct fwnode_handle *child, 538 enum gpiod_flags flags, 539 const char *label) 540 { 541 return ERR_PTR(-ENOSYS); 542 } 543 544 #endif /* CONFIG_GPIOLIB */ 545 546 static inline 547 struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev, 548 const char *con_id, 549 struct fwnode_handle *child, 550 enum gpiod_flags flags, 551 const char *label) 552 { 553 return devm_fwnode_get_index_gpiod_from_child(dev, con_id, 0, child, 554 flags, label); 555 } 556 557 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_OF_GPIO) 558 struct device_node; 559 560 struct gpio_desc *gpiod_get_from_of_node(struct device_node *node, 561 const char *propname, int index, 562 enum gpiod_flags dflags, 563 const char *label); 564 565 #else /* CONFIG_GPIOLIB && CONFIG_OF_GPIO */ 566 567 struct device_node; 568 569 static inline 570 struct gpio_desc *gpiod_get_from_of_node(struct device_node *node, 571 const char *propname, int index, 572 enum gpiod_flags dflags, 573 const char *label) 574 { 575 return ERR_PTR(-ENOSYS); 576 } 577 578 #endif /* CONFIG_GPIOLIB && CONFIG_OF_GPIO */ 579 580 #ifdef CONFIG_GPIOLIB 581 struct device_node; 582 583 struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev, 584 struct device_node *node, 585 const char *propname, int index, 586 enum gpiod_flags dflags, 587 const char *label); 588 589 #else /* CONFIG_GPIOLIB */ 590 591 struct device_node; 592 593 static inline 594 struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev, 595 struct device_node *node, 596 const char *propname, int index, 597 enum gpiod_flags dflags, 598 const char *label) 599 { 600 return ERR_PTR(-ENOSYS); 601 } 602 603 #endif /* CONFIG_GPIOLIB */ 604 605 struct acpi_gpio_params { 606 unsigned int crs_entry_index; 607 unsigned int line_index; 608 bool active_low; 609 }; 610 611 struct acpi_gpio_mapping { 612 const char *name; 613 const struct acpi_gpio_params *data; 614 unsigned int size; 615 616 /* Ignore IoRestriction field */ 617 #define ACPI_GPIO_QUIRK_NO_IO_RESTRICTION BIT(0) 618 /* 619 * When ACPI GPIO mapping table is in use the index parameter inside it 620 * refers to the GPIO resource in _CRS method. That index has no 621 * distinction of actual type of the resource. When consumer wants to 622 * get GpioIo type explicitly, this quirk may be used. 623 */ 624 #define ACPI_GPIO_QUIRK_ONLY_GPIOIO BIT(1) 625 626 unsigned int quirks; 627 }; 628 629 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_ACPI) 630 631 struct acpi_device; 632 633 int acpi_dev_add_driver_gpios(struct acpi_device *adev, 634 const struct acpi_gpio_mapping *gpios); 635 void acpi_dev_remove_driver_gpios(struct acpi_device *adev); 636 637 int devm_acpi_dev_add_driver_gpios(struct device *dev, 638 const struct acpi_gpio_mapping *gpios); 639 void devm_acpi_dev_remove_driver_gpios(struct device *dev); 640 641 #else /* CONFIG_GPIOLIB && CONFIG_ACPI */ 642 643 struct acpi_device; 644 645 static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev, 646 const struct acpi_gpio_mapping *gpios) 647 { 648 return -ENXIO; 649 } 650 static inline void acpi_dev_remove_driver_gpios(struct acpi_device *adev) {} 651 652 static inline int devm_acpi_dev_add_driver_gpios(struct device *dev, 653 const struct acpi_gpio_mapping *gpios) 654 { 655 return -ENXIO; 656 } 657 static inline void devm_acpi_dev_remove_driver_gpios(struct device *dev) {} 658 659 #endif /* CONFIG_GPIOLIB && CONFIG_ACPI */ 660 661 662 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS) 663 664 int gpiod_export(struct gpio_desc *desc, bool direction_may_change); 665 int gpiod_export_link(struct device *dev, const char *name, 666 struct gpio_desc *desc); 667 void gpiod_unexport(struct gpio_desc *desc); 668 669 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ 670 671 static inline int gpiod_export(struct gpio_desc *desc, 672 bool direction_may_change) 673 { 674 return -ENOSYS; 675 } 676 677 static inline int gpiod_export_link(struct device *dev, const char *name, 678 struct gpio_desc *desc) 679 { 680 return -ENOSYS; 681 } 682 683 static inline void gpiod_unexport(struct gpio_desc *desc) 684 { 685 } 686 687 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ 688 689 #endif 690