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