gpiolib-legacy.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. err = gpiod_request(desc, label);
  22. if (err)
  23. return err;
  24. if (flags & GPIOF_OPEN_DRAIN)
  25. set_bit(FLAG_OPEN_DRAIN, &desc->flags);
  26. if (flags & GPIOF_OPEN_SOURCE)
  27. set_bit(FLAG_OPEN_SOURCE, &desc->flags);
  28. if (flags & GPIOF_ACTIVE_LOW)
  29. set_bit(FLAG_ACTIVE_LOW, &desc->flags);
  30. if (flags & GPIOF_DIR_IN)
  31. err = gpiod_direction_input(desc);
  32. else
  33. err = gpiod_direction_output_raw(desc,
  34. (flags & GPIOF_INIT_HIGH) ? 1 : 0);
  35. if (err)
  36. goto free_gpio;
  37. if (flags & GPIOF_EXPORT) {
  38. err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
  39. if (err)
  40. goto free_gpio;
  41. }
  42. return 0;
  43. free_gpio:
  44. gpiod_free(desc);
  45. return err;
  46. }
  47. EXPORT_SYMBOL_GPL(gpio_request_one);
  48. int gpio_request(unsigned gpio, const char *label)
  49. {
  50. return gpiod_request(gpio_to_desc(gpio), label);
  51. }
  52. EXPORT_SYMBOL_GPL(gpio_request);
  53. /**
  54. * gpio_request_array - request multiple GPIOs in a single call
  55. * @array: array of the 'struct gpio'
  56. * @num: how many GPIOs in the array
  57. */
  58. int gpio_request_array(const struct gpio *array, size_t num)
  59. {
  60. int i, err;
  61. for (i = 0; i < num; i++, array++) {
  62. err = gpio_request_one(array->gpio, array->flags, array->label);
  63. if (err)
  64. goto err_free;
  65. }
  66. return 0;
  67. err_free:
  68. while (i--)
  69. gpio_free((--array)->gpio);
  70. return err;
  71. }
  72. EXPORT_SYMBOL_GPL(gpio_request_array);
  73. /**
  74. * gpio_free_array - release multiple GPIOs in a single call
  75. * @array: array of the 'struct gpio'
  76. * @num: how many GPIOs in the array
  77. */
  78. void gpio_free_array(const struct gpio *array, size_t num)
  79. {
  80. while (num--)
  81. gpio_free((array++)->gpio);
  82. }
  83. EXPORT_SYMBOL_GPL(gpio_free_array);