1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/bitops.h> 3 #include <linux/device.h> 4 #include <linux/errno.h> 5 #include <linux/export.h> 6 #include <linux/gfp.h> 7 8 #include <linux/gpio/consumer.h> 9 #include <linux/gpio/driver.h> 10 11 #include <linux/gpio.h> 12 13 #include "gpiolib.h" 14 15 /* 16 * **DEPRECATED** This function is deprecated and must not be used in new code. 17 */ 18 void gpio_free(unsigned gpio) 19 { 20 gpiod_free(gpio_to_desc(gpio)); 21 } 22 EXPORT_SYMBOL_GPL(gpio_free); 23 24 /** 25 * gpio_request_one - request a single GPIO with initial configuration 26 * @gpio: the GPIO number 27 * @flags: GPIO configuration as specified by GPIOF_* 28 * @label: a literal description string of this GPIO 29 * 30 * **DEPRECATED** This function is deprecated and must not be used in new code. 31 * 32 * Returns: 33 * 0 on success, or negative errno on failure. 34 */ 35 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label) 36 { 37 int err; 38 39 err = gpio_request(gpio, label); 40 if (err) 41 return err; 42 43 if (flags & GPIOF_IN) 44 err = gpio_direction_input(gpio); 45 else 46 err = gpio_direction_output(gpio, !!(flags & GPIOF_OUT_INIT_HIGH)); 47 48 if (err) 49 gpio_free(gpio); 50 51 return err; 52 } 53 EXPORT_SYMBOL_GPL(gpio_request_one); 54 55 /* 56 * **DEPRECATED** This function is deprecated and must not be used in new code. 57 */ 58 int gpio_request(unsigned gpio, const char *label) 59 { 60 struct gpio_desc *desc; 61 62 /* Compatibility: assume unavailable "valid" GPIOs will appear later */ 63 desc = gpio_to_desc(gpio); 64 if (!desc) 65 return -EPROBE_DEFER; 66 67 return gpiod_request(desc, label); 68 } 69 EXPORT_SYMBOL_GPL(gpio_request); 70 71 static void devm_gpio_release(void *gpio) 72 { 73 gpio_free((unsigned)(unsigned long)gpio); 74 } 75 76 /** 77 * devm_gpio_request_one - request a single GPIO with initial setup 78 * @dev: device to request for 79 * @gpio: the GPIO number 80 * @flags: GPIO configuration as specified by GPIOF_* 81 * @label: a literal description string of this GPIO 82 * 83 * **DEPRECATED** This function is deprecated and must not be used in new code. 84 * 85 * Returns: 86 * 0 on success, or negative errno on failure. 87 */ 88 int devm_gpio_request_one(struct device *dev, unsigned gpio, 89 unsigned long flags, const char *label) 90 { 91 int rc; 92 93 rc = gpio_request(gpio, label); 94 if (rc) 95 return rc; 96 97 if (flags & GPIOF_IN) 98 rc = gpio_direction_input(gpio); 99 else 100 rc = gpio_direction_output(gpio, !!(flags & GPIOF_OUT_INIT_HIGH)); 101 102 if (rc) { 103 gpio_free(gpio); 104 return rc; 105 } 106 107 return devm_add_action_or_reset(dev, devm_gpio_release, (void *)(unsigned long)gpio); 108 } 109 EXPORT_SYMBOL_GPL(devm_gpio_request_one); 110