1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __LINUX_GPIO_CONSUMER_H 3 #define __LINUX_GPIO_CONSUMER_H 4 5 #include <linux/bits.h> 6 #include <linux/err.h> 7 #include <linux/types.h> 8 9 struct acpi_device; 10 struct device; 11 struct fwnode_handle; 12 13 struct gpio_array; 14 struct gpio_desc; 15 16 /** 17 * struct gpio_descs - Struct containing an array of descriptors that can be 18 * obtained using gpiod_get_array() 19 * 20 * @info: Pointer to the opaque gpio_array structure 21 * @ndescs: Number of held descriptors 22 * @desc: Array of pointers to GPIO descriptors 23 */ 24 struct gpio_descs { 25 struct gpio_array *info; 26 unsigned int ndescs; 27 struct gpio_desc *desc[]; 28 }; 29 30 #define GPIOD_FLAGS_BIT_DIR_SET BIT(0) 31 #define GPIOD_FLAGS_BIT_DIR_OUT BIT(1) 32 #define GPIOD_FLAGS_BIT_DIR_VAL BIT(2) 33 #define GPIOD_FLAGS_BIT_OPEN_DRAIN BIT(3) 34 #define GPIOD_FLAGS_BIT_NONEXCLUSIVE BIT(4) 35 36 /** 37 * enum gpiod_flags - Optional flags that can be passed to one of gpiod_* to 38 * configure direction and output value. These values 39 * cannot be OR'd. 40 * 41 * @GPIOD_ASIS: Don't change anything 42 * @GPIOD_IN: Set lines to input mode 43 * @GPIOD_OUT_LOW: Set lines to output and drive them low 44 * @GPIOD_OUT_HIGH: Set lines to output and drive them high 45 * @GPIOD_OUT_LOW_OPEN_DRAIN: Set lines to open-drain output and drive them low 46 * @GPIOD_OUT_HIGH_OPEN_DRAIN: Set lines to open-drain output and drive them high 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 int 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 int 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 int 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 int 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_config(struct gpio_desc *desc, unsigned long config); 160 int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce); 161 void gpiod_toggle_active_low(struct gpio_desc *desc); 162 163 int gpiod_is_active_low(const struct gpio_desc *desc); 164 int gpiod_cansleep(const struct gpio_desc *desc); 165 166 int gpiod_to_irq(const struct gpio_desc *desc); 167 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name); 168 169 /* Convert between the old gpio_ and new gpiod_ interfaces */ 170 struct gpio_desc *gpio_to_desc(unsigned gpio); 171 int desc_to_gpio(const struct gpio_desc *desc); 172 173 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode, 174 const char *con_id, int index, 175 enum gpiod_flags flags, 176 const char *label); 177 struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev, 178 struct fwnode_handle *child, 179 const char *con_id, int index, 180 enum gpiod_flags flags, 181 const char *label); 182 183 #else /* CONFIG_GPIOLIB */ 184 185 #include <linux/bug.h> 186 #include <linux/kernel.h> 187 188 static inline int gpiod_count(struct device *dev, const char *con_id) 189 { 190 return 0; 191 } 192 193 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev, 194 const char *con_id, 195 enum gpiod_flags flags) 196 { 197 return ERR_PTR(-ENOSYS); 198 } 199 static inline struct gpio_desc *__must_check 200 gpiod_get_index(struct device *dev, 201 const char *con_id, 202 unsigned int idx, 203 enum gpiod_flags flags) 204 { 205 return ERR_PTR(-ENOSYS); 206 } 207 208 static inline struct gpio_desc *__must_check 209 gpiod_get_optional(struct device *dev, const char *con_id, 210 enum gpiod_flags flags) 211 { 212 return NULL; 213 } 214 215 static inline struct gpio_desc *__must_check 216 gpiod_get_index_optional(struct device *dev, const char *con_id, 217 unsigned int index, enum gpiod_flags flags) 218 { 219 return NULL; 220 } 221 222 static inline struct gpio_descs *__must_check 223 gpiod_get_array(struct device *dev, const char *con_id, 224 enum gpiod_flags flags) 225 { 226 return ERR_PTR(-ENOSYS); 227 } 228 229 static inline struct gpio_descs *__must_check 230 gpiod_get_array_optional(struct device *dev, const char *con_id, 231 enum gpiod_flags flags) 232 { 233 return NULL; 234 } 235 236 static inline void gpiod_put(struct gpio_desc *desc) 237 { 238 might_sleep(); 239 240 /* GPIO can never have been requested */ 241 WARN_ON(desc); 242 } 243 244 static inline void devm_gpiod_unhinge(struct device *dev, 245 struct gpio_desc *desc) 246 { 247 might_sleep(); 248 249 /* GPIO can never have been requested */ 250 WARN_ON(desc); 251 } 252 253 static inline void gpiod_put_array(struct gpio_descs *descs) 254 { 255 might_sleep(); 256 257 /* GPIO can never have been requested */ 258 WARN_ON(descs); 259 } 260 261 static inline struct gpio_desc *__must_check 262 devm_gpiod_get(struct device *dev, 263 const char *con_id, 264 enum gpiod_flags flags) 265 { 266 return ERR_PTR(-ENOSYS); 267 } 268 static inline 269 struct gpio_desc *__must_check 270 devm_gpiod_get_index(struct device *dev, 271 const char *con_id, 272 unsigned int idx, 273 enum gpiod_flags flags) 274 { 275 return ERR_PTR(-ENOSYS); 276 } 277 278 static inline struct gpio_desc *__must_check 279 devm_gpiod_get_optional(struct device *dev, const char *con_id, 280 enum gpiod_flags flags) 281 { 282 return NULL; 283 } 284 285 static inline struct gpio_desc *__must_check 286 devm_gpiod_get_index_optional(struct device *dev, const char *con_id, 287 unsigned int index, enum gpiod_flags flags) 288 { 289 return NULL; 290 } 291 292 static inline struct gpio_descs *__must_check 293 devm_gpiod_get_array(struct device *dev, const char *con_id, 294 enum gpiod_flags flags) 295 { 296 return ERR_PTR(-ENOSYS); 297 } 298 299 static inline struct gpio_descs *__must_check 300 devm_gpiod_get_array_optional(struct device *dev, const char *con_id, 301 enum gpiod_flags flags) 302 { 303 return NULL; 304 } 305 306 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc) 307 { 308 might_sleep(); 309 310 /* GPIO can never have been requested */ 311 WARN_ON(desc); 312 } 313 314 static inline void devm_gpiod_put_array(struct device *dev, 315 struct gpio_descs *descs) 316 { 317 might_sleep(); 318 319 /* GPIO can never have been requested */ 320 WARN_ON(descs); 321 } 322 323 324 static inline int gpiod_get_direction(const struct gpio_desc *desc) 325 { 326 /* GPIO can never have been requested */ 327 WARN_ON(desc); 328 return -ENOSYS; 329 } 330 static inline int gpiod_direction_input(struct gpio_desc *desc) 331 { 332 /* GPIO can never have been requested */ 333 WARN_ON(desc); 334 return -ENOSYS; 335 } 336 static inline int gpiod_direction_output(struct gpio_desc *desc, int value) 337 { 338 /* GPIO can never have been requested */ 339 WARN_ON(desc); 340 return -ENOSYS; 341 } 342 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value) 343 { 344 /* GPIO can never have been requested */ 345 WARN_ON(desc); 346 return -ENOSYS; 347 } 348 static inline int gpiod_get_value(const struct gpio_desc *desc) 349 { 350 /* GPIO can never have been requested */ 351 WARN_ON(desc); 352 return 0; 353 } 354 static inline int gpiod_get_array_value(unsigned int array_size, 355 struct gpio_desc **desc_array, 356 struct gpio_array *array_info, 357 unsigned long *value_bitmap) 358 { 359 /* GPIO can never have been requested */ 360 WARN_ON(desc_array); 361 return 0; 362 } 363 static inline int gpiod_set_value(struct gpio_desc *desc, int value) 364 { 365 /* GPIO can never have been requested */ 366 WARN_ON(desc); 367 return 0; 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 int gpiod_set_raw_value(struct gpio_desc *desc, int value) 394 { 395 /* GPIO can never have been requested */ 396 WARN_ON(desc); 397 return 0; 398 } 399 static inline int gpiod_set_raw_array_value(unsigned int array_size, 400 struct gpio_desc **desc_array, 401 struct gpio_array *array_info, 402 unsigned long *value_bitmap) 403 { 404 /* GPIO can never have been requested */ 405 WARN_ON(desc_array); 406 return 0; 407 } 408 409 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc) 410 { 411 /* GPIO can never have been requested */ 412 WARN_ON(desc); 413 return 0; 414 } 415 static inline int gpiod_get_array_value_cansleep(unsigned int array_size, 416 struct gpio_desc **desc_array, 417 struct gpio_array *array_info, 418 unsigned long *value_bitmap) 419 { 420 /* GPIO can never have been requested */ 421 WARN_ON(desc_array); 422 return 0; 423 } 424 static inline int gpiod_set_value_cansleep(struct gpio_desc *desc, int value) 425 { 426 /* GPIO can never have been requested */ 427 WARN_ON(desc); 428 return 0; 429 } 430 static inline int gpiod_set_array_value_cansleep(unsigned int array_size, 431 struct gpio_desc **desc_array, 432 struct gpio_array *array_info, 433 unsigned long *value_bitmap) 434 { 435 /* GPIO can never have been requested */ 436 WARN_ON(desc_array); 437 return 0; 438 } 439 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) 440 { 441 /* GPIO can never have been requested */ 442 WARN_ON(desc); 443 return 0; 444 } 445 static inline int gpiod_get_raw_array_value_cansleep(unsigned int array_size, 446 struct gpio_desc **desc_array, 447 struct gpio_array *array_info, 448 unsigned long *value_bitmap) 449 { 450 /* GPIO can never have been requested */ 451 WARN_ON(desc_array); 452 return 0; 453 } 454 static inline int gpiod_set_raw_value_cansleep(struct gpio_desc *desc, 455 int value) 456 { 457 /* GPIO can never have been requested */ 458 WARN_ON(desc); 459 return 0; 460 } 461 static inline int gpiod_set_raw_array_value_cansleep(unsigned int array_size, 462 struct gpio_desc **desc_array, 463 struct gpio_array *array_info, 464 unsigned long *value_bitmap) 465 { 466 /* GPIO can never have been requested */ 467 WARN_ON(desc_array); 468 return 0; 469 } 470 471 static inline int gpiod_set_config(struct gpio_desc *desc, unsigned long config) 472 { 473 /* GPIO can never have been requested */ 474 WARN_ON(desc); 475 return -ENOSYS; 476 } 477 478 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce) 479 { 480 /* GPIO can never have been requested */ 481 WARN_ON(desc); 482 return -ENOSYS; 483 } 484 485 static inline void gpiod_toggle_active_low(struct gpio_desc *desc) 486 { 487 /* GPIO can never have been requested */ 488 WARN_ON(desc); 489 } 490 491 static inline int gpiod_is_active_low(const struct gpio_desc *desc) 492 { 493 /* GPIO can never have been requested */ 494 WARN_ON(desc); 495 return 0; 496 } 497 static inline int gpiod_cansleep(const struct gpio_desc *desc) 498 { 499 /* GPIO can never have been requested */ 500 WARN_ON(desc); 501 return 0; 502 } 503 504 static inline int gpiod_to_irq(const struct gpio_desc *desc) 505 { 506 /* GPIO can never have been requested */ 507 WARN_ON(desc); 508 return -EINVAL; 509 } 510 511 static inline int gpiod_set_consumer_name(struct gpio_desc *desc, 512 const char *name) 513 { 514 /* GPIO can never have been requested */ 515 WARN_ON(desc); 516 return -EINVAL; 517 } 518 519 static inline struct gpio_desc *gpio_to_desc(unsigned gpio) 520 { 521 return NULL; 522 } 523 524 static inline int desc_to_gpio(const struct gpio_desc *desc) 525 { 526 /* GPIO can never have been requested */ 527 WARN_ON(desc); 528 return -EINVAL; 529 } 530 531 static inline 532 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode, 533 const char *con_id, int index, 534 enum gpiod_flags flags, 535 const char *label) 536 { 537 return ERR_PTR(-ENOSYS); 538 } 539 540 static inline 541 struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev, 542 struct fwnode_handle *fwnode, 543 const char *con_id, int index, 544 enum gpiod_flags flags, 545 const char *label) 546 { 547 return ERR_PTR(-ENOSYS); 548 } 549 550 #endif /* CONFIG_GPIOLIB */ 551 552 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_HTE) 553 int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags); 554 int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags); 555 #else 556 557 #include <linux/bug.h> 558 559 static inline int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, 560 unsigned long flags) 561 { 562 if (!IS_ENABLED(CONFIG_GPIOLIB)) 563 WARN_ON(desc); 564 565 return -ENOSYS; 566 } 567 static inline int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, 568 unsigned long flags) 569 { 570 if (!IS_ENABLED(CONFIG_GPIOLIB)) 571 WARN_ON(desc); 572 573 return -ENOSYS; 574 } 575 #endif /* CONFIG_GPIOLIB && CONFIG_HTE */ 576 577 static inline 578 struct gpio_desc *devm_fwnode_gpiod_get(struct device *dev, 579 struct fwnode_handle *fwnode, 580 const char *con_id, 581 enum gpiod_flags flags, 582 const char *label) 583 { 584 return devm_fwnode_gpiod_get_index(dev, fwnode, con_id, 0, 585 flags, label); 586 } 587 588 struct acpi_gpio_params { 589 unsigned int crs_entry_index; 590 unsigned int line_index; 591 bool active_low; 592 }; 593 594 struct acpi_gpio_mapping { 595 const char *name; 596 const struct acpi_gpio_params *data; 597 unsigned int size; 598 599 /* Ignore IoRestriction field */ 600 #define ACPI_GPIO_QUIRK_NO_IO_RESTRICTION BIT(0) 601 /* 602 * When ACPI GPIO mapping table is in use the index parameter inside it 603 * refers to the GPIO resource in _CRS method. That index has no 604 * distinction of actual type of the resource. When consumer wants to 605 * get GpioIo type explicitly, this quirk may be used. 606 */ 607 #define ACPI_GPIO_QUIRK_ONLY_GPIOIO BIT(1) 608 /* Use given pin as an absolute GPIO number in the system */ 609 #define ACPI_GPIO_QUIRK_ABSOLUTE_NUMBER BIT(2) 610 611 unsigned int quirks; 612 }; 613 614 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_ACPI) 615 616 int acpi_dev_add_driver_gpios(struct acpi_device *adev, 617 const struct acpi_gpio_mapping *gpios); 618 void acpi_dev_remove_driver_gpios(struct acpi_device *adev); 619 620 int devm_acpi_dev_add_driver_gpios(struct device *dev, 621 const struct acpi_gpio_mapping *gpios); 622 623 #else /* CONFIG_GPIOLIB && CONFIG_ACPI */ 624 625 static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev, 626 const struct acpi_gpio_mapping *gpios) 627 { 628 return -ENXIO; 629 } 630 static inline void acpi_dev_remove_driver_gpios(struct acpi_device *adev) {} 631 632 static inline int devm_acpi_dev_add_driver_gpios(struct device *dev, 633 const struct acpi_gpio_mapping *gpios) 634 { 635 return -ENXIO; 636 } 637 638 #endif /* CONFIG_GPIOLIB && CONFIG_ACPI */ 639 640 641 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS) 642 643 int gpiod_export(struct gpio_desc *desc, bool direction_may_change); 644 int gpiod_export_link(struct device *dev, const char *name, 645 struct gpio_desc *desc); 646 void gpiod_unexport(struct gpio_desc *desc); 647 648 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ 649 650 static inline int gpiod_export(struct gpio_desc *desc, 651 bool direction_may_change) 652 { 653 return -ENOSYS; 654 } 655 656 static inline int gpiod_export_link(struct device *dev, const char *name, 657 struct gpio_desc *desc) 658 { 659 return -ENOSYS; 660 } 661 662 static inline void gpiod_unexport(struct gpio_desc *desc) 663 { 664 } 665 666 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ 667 668 static inline int gpiod_multi_set_value_cansleep(struct gpio_descs *descs, 669 unsigned long *value_bitmap) 670 { 671 if (IS_ERR_OR_NULL(descs)) 672 return PTR_ERR_OR_ZERO(descs); 673 674 return gpiod_set_array_value_cansleep(descs->ndescs, descs->desc, 675 descs->info, value_bitmap); 676 } 677 678 #endif 679