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