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