xref: /linux/drivers/gpio/gpiolib-legacy.c (revision 22c55fb9eb92395d999b8404d73e58540d11bdd8)
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 	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_IN)
50 		err = gpiod_direction_input(desc);
51 	else
52 		err = gpiod_direction_output_raw(desc, !!(flags & GPIOF_OUT_INIT_HIGH));
53 
54 	if (err)
55 		goto free_gpio;
56 
57 	return 0;
58 
59  free_gpio:
60 	gpiod_free(desc);
61 	return err;
62 }
63 EXPORT_SYMBOL_GPL(gpio_request_one);
64 
65 /*
66  * **DEPRECATED** This function is deprecated and must not be used in new code.
67  */
68 int gpio_request(unsigned gpio, const char *label)
69 {
70 	struct gpio_desc *desc;
71 
72 	/* Compatibility: assume unavailable "valid" GPIOs will appear later */
73 	desc = gpio_to_desc(gpio);
74 	if (!desc)
75 		return -EPROBE_DEFER;
76 
77 	return gpiod_request(desc, label);
78 }
79 EXPORT_SYMBOL_GPL(gpio_request);
80 
81 static void devm_gpio_release(struct device *dev, void *res)
82 {
83 	unsigned *gpio = res;
84 
85 	gpio_free(*gpio);
86 }
87 
88 /**
89  * devm_gpio_request_one - request a single GPIO with initial setup
90  * @dev: device to request for
91  * @gpio: the GPIO number
92  * @flags: GPIO configuration as specified by GPIOF_*
93  * @label: a literal description string of this GPIO
94  *
95  * **DEPRECATED** This function is deprecated and must not be used in new code.
96  *
97  * Returns:
98  * 0 on success, or negative errno on failure.
99  */
100 int devm_gpio_request_one(struct device *dev, unsigned gpio,
101 			  unsigned long flags, const char *label)
102 {
103 	unsigned *dr;
104 	int rc;
105 
106 	dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
107 	if (!dr)
108 		return -ENOMEM;
109 
110 	rc = gpio_request_one(gpio, flags, label);
111 	if (rc) {
112 		devres_free(dr);
113 		return rc;
114 	}
115 
116 	*dr = gpio;
117 	devres_add(dev, dr);
118 
119 	return 0;
120 }
121 EXPORT_SYMBOL_GPL(devm_gpio_request_one);
122