gpiolib-legacy.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <linux/gpio/consumer.h>
  2. #include <linux/gpio/driver.h>
  3. #include <linux/gpio.h>
  4. #include "gpiolib.h"
  5. void gpio_free(unsigned gpio)
  6. {
  7. gpiod_free(gpio_to_desc(gpio));
  8. }
  9. EXPORT_SYMBOL_GPL(gpio_free);
  10. /**
  11. * gpio_request_one - request a single GPIO with initial configuration
  12. * @gpio: the GPIO number
  13. * @flags: GPIO configuration as specified by GPIOF_*
  14. * @label: a literal description string of this GPIO
  15. */
  16. int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
  17. {
  18. struct gpio_desc *desc;
  19. int err;
  20. desc = gpio_to_desc(gpio);
  21. /* Compatibility: assume unavailable "valid" GPIOs will appear later */
  22. if (!desc && gpio_is_valid(gpio))
  23. return -EPROBE_DEFER;
  24. err = gpiod_request(desc, label);
  25. if (err)
  26. return err;
  27. if (flags & GPIOF_OPEN_DRAIN)
  28. set_bit(FLAG_OPEN_DRAIN, &desc->flags);
  29. if (flags & GPIOF_OPEN_SOURCE)
  30. set_bit(FLAG_OPEN_SOURCE, &desc->flags);
  31. if (flags & GPIOF_ACTIVE_LOW)
  32. set_bit(FLAG_ACTIVE_LOW, &desc->flags);
  33. if (flags & GPIOF_DIR_IN)
  34. err = gpiod_direction_input(desc);
  35. else
  36. err = gpiod_direction_output_raw(desc,
  37. (flags & GPIOF_INIT_HIGH) ? 1 : 0);
  38. if (err)
  39. goto free_gpio;
  40. if (flags & GPIOF_EXPORT) {
  41. err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
  42. if (err)
  43. goto free_gpio;
  44. }
  45. return 0;
  46. free_gpio:
  47. gpiod_free(desc);
  48. return err;
  49. }
  50. EXPORT_SYMBOL_GPL(gpio_request_one);
  51. int gpio_request(unsigned gpio, const char *label)
  52. {
  53. struct gpio_desc *desc = gpio_to_desc(gpio);
  54. /* Compatibility: assume unavailable "valid" GPIOs will appear later */
  55. if (!desc && gpio_is_valid(gpio))
  56. return -EPROBE_DEFER;
  57. return gpiod_request(desc, label);
  58. }
  59. EXPORT_SYMBOL_GPL(gpio_request);
  60. /**
  61. * gpio_request_array - request multiple GPIOs in a single call
  62. * @array: array of the 'struct gpio'
  63. * @num: how many GPIOs in the array
  64. */
  65. int gpio_request_array(const struct gpio *array, size_t num)
  66. {
  67. int i, err;
  68. for (i = 0; i < num; i++, array++) {
  69. err = gpio_request_one(array->gpio, array->flags, array->label);
  70. if (err)
  71. goto err_free;
  72. }
  73. return 0;
  74. err_free:
  75. while (i--)
  76. gpio_free((--array)->gpio);
  77. return err;
  78. }
  79. EXPORT_SYMBOL_GPL(gpio_request_array);
  80. /**
  81. * gpio_free_array - release multiple GPIOs in a single call
  82. * @array: array of the 'struct gpio'
  83. * @num: how many GPIOs in the array
  84. */
  85. void gpio_free_array(const struct gpio *array, size_t num)
  86. {
  87. while (num--)
  88. gpio_free((array++)->gpio);
  89. }
  90. EXPORT_SYMBOL_GPL(gpio_free_array);