gpio.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2021 Western Digital Corporation or its affiliates.
  5. *
  6. * Authors:
  7. * Anup Patel <anup.patel@wdc.com>
  8. */
  9. #ifndef __GPIO_H__
  10. #define __GPIO_H__
  11. #include <sbi/sbi_types.h>
  12. #include <sbi/sbi_list.h>
  13. #define GPIO_LINE_DIRECTION_IN 1
  14. #define GPIO_LINE_DIRECTION_OUT 0
  15. /** Representation of a GPIO pin */
  16. struct gpio_pin {
  17. /** Pointer to the GPIO chip */
  18. struct gpio_chip *chip;
  19. /** Identification of GPIO pin within GPIO chip */
  20. unsigned int offset;
  21. /**
  22. * Additional configuration flags of the GPIO pin desired
  23. * by GPIO clients.
  24. *
  25. * NOTE: GPIO chip can have custom configuration flags.
  26. */
  27. unsigned int flags;
  28. #define GPIO_FLAG_ACTIVE_LOW 0x1
  29. #define GPIO_FLAG_SINGLE_ENDED 0x2
  30. #define GPIO_FLAG_OPEN_DRAIN 0x4
  31. #define GPIO_FLAG_TRANSITORY 0x8
  32. #define GPIO_FLAG_PULL_UP 0x10
  33. #define GPIO_FLAG_PULL_DOWN 0x20
  34. };
  35. /** Representation of a GPIO chip */
  36. struct gpio_chip {
  37. /** Pointer to GPIO driver owning this GPIO chip */
  38. void *driver;
  39. /** Uniquie ID of the GPIO chip assigned by the driver */
  40. unsigned int id;
  41. /** Number of GPIOs supported by the GPIO chip */
  42. unsigned int ngpio;
  43. /**
  44. * Get current direction of GPIO pin
  45. *
  46. * @return 0=output, 1=input, or negative error
  47. */
  48. int (*get_direction)(struct gpio_pin *gp);
  49. /**
  50. * Set input direction of GPIO pin
  51. *
  52. * @return 0 on success and negative error code on failure
  53. */
  54. int (*direction_input)(struct gpio_pin *gp);
  55. /**
  56. * Set output direction of GPIO pin with given output value
  57. *
  58. * @return 0 on success and negative error code on failure
  59. */
  60. int (*direction_output)(struct gpio_pin *gp, int value);
  61. /**
  62. * Get current value of GPIO pin
  63. *
  64. * @return 0=low, 1=high, or negative error
  65. */
  66. int (*get)(struct gpio_pin *gp);
  67. /** Set output value for GPIO pin */
  68. void (*set)(struct gpio_pin *gp, int value);
  69. /** List */
  70. struct sbi_dlist node;
  71. };
  72. static inline struct gpio_chip *to_gpio_chip(struct sbi_dlist *node)
  73. {
  74. return container_of(node, struct gpio_chip, node);
  75. }
  76. /** Find a registered GPIO chip */
  77. struct gpio_chip *gpio_chip_find(unsigned int id);
  78. /** Register GPIO chip */
  79. int gpio_chip_add(struct gpio_chip *gc);
  80. /** Un-register GPIO chip */
  81. void gpio_chip_remove(struct gpio_chip *gc);
  82. /** Get current direction of GPIO pin */
  83. int gpio_get_direction(struct gpio_pin *gp);
  84. /** Set input direction of GPIO pin */
  85. int gpio_direction_input(struct gpio_pin *gp);
  86. /** Set output direction of GPIO pin */
  87. int gpio_direction_output(struct gpio_pin *gp, int value);
  88. /** Get current value of GPIO pin */
  89. int gpio_get(struct gpio_pin *gp);
  90. /** Set output value of GPIO pin */
  91. int gpio_set(struct gpio_pin *gp, int value);
  92. #endif