xref: /openbmc/linux/drivers/gpio/gpiolib-legacy.c (revision d82da79722400c63cc70f4c9c2493e31561ea607)
1 #include <linux/gpio/consumer.h>
2 #include <linux/gpio/driver.h>
3 
4 #include <linux/gpio.h>
5 
6 #include "gpiolib.h"
7 
8 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
9  * when setting direction, and otherwise illegal.  Until board setup code
10  * and drivers use explicit requests everywhere (which won't happen when
11  * those calls have no teeth) we can't avoid autorequesting.  This nag
12  * message should motivate switching to explicit requests... so should
13  * the weaker cleanup after faults, compared to gpio_request().
14  *
15  * NOTE: the autorequest mechanism is going away; at this point it's
16  * only "legal" in the sense that (old) code using it won't break yet,
17  * but instead only triggers a WARN() stack dump.
18  */
19 static int gpio_ensure_requested(struct gpio_desc *desc)
20 {
21 	struct gpio_chip *chip = desc->chip;
22 	unsigned long flags;
23 	bool request = false;
24 	int err = 0;
25 
26 	spin_lock_irqsave(&gpio_lock, flags);
27 
28 	if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
29 			"autorequest GPIO-%d\n", desc_to_gpio(desc))) {
30 		if (!try_module_get(chip->owner)) {
31 			gpiod_err(desc, "%s: module can't be gotten\n",
32 					__func__);
33 			clear_bit(FLAG_REQUESTED, &desc->flags);
34 			/* lose */
35 			err = -EIO;
36 			goto end;
37 		}
38 		desc->label = "[auto]";
39 		/* caller must chip->request() w/o spinlock */
40 		if (chip->request)
41 			request = true;
42 	}
43 
44 end:
45 	spin_unlock_irqrestore(&gpio_lock, flags);
46 
47 	if (request) {
48 		might_sleep_if(chip->can_sleep);
49 		err = chip->request(chip, gpio_chip_hwgpio(desc));
50 
51 		if (err < 0) {
52 			gpiod_dbg(desc, "%s: chip request fail, %d\n",
53 					__func__, err);
54 			spin_lock_irqsave(&gpio_lock, flags);
55 
56 			desc->label = NULL;
57 			clear_bit(FLAG_REQUESTED, &desc->flags);
58 
59 			spin_unlock_irqrestore(&gpio_lock, flags);
60 		}
61 	}
62 
63 	return err;
64 }
65 
66 void gpio_free(unsigned gpio)
67 {
68 	gpiod_free(gpio_to_desc(gpio));
69 }
70 EXPORT_SYMBOL_GPL(gpio_free);
71 
72 /**
73  * gpio_request_one - request a single GPIO with initial configuration
74  * @gpio:	the GPIO number
75  * @flags:	GPIO configuration as specified by GPIOF_*
76  * @label:	a literal description string of this GPIO
77  */
78 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
79 {
80 	struct gpio_desc *desc;
81 	int err;
82 
83 	desc = gpio_to_desc(gpio);
84 
85 	err = gpiod_request(desc, label);
86 	if (err)
87 		return err;
88 
89 	if (flags & GPIOF_OPEN_DRAIN)
90 		set_bit(FLAG_OPEN_DRAIN, &desc->flags);
91 
92 	if (flags & GPIOF_OPEN_SOURCE)
93 		set_bit(FLAG_OPEN_SOURCE, &desc->flags);
94 
95 	if (flags & GPIOF_ACTIVE_LOW)
96 		set_bit(FLAG_ACTIVE_LOW, &desc->flags);
97 
98 	if (flags & GPIOF_DIR_IN)
99 		err = gpiod_direction_input(desc);
100 	else
101 		err = gpiod_direction_output_raw(desc,
102 				(flags & GPIOF_INIT_HIGH) ? 1 : 0);
103 
104 	if (err)
105 		goto free_gpio;
106 
107 	if (flags & GPIOF_EXPORT) {
108 		err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
109 		if (err)
110 			goto free_gpio;
111 	}
112 
113 	return 0;
114 
115  free_gpio:
116 	gpiod_free(desc);
117 	return err;
118 }
119 EXPORT_SYMBOL_GPL(gpio_request_one);
120 
121 int gpio_request(unsigned gpio, const char *label)
122 {
123 	return gpiod_request(gpio_to_desc(gpio), label);
124 }
125 EXPORT_SYMBOL_GPL(gpio_request);
126 
127 /**
128  * gpio_request_array - request multiple GPIOs in a single call
129  * @array:	array of the 'struct gpio'
130  * @num:	how many GPIOs in the array
131  */
132 int gpio_request_array(const struct gpio *array, size_t num)
133 {
134 	int i, err;
135 
136 	for (i = 0; i < num; i++, array++) {
137 		err = gpio_request_one(array->gpio, array->flags, array->label);
138 		if (err)
139 			goto err_free;
140 	}
141 	return 0;
142 
143 err_free:
144 	while (i--)
145 		gpio_free((--array)->gpio);
146 	return err;
147 }
148 EXPORT_SYMBOL_GPL(gpio_request_array);
149 
150 /**
151  * gpio_free_array - release multiple GPIOs in a single call
152  * @array:	array of the 'struct gpio'
153  * @num:	how many GPIOs in the array
154  */
155 void gpio_free_array(const struct gpio *array, size_t num)
156 {
157 	while (num--)
158 		gpio_free((array++)->gpio);
159 }
160 EXPORT_SYMBOL_GPL(gpio_free_array);
161 
162 int gpio_direction_input(unsigned gpio)
163 {
164 	struct gpio_desc *desc = gpio_to_desc(gpio);
165 	int err;
166 
167 	if (!desc)
168 		return -EINVAL;
169 
170 	err = gpio_ensure_requested(desc);
171 	if (err < 0)
172 		return err;
173 
174 	return gpiod_direction_input(desc);
175 }
176 EXPORT_SYMBOL_GPL(gpio_direction_input);
177 
178 int gpio_direction_output(unsigned gpio, int value)
179 {
180 	struct gpio_desc *desc = gpio_to_desc(gpio);
181 	int err;
182 
183 	if (!desc)
184 		return -EINVAL;
185 
186 	err = gpio_ensure_requested(desc);
187 	if (err < 0)
188 		return err;
189 
190 	return gpiod_direction_output_raw(desc, value);
191 }
192 EXPORT_SYMBOL_GPL(gpio_direction_output);
193 
194 int gpio_set_debounce(unsigned gpio, unsigned debounce)
195 {
196 	struct gpio_desc *desc = gpio_to_desc(gpio);
197 	int err;
198 
199 	if (!desc)
200 		return -EINVAL;
201 
202 	err = gpio_ensure_requested(desc);
203 	if (err < 0)
204 		return err;
205 
206 	return gpiod_set_debounce(desc, debounce);
207 }
208 EXPORT_SYMBOL_GPL(gpio_set_debounce);
209