163 lines · c
1// SPDX-License-Identifier: GPL-2.02#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 */18void gpio_free(unsigned gpio)19{20 gpiod_free(gpio_to_desc(gpio));21}22EXPORT_SYMBOL_GPL(gpio_free);23 24/**25 * gpio_request_one - request a single GPIO with initial configuration26 * @gpio: the GPIO number27 * @flags: GPIO configuration as specified by GPIOF_*28 * @label: a literal description string of this GPIO29 *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 */35int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)36{37 struct gpio_desc *desc;38 int err;39 40 /* Compatibility: assume unavailable "valid" GPIOs will appear later */41 desc = gpio_to_desc(gpio);42 if (!desc)43 return -EPROBE_DEFER;44 45 err = gpiod_request(desc, label);46 if (err)47 return err;48 49 if (flags & GPIOF_ACTIVE_LOW)50 set_bit(FLAG_ACTIVE_LOW, &desc->flags);51 52 if (flags & GPIOF_IN)53 err = gpiod_direction_input(desc);54 else55 err = gpiod_direction_output_raw(desc, !!(flags & GPIOF_OUT_INIT_HIGH));56 57 if (err)58 goto free_gpio;59 60 return 0;61 62 free_gpio:63 gpiod_free(desc);64 return err;65}66EXPORT_SYMBOL_GPL(gpio_request_one);67 68/*69 * **DEPRECATED** This function is deprecated and must not be used in new code.70 */71int gpio_request(unsigned gpio, const char *label)72{73 struct gpio_desc *desc;74 75 /* Compatibility: assume unavailable "valid" GPIOs will appear later */76 desc = gpio_to_desc(gpio);77 if (!desc)78 return -EPROBE_DEFER;79 80 return gpiod_request(desc, label);81}82EXPORT_SYMBOL_GPL(gpio_request);83 84static void devm_gpio_release(struct device *dev, void *res)85{86 unsigned *gpio = res;87 88 gpio_free(*gpio);89}90 91/**92 * devm_gpio_request - request a GPIO for a managed device93 * @dev: device to request the GPIO for94 * @gpio: GPIO to allocate95 * @label: the name of the requested GPIO96 *97 * Except for the extra @dev argument, this function takes the98 * same arguments and performs the same function as gpio_request().99 * GPIOs requested with this function will be automatically freed100 * on driver detach.101 *102 * **DEPRECATED** This function is deprecated and must not be used in new code.103 *104 * Returns:105 * 0 on success, or negative errno on failure.106 */107int devm_gpio_request(struct device *dev, unsigned gpio, const char *label)108{109 unsigned *dr;110 int rc;111 112 dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);113 if (!dr)114 return -ENOMEM;115 116 rc = gpio_request(gpio, label);117 if (rc) {118 devres_free(dr);119 return rc;120 }121 122 *dr = gpio;123 devres_add(dev, dr);124 125 return 0;126}127EXPORT_SYMBOL_GPL(devm_gpio_request);128 129/**130 * devm_gpio_request_one - request a single GPIO with initial setup131 * @dev: device to request for132 * @gpio: the GPIO number133 * @flags: GPIO configuration as specified by GPIOF_*134 * @label: a literal description string of this GPIO135 *136 * **DEPRECATED** This function is deprecated and must not be used in new code.137 *138 * Returns:139 * 0 on success, or negative errno on failure.140 */141int devm_gpio_request_one(struct device *dev, unsigned gpio,142 unsigned long flags, const char *label)143{144 unsigned *dr;145 int rc;146 147 dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);148 if (!dr)149 return -ENOMEM;150 151 rc = gpio_request_one(gpio, flags, label);152 if (rc) {153 devres_free(dr);154 return rc;155 }156 157 *dr = gpio;158 devres_add(dev, dr);159 160 return 0;161}162EXPORT_SYMBOL_GPL(devm_gpio_request_one);163