at v6.4 19 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/bits.h> 6#include <linux/types.h> 7 8struct acpi_device; 9struct device; 10struct fwnode_handle; 11 12struct gpio_array; 13struct gpio_desc; 14 15/** 16 * struct gpio_descs - Struct containing an array of descriptors that can be 17 * obtained using gpiod_get_array() 18 * 19 * @info: Pointer to the opaque gpio_array structure 20 * @ndescs: Number of held descriptors 21 * @desc: Array of pointers to GPIO descriptors 22 */ 23struct gpio_descs { 24 struct gpio_array *info; 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#define GPIOD_FLAGS_BIT_NONEXCLUSIVE BIT(4) 34 35/** 36 * enum gpiod_flags - Optional flags that can be passed to one of gpiod_* to 37 * configure direction and output value. These values 38 * cannot be OR'd. 39 * 40 * @GPIOD_ASIS: Don't change anything 41 * @GPIOD_IN: Set lines to input mode 42 * @GPIOD_OUT_LOW: Set lines to output and drive them low 43 * @GPIOD_OUT_HIGH: Set lines to output and drive them high 44 * @GPIOD_OUT_LOW_OPEN_DRAIN: Set lines to open-drain output and drive them low 45 * @GPIOD_OUT_HIGH_OPEN_DRAIN: Set lines to open-drain output and drive them high 46 */ 47enum gpiod_flags { 48 GPIOD_ASIS = 0, 49 GPIOD_IN = GPIOD_FLAGS_BIT_DIR_SET, 50 GPIOD_OUT_LOW = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT, 51 GPIOD_OUT_HIGH = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT | 52 GPIOD_FLAGS_BIT_DIR_VAL, 53 GPIOD_OUT_LOW_OPEN_DRAIN = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_OPEN_DRAIN, 54 GPIOD_OUT_HIGH_OPEN_DRAIN = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_OPEN_DRAIN, 55}; 56 57#ifdef CONFIG_GPIOLIB 58 59/* Return the number of GPIOs associated with a device / function */ 60int gpiod_count(struct device *dev, const char *con_id); 61 62/* Acquire and dispose GPIOs */ 63struct gpio_desc *__must_check gpiod_get(struct device *dev, 64 const char *con_id, 65 enum gpiod_flags flags); 66struct gpio_desc *__must_check gpiod_get_index(struct device *dev, 67 const char *con_id, 68 unsigned int idx, 69 enum gpiod_flags flags); 70struct gpio_desc *__must_check gpiod_get_optional(struct device *dev, 71 const char *con_id, 72 enum gpiod_flags flags); 73struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev, 74 const char *con_id, 75 unsigned int index, 76 enum gpiod_flags flags); 77struct gpio_descs *__must_check gpiod_get_array(struct device *dev, 78 const char *con_id, 79 enum gpiod_flags flags); 80struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev, 81 const char *con_id, 82 enum gpiod_flags flags); 83void gpiod_put(struct gpio_desc *desc); 84void gpiod_put_array(struct gpio_descs *descs); 85 86struct gpio_desc *__must_check devm_gpiod_get(struct device *dev, 87 const char *con_id, 88 enum gpiod_flags flags); 89struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, 90 const char *con_id, 91 unsigned int idx, 92 enum gpiod_flags flags); 93struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev, 94 const char *con_id, 95 enum gpiod_flags flags); 96struct gpio_desc *__must_check 97devm_gpiod_get_index_optional(struct device *dev, const char *con_id, 98 unsigned int index, enum gpiod_flags flags); 99struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev, 100 const char *con_id, 101 enum gpiod_flags flags); 102struct gpio_descs *__must_check 103devm_gpiod_get_array_optional(struct device *dev, const char *con_id, 104 enum gpiod_flags flags); 105void devm_gpiod_put(struct device *dev, struct gpio_desc *desc); 106void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc); 107void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs); 108 109int gpiod_get_direction(struct gpio_desc *desc); 110int gpiod_direction_input(struct gpio_desc *desc); 111int gpiod_direction_output(struct gpio_desc *desc, int value); 112int gpiod_direction_output_raw(struct gpio_desc *desc, int value); 113int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags); 114int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags); 115 116/* Value get/set from non-sleeping context */ 117int gpiod_get_value(const struct gpio_desc *desc); 118int gpiod_get_array_value(unsigned int array_size, 119 struct gpio_desc **desc_array, 120 struct gpio_array *array_info, 121 unsigned long *value_bitmap); 122void gpiod_set_value(struct gpio_desc *desc, int value); 123int gpiod_set_array_value(unsigned int array_size, 124 struct gpio_desc **desc_array, 125 struct gpio_array *array_info, 126 unsigned long *value_bitmap); 127int gpiod_get_raw_value(const struct gpio_desc *desc); 128int gpiod_get_raw_array_value(unsigned int array_size, 129 struct gpio_desc **desc_array, 130 struct gpio_array *array_info, 131 unsigned long *value_bitmap); 132void gpiod_set_raw_value(struct gpio_desc *desc, int value); 133int gpiod_set_raw_array_value(unsigned int array_size, 134 struct gpio_desc **desc_array, 135 struct gpio_array *array_info, 136 unsigned long *value_bitmap); 137 138/* Value get/set from sleeping context */ 139int gpiod_get_value_cansleep(const struct gpio_desc *desc); 140int gpiod_get_array_value_cansleep(unsigned int array_size, 141 struct gpio_desc **desc_array, 142 struct gpio_array *array_info, 143 unsigned long *value_bitmap); 144void gpiod_set_value_cansleep(struct gpio_desc *desc, int value); 145int gpiod_set_array_value_cansleep(unsigned int array_size, 146 struct gpio_desc **desc_array, 147 struct gpio_array *array_info, 148 unsigned long *value_bitmap); 149int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc); 150int gpiod_get_raw_array_value_cansleep(unsigned int array_size, 151 struct gpio_desc **desc_array, 152 struct gpio_array *array_info, 153 unsigned long *value_bitmap); 154void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value); 155int gpiod_set_raw_array_value_cansleep(unsigned int array_size, 156 struct gpio_desc **desc_array, 157 struct gpio_array *array_info, 158 unsigned long *value_bitmap); 159 160int gpiod_set_config(struct gpio_desc *desc, unsigned long config); 161int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce); 162int gpiod_set_transitory(struct gpio_desc *desc, bool transitory); 163void gpiod_toggle_active_low(struct gpio_desc *desc); 164 165int gpiod_is_active_low(const struct gpio_desc *desc); 166int gpiod_cansleep(const struct gpio_desc *desc); 167 168int gpiod_to_irq(const struct gpio_desc *desc); 169int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name); 170 171/* Convert between the old gpio_ and new gpiod_ interfaces */ 172struct gpio_desc *gpio_to_desc(unsigned gpio); 173int desc_to_gpio(const struct gpio_desc *desc); 174 175struct 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); 179struct 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 192static inline int gpiod_count(struct device *dev, const char *con_id) 193{ 194 return 0; 195} 196 197static 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} 203static inline struct gpio_desc *__must_check 204gpiod_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 212static inline struct gpio_desc *__must_check 213gpiod_get_optional(struct device *dev, const char *con_id, 214 enum gpiod_flags flags) 215{ 216 return NULL; 217} 218 219static inline struct gpio_desc *__must_check 220gpiod_get_index_optional(struct device *dev, const char *con_id, 221 unsigned int index, enum gpiod_flags flags) 222{ 223 return NULL; 224} 225 226static inline struct gpio_descs *__must_check 227gpiod_get_array(struct device *dev, const char *con_id, 228 enum gpiod_flags flags) 229{ 230 return ERR_PTR(-ENOSYS); 231} 232 233static inline struct gpio_descs *__must_check 234gpiod_get_array_optional(struct device *dev, const char *con_id, 235 enum gpiod_flags flags) 236{ 237 return NULL; 238} 239 240static 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 248static 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 257static 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 265static inline struct gpio_desc *__must_check 266devm_gpiod_get(struct device *dev, 267 const char *con_id, 268 enum gpiod_flags flags) 269{ 270 return ERR_PTR(-ENOSYS); 271} 272static inline 273struct gpio_desc *__must_check 274devm_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 282static inline struct gpio_desc *__must_check 283devm_gpiod_get_optional(struct device *dev, const char *con_id, 284 enum gpiod_flags flags) 285{ 286 return NULL; 287} 288 289static inline struct gpio_desc *__must_check 290devm_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 296static inline struct gpio_descs *__must_check 297devm_gpiod_get_array(struct device *dev, const char *con_id, 298 enum gpiod_flags flags) 299{ 300 return ERR_PTR(-ENOSYS); 301} 302 303static inline struct gpio_descs *__must_check 304devm_gpiod_get_array_optional(struct device *dev, const char *con_id, 305 enum gpiod_flags flags) 306{ 307 return NULL; 308} 309 310static 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 318static 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 328static 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} 334static 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} 340static 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} 346static 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} 352static inline int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, 353 unsigned long flags) 354{ 355 WARN_ON(desc); 356 return -ENOSYS; 357} 358static inline int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, 359 unsigned long flags) 360{ 361 WARN_ON(desc); 362 return -ENOSYS; 363} 364static 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} 370static 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} 379static inline void gpiod_set_value(struct gpio_desc *desc, int value) 380{ 381 /* GPIO can never have been requested */ 382 WARN_ON(desc); 383} 384static 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} 393static 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} 399static 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} 408static 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} 413static 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 423static 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} 429static 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} 438static 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} 443static 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} 452static 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} 458static 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} 467static 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} 473static 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 483static 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 490static 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 497static inline int gpiod_set_transitory(struct gpio_desc *desc, bool transitory) 498{ 499 /* GPIO can never have been requested */ 500 WARN_ON(desc); 501 return -ENOSYS; 502} 503 504static inline void gpiod_toggle_active_low(struct gpio_desc *desc) 505{ 506 /* GPIO can never have been requested */ 507 WARN_ON(desc); 508} 509 510static inline int gpiod_is_active_low(const struct gpio_desc *desc) 511{ 512 /* GPIO can never have been requested */ 513 WARN_ON(desc); 514 return 0; 515} 516static inline int gpiod_cansleep(const struct gpio_desc *desc) 517{ 518 /* GPIO can never have been requested */ 519 WARN_ON(desc); 520 return 0; 521} 522 523static inline int gpiod_to_irq(const struct gpio_desc *desc) 524{ 525 /* GPIO can never have been requested */ 526 WARN_ON(desc); 527 return -EINVAL; 528} 529 530static inline int gpiod_set_consumer_name(struct gpio_desc *desc, 531 const char *name) 532{ 533 /* GPIO can never have been requested */ 534 WARN_ON(desc); 535 return -EINVAL; 536} 537 538static inline struct gpio_desc *gpio_to_desc(unsigned gpio) 539{ 540 return NULL; 541} 542 543static inline int desc_to_gpio(const struct gpio_desc *desc) 544{ 545 /* GPIO can never have been requested */ 546 WARN_ON(desc); 547 return -EINVAL; 548} 549 550static inline 551struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode, 552 const char *con_id, int index, 553 enum gpiod_flags flags, 554 const char *label) 555{ 556 return ERR_PTR(-ENOSYS); 557} 558 559static inline 560struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev, 561 struct fwnode_handle *fwnode, 562 const char *con_id, int index, 563 enum gpiod_flags flags, 564 const char *label) 565{ 566 return ERR_PTR(-ENOSYS); 567} 568 569#endif /* CONFIG_GPIOLIB */ 570 571static inline 572struct gpio_desc *devm_fwnode_gpiod_get(struct device *dev, 573 struct fwnode_handle *fwnode, 574 const char *con_id, 575 enum gpiod_flags flags, 576 const char *label) 577{ 578 return devm_fwnode_gpiod_get_index(dev, fwnode, con_id, 0, 579 flags, label); 580} 581 582struct acpi_gpio_params { 583 unsigned int crs_entry_index; 584 unsigned int line_index; 585 bool active_low; 586}; 587 588struct acpi_gpio_mapping { 589 const char *name; 590 const struct acpi_gpio_params *data; 591 unsigned int size; 592 593/* Ignore IoRestriction field */ 594#define ACPI_GPIO_QUIRK_NO_IO_RESTRICTION BIT(0) 595/* 596 * When ACPI GPIO mapping table is in use the index parameter inside it 597 * refers to the GPIO resource in _CRS method. That index has no 598 * distinction of actual type of the resource. When consumer wants to 599 * get GpioIo type explicitly, this quirk may be used. 600 */ 601#define ACPI_GPIO_QUIRK_ONLY_GPIOIO BIT(1) 602/* Use given pin as an absolute GPIO number in the system */ 603#define ACPI_GPIO_QUIRK_ABSOLUTE_NUMBER BIT(2) 604 605 unsigned int quirks; 606}; 607 608#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_ACPI) 609 610int acpi_dev_add_driver_gpios(struct acpi_device *adev, 611 const struct acpi_gpio_mapping *gpios); 612void acpi_dev_remove_driver_gpios(struct acpi_device *adev); 613 614int devm_acpi_dev_add_driver_gpios(struct device *dev, 615 const struct acpi_gpio_mapping *gpios); 616 617struct gpio_desc *acpi_get_and_request_gpiod(char *path, unsigned int pin, char *label); 618 619#else /* CONFIG_GPIOLIB && CONFIG_ACPI */ 620 621#include <linux/err.h> 622 623static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev, 624 const struct acpi_gpio_mapping *gpios) 625{ 626 return -ENXIO; 627} 628static inline void acpi_dev_remove_driver_gpios(struct acpi_device *adev) {} 629 630static inline int devm_acpi_dev_add_driver_gpios(struct device *dev, 631 const struct acpi_gpio_mapping *gpios) 632{ 633 return -ENXIO; 634} 635 636static inline struct gpio_desc *acpi_get_and_request_gpiod(char *path, unsigned int pin, 637 char *label) 638{ 639 return ERR_PTR(-ENOSYS); 640} 641 642#endif /* CONFIG_GPIOLIB && CONFIG_ACPI */ 643 644 645#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS) 646 647int gpiod_export(struct gpio_desc *desc, bool direction_may_change); 648int gpiod_export_link(struct device *dev, const char *name, 649 struct gpio_desc *desc); 650void gpiod_unexport(struct gpio_desc *desc); 651 652#else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ 653 654#include <asm/errno.h> 655 656static inline int gpiod_export(struct gpio_desc *desc, 657 bool direction_may_change) 658{ 659 return -ENOSYS; 660} 661 662static inline int gpiod_export_link(struct device *dev, const char *name, 663 struct gpio_desc *desc) 664{ 665 return -ENOSYS; 666} 667 668static inline void gpiod_unexport(struct gpio_desc *desc) 669{ 670} 671 672#endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ 673 674#endif