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 */
gpio_free(unsigned gpio)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 */
gpio_request_one(unsigned gpio,unsigned long flags,const char * label)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_ACTIVE_LOW)
50 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
51
52 if (flags & GPIOF_IN)
53 err = gpiod_direction_input(desc);
54 else
55 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 }
66 EXPORT_SYMBOL_GPL(gpio_request_one);
67
68 /*
69 * **DEPRECATED** This function is deprecated and must not be used in new code.
70 */
gpio_request(unsigned gpio,const char * label)71 int 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 }
82 EXPORT_SYMBOL_GPL(gpio_request);
83
devm_gpio_release(struct device * dev,void * res)84 static 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 device
93 * @dev: device to request the GPIO for
94 * @gpio: GPIO to allocate
95 * @label: the name of the requested GPIO
96 *
97 * Except for the extra @dev argument, this function takes the
98 * same arguments and performs the same function as gpio_request().
99 * GPIOs requested with this function will be automatically freed
100 * 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 */
devm_gpio_request(struct device * dev,unsigned gpio,const char * label)107 int 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 }
127 EXPORT_SYMBOL_GPL(devm_gpio_request);
128
129 /**
130 * devm_gpio_request_one - request a single GPIO with initial setup
131 * @dev: device to request for
132 * @gpio: the GPIO number
133 * @flags: GPIO configuration as specified by GPIOF_*
134 * @label: a literal description string of this GPIO
135 *
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 */
devm_gpio_request_one(struct device * dev,unsigned gpio,unsigned long flags,const char * label)141 int 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 }
162 EXPORT_SYMBOL_GPL(devm_gpio_request_one);
163