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