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 int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags); 115 int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags); 116 117 /* Value get/set from non-sleeping context */ 118 int gpiod_get_value(const struct gpio_desc *desc); 119 int gpiod_get_array_value(unsigned int array_size, 120 struct gpio_desc **desc_array, 121 struct gpio_array *array_info, 122 unsigned long *value_bitmap); 123 void gpiod_set_value(struct gpio_desc *desc, int value); 124 int gpiod_set_array_value(unsigned int array_size, 125 struct gpio_desc **desc_array, 126 struct gpio_array *array_info, 127 unsigned long *value_bitmap); 128 int gpiod_get_raw_value(const struct gpio_desc *desc); 129 int gpiod_get_raw_array_value(unsigned int array_size, 130 struct gpio_desc **desc_array, 131 struct gpio_array *array_info, 132 unsigned long *value_bitmap); 133 void gpiod_set_raw_value(struct gpio_desc *desc, int value); 134 int gpiod_set_raw_array_value(unsigned int array_size, 135 struct gpio_desc **desc_array, 136 struct gpio_array *array_info, 137 unsigned long *value_bitmap); 138 139 /* Value get/set from sleeping context */ 140 int gpiod_get_value_cansleep(const struct gpio_desc *desc); 141 int gpiod_get_array_value_cansleep(unsigned int array_size, 142 struct gpio_desc **desc_array, 143 struct gpio_array *array_info, 144 unsigned long *value_bitmap); 145 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value); 146 int gpiod_set_array_value_cansleep(unsigned int array_size, 147 struct gpio_desc **desc_array, 148 struct gpio_array *array_info, 149 unsigned long *value_bitmap); 150 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc); 151 int gpiod_get_raw_array_value_cansleep(unsigned int array_size, 152 struct gpio_desc **desc_array, 153 struct gpio_array *array_info, 154 unsigned long *value_bitmap); 155 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value); 156 int gpiod_set_raw_array_value_cansleep(unsigned int array_size, 157 struct gpio_desc **desc_array, 158 struct gpio_array *array_info, 159 unsigned long *value_bitmap); 160 161 int gpiod_set_config(struct gpio_desc *desc, unsigned long config); 162 int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce); 163 void gpiod_toggle_active_low(struct gpio_desc *desc); 164 165 int gpiod_is_active_low(const struct gpio_desc *desc); 166 int gpiod_cansleep(const struct gpio_desc *desc); 167 168 int gpiod_to_irq(const struct gpio_desc *desc); 169 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name); 170 171 /* Convert between the old gpio_ and new gpiod_ interfaces */ 172 struct gpio_desc *gpio_to_desc(unsigned gpio); 173 int desc_to_gpio(const struct gpio_desc *desc); 174 175 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode, 176 const char *con_id, int index, 177 enum gpiod_flags flags, 178 const char *label); 179 struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev, 180 struct fwnode_handle *child, 181 const char *con_id, int index, 182 enum gpiod_flags flags, 183 const char *label); 184 185 #else /* CONFIG_GPIOLIB */ 186 187 #include <linux/err.h> 188 #include <linux/kernel.h> 189 190 #include <asm/bug.h> 191 192 static inline int gpiod_count(struct device *dev, const char *con_id) 193 { 194 return 0; 195 } 196 197 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev, 198 const char *con_id, 199 enum gpiod_flags flags) 200 { 201 return ERR_PTR(-ENOSYS); 202 } 203 static inline struct gpio_desc *__must_check 204 gpiod_get_index(struct device *dev, 205 const char *con_id, 206 unsigned int idx, 207 enum gpiod_flags flags) 208 { 209 return ERR_PTR(-ENOSYS); 210 } 211 212 static inline struct gpio_desc *__must_check 213 gpiod_get_optional(struct device *dev, const char *con_id, 214 enum gpiod_flags flags) 215 { 216 return NULL; 217 } 218 219 static inline struct gpio_desc *__must_check 220 gpiod_get_index_optional(struct device *dev, const char *con_id, 221 unsigned int index, enum gpiod_flags flags) 222 { 223 return NULL; 224 } 225 226 static inline struct gpio_descs *__must_check 227 gpiod_get_array(struct device *dev, const char *con_id, 228 enum gpiod_flags flags) 229 { 230 return ERR_PTR(-ENOSYS); 231 } 232 233 static inline struct gpio_descs *__must_check 234 gpiod_get_array_optional(struct device *dev, const char *con_id, 235 enum gpiod_flags flags) 236 { 237 return NULL; 238 } 239 240 static inline void gpiod_put(struct gpio_desc *desc) 241 { 242 might_sleep(); 243 244 /* GPIO can never have been requested */ 245 WARN_ON(desc); 246 } 247 248 static inline void devm_gpiod_unhinge(struct device *dev, 249 struct gpio_desc *desc) 250 { 251 might_sleep(); 252 253 /* GPIO can never have been requested */ 254 WARN_ON(desc); 255 } 256 257 static inline void gpiod_put_array(struct gpio_descs *descs) 258 { 259 might_sleep(); 260 261 /* GPIO can never have been requested */ 262 WARN_ON(descs); 263 } 264 265 static inline struct gpio_desc *__must_check 266 devm_gpiod_get(struct device *dev, 267 const char *con_id, 268 enum gpiod_flags flags) 269 { 270 return ERR_PTR(-ENOSYS); 271 } 272 static inline 273 struct gpio_desc *__must_check 274 devm_gpiod_get_index(struct device *dev, 275 const char *con_id, 276 unsigned int idx, 277 enum gpiod_flags flags) 278 { 279 return ERR_PTR(-ENOSYS); 280 } 281 282 static inline struct gpio_desc *__must_check 283 devm_gpiod_get_optional(struct device *dev, const char *con_id, 284 enum gpiod_flags flags) 285 { 286 return NULL; 287 } 288 289 static inline struct gpio_desc *__must_check 290 devm_gpiod_get_index_optional(struct device *dev, const char *con_id, 291 unsigned int index, enum gpiod_flags flags) 292 { 293 return NULL; 294 } 295 296 static inline struct gpio_descs *__must_check 297 devm_gpiod_get_array(struct device *dev, const char *con_id, 298 enum gpiod_flags flags) 299 { 300 return ERR_PTR(-ENOSYS); 301 } 302 303 static inline struct gpio_descs *__must_check 304 devm_gpiod_get_array_optional(struct device *dev, const char *con_id, 305 enum gpiod_flags flags) 306 { 307 return NULL; 308 } 309 310 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc) 311 { 312 might_sleep(); 313 314 /* GPIO can never have been requested */ 315 WARN_ON(desc); 316 } 317 318 static inline void devm_gpiod_put_array(struct device *dev, 319 struct gpio_descs *descs) 320 { 321 might_sleep(); 322 323 /* GPIO can never have been requested */ 324 WARN_ON(descs); 325 } 326 327 328 static inline int gpiod_get_direction(const struct gpio_desc *desc) 329 { 330 /* GPIO can never have been requested */ 331 WARN_ON(desc); 332 return -ENOSYS; 333 } 334 static inline int gpiod_direction_input(struct gpio_desc *desc) 335 { 336 /* GPIO can never have been requested */ 337 WARN_ON(desc); 338 return -ENOSYS; 339 } 340 static inline int gpiod_direction_output(struct gpio_desc *desc, int value) 341 { 342 /* GPIO can never have been requested */ 343 WARN_ON(desc); 344 return -ENOSYS; 345 } 346 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value) 347 { 348 /* GPIO can never have been requested */ 349 WARN_ON(desc); 350 return -ENOSYS; 351 } 352 static inline int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, 353 unsigned long flags) 354 { 355 WARN_ON(desc); 356 return -ENOSYS; 357 } 358 static inline int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, 359 unsigned long flags) 360 { 361 WARN_ON(desc); 362 return -ENOSYS; 363 } 364 static inline int gpiod_get_value(const struct gpio_desc *desc) 365 { 366 /* GPIO can never have been requested */ 367 WARN_ON(desc); 368 return 0; 369 } 370 static inline int gpiod_get_array_value(unsigned int array_size, 371 struct gpio_desc **desc_array, 372 struct gpio_array *array_info, 373 unsigned long *value_bitmap) 374 { 375 /* GPIO can never have been requested */ 376 WARN_ON(desc_array); 377 return 0; 378 } 379 static inline void gpiod_set_value(struct gpio_desc *desc, int value) 380 { 381 /* GPIO can never have been requested */ 382 WARN_ON(desc); 383 } 384 static inline int gpiod_set_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_get_raw_value(const struct gpio_desc *desc) 394 { 395 /* GPIO can never have been requested */ 396 WARN_ON(desc); 397 return 0; 398 } 399 static inline int gpiod_get_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 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value) 409 { 410 /* GPIO can never have been requested */ 411 WARN_ON(desc); 412 } 413 static inline int gpiod_set_raw_array_value(unsigned int array_size, 414 struct gpio_desc **desc_array, 415 struct gpio_array *array_info, 416 unsigned long *value_bitmap) 417 { 418 /* GPIO can never have been requested */ 419 WARN_ON(desc_array); 420 return 0; 421 } 422 423 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc) 424 { 425 /* GPIO can never have been requested */ 426 WARN_ON(desc); 427 return 0; 428 } 429 static inline int gpiod_get_array_value_cansleep(unsigned int array_size, 430 struct gpio_desc **desc_array, 431 struct gpio_array *array_info, 432 unsigned long *value_bitmap) 433 { 434 /* GPIO can never have been requested */ 435 WARN_ON(desc_array); 436 return 0; 437 } 438 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) 439 { 440 /* GPIO can never have been requested */ 441 WARN_ON(desc); 442 } 443 static inline int gpiod_set_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 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) 453 { 454 /* GPIO can never have been requested */ 455 WARN_ON(desc); 456 return 0; 457 } 458 static inline int gpiod_get_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 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, 468 int value) 469 { 470 /* GPIO can never have been requested */ 471 WARN_ON(desc); 472 } 473 static inline int gpiod_set_raw_array_value_cansleep(unsigned int array_size, 474 struct gpio_desc **desc_array, 475 struct gpio_array *array_info, 476 unsigned long *value_bitmap) 477 { 478 /* GPIO can never have been requested */ 479 WARN_ON(desc_array); 480 return 0; 481 } 482 483 static inline int gpiod_set_config(struct gpio_desc *desc, unsigned long config) 484 { 485 /* GPIO can never have been requested */ 486 WARN_ON(desc); 487 return -ENOSYS; 488 } 489 490 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce) 491 { 492 /* GPIO can never have been requested */ 493 WARN_ON(desc); 494 return -ENOSYS; 495 } 496 497 static inline void gpiod_toggle_active_low(struct gpio_desc *desc) 498 { 499 /* GPIO can never have been requested */ 500 WARN_ON(desc); 501 } 502 503 static inline int gpiod_is_active_low(const struct gpio_desc *desc) 504 { 505 /* GPIO can never have been requested */ 506 WARN_ON(desc); 507 return 0; 508 } 509 static inline int gpiod_cansleep(const struct gpio_desc *desc) 510 { 511 /* GPIO can never have been requested */ 512 WARN_ON(desc); 513 return 0; 514 } 515 516 static inline int gpiod_to_irq(const struct gpio_desc *desc) 517 { 518 /* GPIO can never have been requested */ 519 WARN_ON(desc); 520 return -EINVAL; 521 } 522 523 static inline int gpiod_set_consumer_name(struct gpio_desc *desc, 524 const char *name) 525 { 526 /* GPIO can never have been requested */ 527 WARN_ON(desc); 528 return -EINVAL; 529 } 530 531 static inline struct gpio_desc *gpio_to_desc(unsigned gpio) 532 { 533 return NULL; 534 } 535 536 static inline int desc_to_gpio(const struct gpio_desc *desc) 537 { 538 /* GPIO can never have been requested */ 539 WARN_ON(desc); 540 return -EINVAL; 541 } 542 543 static inline 544 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode, 545 const char *con_id, int index, 546 enum gpiod_flags flags, 547 const char *label) 548 { 549 return ERR_PTR(-ENOSYS); 550 } 551 552 static inline 553 struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev, 554 struct fwnode_handle *fwnode, 555 const char *con_id, int index, 556 enum gpiod_flags flags, 557 const char *label) 558 { 559 return ERR_PTR(-ENOSYS); 560 } 561 562 #endif /* CONFIG_GPIOLIB */ 563 564 static inline 565 struct gpio_desc *devm_fwnode_gpiod_get(struct device *dev, 566 struct fwnode_handle *fwnode, 567 const char *con_id, 568 enum gpiod_flags flags, 569 const char *label) 570 { 571 return devm_fwnode_gpiod_get_index(dev, fwnode, con_id, 0, 572 flags, label); 573 } 574 575 struct acpi_gpio_params { 576 unsigned int crs_entry_index; 577 unsigned int line_index; 578 bool active_low; 579 }; 580 581 struct acpi_gpio_mapping { 582 const char *name; 583 const struct acpi_gpio_params *data; 584 unsigned int size; 585 586 /* Ignore IoRestriction field */ 587 #define ACPI_GPIO_QUIRK_NO_IO_RESTRICTION BIT(0) 588 /* 589 * When ACPI GPIO mapping table is in use the index parameter inside it 590 * refers to the GPIO resource in _CRS method. That index has no 591 * distinction of actual type of the resource. When consumer wants to 592 * get GpioIo type explicitly, this quirk may be used. 593 */ 594 #define ACPI_GPIO_QUIRK_ONLY_GPIOIO BIT(1) 595 /* Use given pin as an absolute GPIO number in the system */ 596 #define ACPI_GPIO_QUIRK_ABSOLUTE_NUMBER BIT(2) 597 598 unsigned int quirks; 599 }; 600 601 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_ACPI) 602 603 int acpi_dev_add_driver_gpios(struct acpi_device *adev, 604 const struct acpi_gpio_mapping *gpios); 605 void acpi_dev_remove_driver_gpios(struct acpi_device *adev); 606 607 int devm_acpi_dev_add_driver_gpios(struct device *dev, 608 const struct acpi_gpio_mapping *gpios); 609 610 #else /* CONFIG_GPIOLIB && CONFIG_ACPI */ 611 612 #include <linux/err.h> 613 614 static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev, 615 const struct acpi_gpio_mapping *gpios) 616 { 617 return -ENXIO; 618 } 619 static inline void acpi_dev_remove_driver_gpios(struct acpi_device *adev) {} 620 621 static inline int devm_acpi_dev_add_driver_gpios(struct device *dev, 622 const struct acpi_gpio_mapping *gpios) 623 { 624 return -ENXIO; 625 } 626 627 #endif /* CONFIG_GPIOLIB && CONFIG_ACPI */ 628 629 630 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS) 631 632 int gpiod_export(struct gpio_desc *desc, bool direction_may_change); 633 int gpiod_export_link(struct device *dev, const char *name, 634 struct gpio_desc *desc); 635 void gpiod_unexport(struct gpio_desc *desc); 636 637 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ 638 639 #include <asm/errno.h> 640 641 static inline int gpiod_export(struct gpio_desc *desc, 642 bool direction_may_change) 643 { 644 return -ENOSYS; 645 } 646 647 static inline int gpiod_export_link(struct device *dev, const char *name, 648 struct gpio_desc *desc) 649 { 650 return -ENOSYS; 651 } 652 653 static inline void gpiod_unexport(struct gpio_desc *desc) 654 { 655 } 656 657 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ 658 659 static inline int gpiod_multi_set_value_cansleep(struct gpio_descs *descs, 660 unsigned long *value_bitmap) 661 { 662 if (IS_ERR_OR_NULL(descs)) 663 return PTR_ERR_OR_ZERO(descs); 664 665 return gpiod_set_array_value_cansleep(descs->ndescs, descs->desc, 666 descs->info, value_bitmap); 667 } 668 669 #endif 670