gpiolib-legacy.c 2.4 KB

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