Commit | Line | Data |
---|---|---|
d2876d08 DB |
1 | #include <linux/kernel.h> |
2 | #include <linux/module.h> | |
ff77c352 | 3 | #include <linux/interrupt.h> |
d2876d08 DB |
4 | #include <linux/irq.h> |
5 | #include <linux/spinlock.h> | |
1a989d0f | 6 | #include <linux/list.h> |
d8f388d8 DB |
7 | #include <linux/device.h> |
8 | #include <linux/err.h> | |
9 | #include <linux/debugfs.h> | |
10 | #include <linux/seq_file.h> | |
11 | #include <linux/gpio.h> | |
391c970c | 12 | #include <linux/of_gpio.h> |
ff77c352 | 13 | #include <linux/idr.h> |
5a0e3ad6 | 14 | #include <linux/slab.h> |
d2876d08 | 15 | |
3f397c21 UKK |
16 | #define CREATE_TRACE_POINTS |
17 | #include <trace/events/gpio.h> | |
d2876d08 DB |
18 | |
19 | /* Optional implementation infrastructure for GPIO interfaces. | |
20 | * | |
21 | * Platforms may want to use this if they tend to use very many GPIOs | |
22 | * that aren't part of a System-On-Chip core; or across I2C/SPI/etc. | |
23 | * | |
24 | * When kernel footprint or instruction count is an issue, simpler | |
25 | * implementations may be preferred. The GPIO programming interface | |
26 | * allows for inlining speed-critical get/set operations for common | |
27 | * cases, so that access to SOC-integrated GPIOs can sometimes cost | |
28 | * only an instruction or two per bit. | |
29 | */ | |
30 | ||
31 | ||
32 | /* When debugging, extend minimal trust to callers and platform code. | |
33 | * Also emit diagnostic messages that may help initial bringup, when | |
34 | * board setup or driver bugs are most common. | |
35 | * | |
36 | * Otherwise, minimize overhead in what may be bitbanging codepaths. | |
37 | */ | |
38 | #ifdef DEBUG | |
39 | #define extra_checks 1 | |
40 | #else | |
41 | #define extra_checks 0 | |
42 | #endif | |
43 | ||
44 | /* gpio_lock prevents conflicts during gpio_desc[] table updates. | |
45 | * While any GPIO is requested, its gpio_chip is not removable; | |
46 | * each GPIO's "requested" flag serves as a lock and refcount. | |
47 | */ | |
48 | static DEFINE_SPINLOCK(gpio_lock); | |
49 | ||
50 | struct gpio_desc { | |
51 | struct gpio_chip *chip; | |
52 | unsigned long flags; | |
53 | /* flag symbols are bit numbers */ | |
54 | #define FLAG_REQUESTED 0 | |
55 | #define FLAG_IS_OUT 1 | |
710b40ea AC |
56 | #define FLAG_EXPORT 2 /* protected by sysfs_lock */ |
57 | #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */ | |
58 | #define FLAG_TRIG_FALL 4 /* trigger on falling edge */ | |
59 | #define FLAG_TRIG_RISE 5 /* trigger on rising edge */ | |
60 | #define FLAG_ACTIVE_LOW 6 /* sysfs value has active low */ | |
61 | #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ | |
62 | #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ | |
d468bf9e | 63 | #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ |
ff77c352 | 64 | |
5ba1821d | 65 | #define ID_SHIFT 16 /* add new flags before this one */ |
ff77c352 | 66 | |
5ba1821d | 67 | #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1) |
ff77c352 | 68 | #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE)) |
d2876d08 DB |
69 | |
70 | #ifdef CONFIG_DEBUG_FS | |
71 | const char *label; | |
72 | #endif | |
73 | }; | |
74 | static struct gpio_desc gpio_desc[ARCH_NR_GPIOS]; | |
75 | ||
6c0b4e6c AC |
76 | #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio) |
77 | ||
1a989d0f AC |
78 | static LIST_HEAD(gpio_chips); |
79 | ||
ff77c352 | 80 | #ifdef CONFIG_GPIO_SYSFS |
5ba1821d | 81 | static DEFINE_IDR(dirent_idr); |
ff77c352 DG |
82 | #endif |
83 | ||
372e722e AC |
84 | /* |
85 | * Internal gpiod_* API using descriptors instead of the integer namespace. | |
86 | * Most of this should eventually go public. | |
87 | */ | |
88 | static int gpiod_request(struct gpio_desc *desc, const char *label); | |
89 | static void gpiod_free(struct gpio_desc *desc); | |
90 | static int gpiod_direction_input(struct gpio_desc *desc); | |
91 | static int gpiod_direction_output(struct gpio_desc *desc, int value); | |
def63433 | 92 | static int gpiod_get_direction(const struct gpio_desc *desc); |
372e722e | 93 | static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce); |
def63433 | 94 | static int gpiod_get_value_cansleep(const struct gpio_desc *desc); |
372e722e | 95 | static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value); |
def63433 | 96 | static int gpiod_get_value(const struct gpio_desc *desc); |
372e722e | 97 | static void gpiod_set_value(struct gpio_desc *desc, int value); |
def63433 AC |
98 | static int gpiod_cansleep(const struct gpio_desc *desc); |
99 | static int gpiod_to_irq(const struct gpio_desc *desc); | |
d468bf9e LW |
100 | static int gpiod_lock_as_irq(struct gpio_desc *desc); |
101 | static void gpiod_unlock_as_irq(struct gpio_desc *desc); | |
372e722e AC |
102 | static int gpiod_export(struct gpio_desc *desc, bool direction_may_change); |
103 | static int gpiod_export_link(struct device *dev, const char *name, | |
104 | struct gpio_desc *desc); | |
105 | static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value); | |
106 | static void gpiod_unexport(struct gpio_desc *desc); | |
107 | ||
7b17b59f MB |
108 | #ifdef CONFIG_DEBUG_FS |
109 | #define gpiod_emerg(desc, fmt, ...) \ | |
110 | pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ | |
111 | ##__VA_ARGS__) | |
112 | #define gpiod_crit(desc, fmt, ...) \ | |
113 | pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ | |
114 | ##__VA_ARGS__) | |
115 | #define gpiod_err(desc, fmt, ...) \ | |
116 | pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ | |
117 | ##__VA_ARGS__) | |
118 | #define gpiod_warn(desc, fmt, ...) \ | |
119 | pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ | |
120 | ##__VA_ARGS__) | |
121 | #define gpiod_info(desc, fmt, ...) \ | |
122 | pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ | |
123 | ##__VA_ARGS__) | |
124 | #define gpiod_dbg(desc, fmt, ...) \ | |
125 | pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ | |
126 | ##__VA_ARGS__) | |
127 | #else | |
6424de5a MB |
128 | #define gpiod_emerg(desc, fmt, ...) \ |
129 | pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) | |
130 | #define gpiod_crit(desc, fmt, ...) \ | |
131 | pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) | |
132 | #define gpiod_err(desc, fmt, ...) \ | |
133 | pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) | |
134 | #define gpiod_warn(desc, fmt, ...) \ | |
135 | pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) | |
136 | #define gpiod_info(desc, fmt, ...) \ | |
137 | pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) | |
138 | #define gpiod_dbg(desc, fmt, ...) \ | |
139 | pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) | |
7b17b59f | 140 | #endif |
372e722e | 141 | |
d2876d08 DB |
142 | static inline void desc_set_label(struct gpio_desc *d, const char *label) |
143 | { | |
144 | #ifdef CONFIG_DEBUG_FS | |
145 | d->label = label; | |
146 | #endif | |
147 | } | |
148 | ||
372e722e AC |
149 | /* |
150 | * Return the GPIO number of the passed descriptor relative to its chip | |
151 | */ | |
152 | static int gpio_chip_hwgpio(const struct gpio_desc *desc) | |
153 | { | |
6c0b4e6c | 154 | return desc - &desc->chip->desc[0]; |
372e722e AC |
155 | } |
156 | ||
157 | /** | |
158 | * Convert a GPIO number to its descriptor | |
159 | */ | |
160 | static struct gpio_desc *gpio_to_desc(unsigned gpio) | |
161 | { | |
162 | if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio)) | |
163 | return NULL; | |
164 | else | |
165 | return &gpio_desc[gpio]; | |
166 | } | |
167 | ||
d468bf9e LW |
168 | /** |
169 | * Convert an offset on a certain chip to a corresponding descriptor | |
170 | */ | |
171 | static struct gpio_desc *gpiochip_offset_to_desc(struct gpio_chip *chip, | |
172 | unsigned int offset) | |
173 | { | |
174 | unsigned int gpio = chip->base + offset; | |
175 | ||
176 | return gpio_to_desc(gpio); | |
177 | } | |
178 | ||
372e722e AC |
179 | /** |
180 | * Convert a GPIO descriptor to the integer namespace. | |
181 | * This should disappear in the future but is needed since we still | |
182 | * use GPIO numbers for error messages and sysfs nodes | |
183 | */ | |
184 | static int desc_to_gpio(const struct gpio_desc *desc) | |
185 | { | |
8c0fca81 | 186 | return desc - &gpio_desc[0]; |
372e722e AC |
187 | } |
188 | ||
189 | ||
d2876d08 DB |
190 | /* Warn when drivers omit gpio_request() calls -- legal but ill-advised |
191 | * when setting direction, and otherwise illegal. Until board setup code | |
192 | * and drivers use explicit requests everywhere (which won't happen when | |
193 | * those calls have no teeth) we can't avoid autorequesting. This nag | |
35e8bb51 DB |
194 | * message should motivate switching to explicit requests... so should |
195 | * the weaker cleanup after faults, compared to gpio_request(). | |
8a0cecff DB |
196 | * |
197 | * NOTE: the autorequest mechanism is going away; at this point it's | |
198 | * only "legal" in the sense that (old) code using it won't break yet, | |
199 | * but instead only triggers a WARN() stack dump. | |
d2876d08 | 200 | */ |
372e722e | 201 | static int gpio_ensure_requested(struct gpio_desc *desc) |
d2876d08 | 202 | { |
8a0cecff | 203 | const struct gpio_chip *chip = desc->chip; |
372e722e | 204 | const int gpio = desc_to_gpio(desc); |
35e8bb51 | 205 | |
8a0cecff DB |
206 | if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0, |
207 | "autorequest GPIO-%d\n", gpio)) { | |
35e8bb51 DB |
208 | if (!try_module_get(chip->owner)) { |
209 | pr_err("GPIO-%d: module can't be gotten \n", gpio); | |
210 | clear_bit(FLAG_REQUESTED, &desc->flags); | |
211 | /* lose */ | |
212 | return -EIO; | |
213 | } | |
d2876d08 | 214 | desc_set_label(desc, "[auto]"); |
35e8bb51 DB |
215 | /* caller must chip->request() w/o spinlock */ |
216 | if (chip->request) | |
217 | return 1; | |
d2876d08 | 218 | } |
35e8bb51 | 219 | return 0; |
d2876d08 DB |
220 | } |
221 | ||
def63433 | 222 | static struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) |
372e722e | 223 | { |
bcabdef1 | 224 | return desc ? desc->chip : NULL; |
372e722e AC |
225 | } |
226 | ||
24d7628f | 227 | /* caller holds gpio_lock *OR* gpio is marked as requested */ |
1a2d397a | 228 | struct gpio_chip *gpio_to_chip(unsigned gpio) |
d2876d08 | 229 | { |
372e722e | 230 | return gpiod_to_chip(gpio_to_desc(gpio)); |
d2876d08 DB |
231 | } |
232 | ||
8d0aab2f AV |
233 | /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ |
234 | static int gpiochip_find_base(int ngpio) | |
235 | { | |
83cabe33 AC |
236 | struct gpio_chip *chip; |
237 | int base = ARCH_NR_GPIOS - ngpio; | |
8d0aab2f | 238 | |
83cabe33 AC |
239 | list_for_each_entry_reverse(chip, &gpio_chips, list) { |
240 | /* found a free space? */ | |
241 | if (chip->base + chip->ngpio <= base) | |
242 | break; | |
243 | else | |
244 | /* nope, check the space right before the chip */ | |
245 | base = chip->base - ngpio; | |
8d0aab2f AV |
246 | } |
247 | ||
83cabe33 | 248 | if (gpio_is_valid(base)) { |
8d0aab2f | 249 | pr_debug("%s: found new base at %d\n", __func__, base); |
83cabe33 AC |
250 | return base; |
251 | } else { | |
252 | pr_err("%s: cannot find free range\n", __func__); | |
253 | return -ENOSPC; | |
169b6a7a | 254 | } |
169b6a7a AV |
255 | } |
256 | ||
80b0a602 | 257 | /* caller ensures gpio is valid and requested, chip->get_direction may sleep */ |
def63433 | 258 | static int gpiod_get_direction(const struct gpio_desc *desc) |
80b0a602 MN |
259 | { |
260 | struct gpio_chip *chip; | |
372e722e | 261 | unsigned offset; |
80b0a602 MN |
262 | int status = -EINVAL; |
263 | ||
372e722e AC |
264 | chip = gpiod_to_chip(desc); |
265 | offset = gpio_chip_hwgpio(desc); | |
80b0a602 MN |
266 | |
267 | if (!chip->get_direction) | |
268 | return status; | |
269 | ||
372e722e | 270 | status = chip->get_direction(chip, offset); |
80b0a602 MN |
271 | if (status > 0) { |
272 | /* GPIOF_DIR_IN, or other positive */ | |
273 | status = 1; | |
def63433 AC |
274 | /* FLAG_IS_OUT is just a cache of the result of get_direction(), |
275 | * so it does not affect constness per se */ | |
276 | clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags); | |
80b0a602 MN |
277 | } |
278 | if (status == 0) { | |
279 | /* GPIOF_DIR_OUT */ | |
def63433 | 280 | set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags); |
80b0a602 MN |
281 | } |
282 | return status; | |
283 | } | |
284 | ||
d8f388d8 DB |
285 | #ifdef CONFIG_GPIO_SYSFS |
286 | ||
287 | /* lock protects against unexport_gpio() being called while | |
288 | * sysfs files are active. | |
289 | */ | |
290 | static DEFINE_MUTEX(sysfs_lock); | |
291 | ||
292 | /* | |
293 | * /sys/class/gpio/gpioN... only for GPIOs that are exported | |
294 | * /direction | |
295 | * * MAY BE OMITTED if kernel won't allow direction changes | |
296 | * * is read/write as "in" or "out" | |
297 | * * may also be written as "high" or "low", initializing | |
298 | * output value as specified ("out" implies "low") | |
299 | * /value | |
300 | * * always readable, subject to hardware behavior | |
301 | * * may be writable, as zero/nonzero | |
ff77c352 DG |
302 | * /edge |
303 | * * configures behavior of poll(2) on /value | |
304 | * * available only if pin can generate IRQs on input | |
305 | * * is read/write as "none", "falling", "rising", or "both" | |
07697461 JN |
306 | * /active_low |
307 | * * configures polarity of /value | |
308 | * * is read/write as zero/nonzero | |
309 | * * also affects existing and subsequent "falling" and "rising" | |
310 | * /edge configuration | |
d8f388d8 DB |
311 | */ |
312 | ||
313 | static ssize_t gpio_direction_show(struct device *dev, | |
314 | struct device_attribute *attr, char *buf) | |
315 | { | |
def63433 | 316 | const struct gpio_desc *desc = dev_get_drvdata(dev); |
d8f388d8 DB |
317 | ssize_t status; |
318 | ||
319 | mutex_lock(&sysfs_lock); | |
320 | ||
476171ce | 321 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { |
d8f388d8 | 322 | status = -EIO; |
476171ce | 323 | } else { |
372e722e | 324 | gpiod_get_direction(desc); |
d8f388d8 DB |
325 | status = sprintf(buf, "%s\n", |
326 | test_bit(FLAG_IS_OUT, &desc->flags) | |
327 | ? "out" : "in"); | |
476171ce | 328 | } |
d8f388d8 DB |
329 | |
330 | mutex_unlock(&sysfs_lock); | |
331 | return status; | |
332 | } | |
333 | ||
334 | static ssize_t gpio_direction_store(struct device *dev, | |
335 | struct device_attribute *attr, const char *buf, size_t size) | |
336 | { | |
372e722e | 337 | struct gpio_desc *desc = dev_get_drvdata(dev); |
d8f388d8 DB |
338 | ssize_t status; |
339 | ||
340 | mutex_lock(&sysfs_lock); | |
341 | ||
342 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
343 | status = -EIO; | |
344 | else if (sysfs_streq(buf, "high")) | |
372e722e | 345 | status = gpiod_direction_output(desc, 1); |
d8f388d8 | 346 | else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low")) |
372e722e | 347 | status = gpiod_direction_output(desc, 0); |
d8f388d8 | 348 | else if (sysfs_streq(buf, "in")) |
372e722e | 349 | status = gpiod_direction_input(desc); |
d8f388d8 DB |
350 | else |
351 | status = -EINVAL; | |
352 | ||
353 | mutex_unlock(&sysfs_lock); | |
354 | return status ? : size; | |
355 | } | |
356 | ||
07697461 | 357 | static /* const */ DEVICE_ATTR(direction, 0644, |
d8f388d8 DB |
358 | gpio_direction_show, gpio_direction_store); |
359 | ||
360 | static ssize_t gpio_value_show(struct device *dev, | |
361 | struct device_attribute *attr, char *buf) | |
362 | { | |
372e722e | 363 | struct gpio_desc *desc = dev_get_drvdata(dev); |
d8f388d8 DB |
364 | ssize_t status; |
365 | ||
366 | mutex_lock(&sysfs_lock); | |
367 | ||
07697461 | 368 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { |
d8f388d8 | 369 | status = -EIO; |
07697461 JN |
370 | } else { |
371 | int value; | |
372 | ||
372e722e | 373 | value = !!gpiod_get_value_cansleep(desc); |
07697461 JN |
374 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
375 | value = !value; | |
376 | ||
377 | status = sprintf(buf, "%d\n", value); | |
378 | } | |
d8f388d8 DB |
379 | |
380 | mutex_unlock(&sysfs_lock); | |
381 | return status; | |
382 | } | |
383 | ||
384 | static ssize_t gpio_value_store(struct device *dev, | |
385 | struct device_attribute *attr, const char *buf, size_t size) | |
386 | { | |
372e722e | 387 | struct gpio_desc *desc = dev_get_drvdata(dev); |
d8f388d8 DB |
388 | ssize_t status; |
389 | ||
390 | mutex_lock(&sysfs_lock); | |
391 | ||
392 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
393 | status = -EIO; | |
394 | else if (!test_bit(FLAG_IS_OUT, &desc->flags)) | |
395 | status = -EPERM; | |
396 | else { | |
397 | long value; | |
398 | ||
a3d88c92 | 399 | status = kstrtol(buf, 0, &value); |
d8f388d8 | 400 | if (status == 0) { |
07697461 JN |
401 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
402 | value = !value; | |
372e722e | 403 | gpiod_set_value_cansleep(desc, value != 0); |
d8f388d8 DB |
404 | status = size; |
405 | } | |
406 | } | |
407 | ||
408 | mutex_unlock(&sysfs_lock); | |
409 | return status; | |
410 | } | |
411 | ||
07697461 | 412 | static const DEVICE_ATTR(value, 0644, |
d8f388d8 DB |
413 | gpio_value_show, gpio_value_store); |
414 | ||
ff77c352 DG |
415 | static irqreturn_t gpio_sysfs_irq(int irq, void *priv) |
416 | { | |
5ba1821d | 417 | struct sysfs_dirent *value_sd = priv; |
ff77c352 | 418 | |
5ba1821d | 419 | sysfs_notify_dirent(value_sd); |
ff77c352 DG |
420 | return IRQ_HANDLED; |
421 | } | |
422 | ||
ff77c352 DG |
423 | static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev, |
424 | unsigned long gpio_flags) | |
425 | { | |
5ba1821d | 426 | struct sysfs_dirent *value_sd; |
ff77c352 DG |
427 | unsigned long irq_flags; |
428 | int ret, irq, id; | |
429 | ||
430 | if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags) | |
431 | return 0; | |
432 | ||
372e722e | 433 | irq = gpiod_to_irq(desc); |
ff77c352 DG |
434 | if (irq < 0) |
435 | return -EIO; | |
436 | ||
5ba1821d DG |
437 | id = desc->flags >> ID_SHIFT; |
438 | value_sd = idr_find(&dirent_idr, id); | |
439 | if (value_sd) | |
440 | free_irq(irq, value_sd); | |
ff77c352 DG |
441 | |
442 | desc->flags &= ~GPIO_TRIGGER_MASK; | |
443 | ||
444 | if (!gpio_flags) { | |
d468bf9e | 445 | gpiod_unlock_as_irq(desc); |
ff77c352 | 446 | ret = 0; |
5ba1821d | 447 | goto free_id; |
ff77c352 DG |
448 | } |
449 | ||
450 | irq_flags = IRQF_SHARED; | |
451 | if (test_bit(FLAG_TRIG_FALL, &gpio_flags)) | |
07697461 JN |
452 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
453 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; | |
ff77c352 | 454 | if (test_bit(FLAG_TRIG_RISE, &gpio_flags)) |
07697461 JN |
455 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
456 | IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; | |
ff77c352 | 457 | |
5ba1821d DG |
458 | if (!value_sd) { |
459 | value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value"); | |
460 | if (!value_sd) { | |
461 | ret = -ENODEV; | |
ff77c352 DG |
462 | goto err_out; |
463 | } | |
464 | ||
62f516b8 TH |
465 | ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL); |
466 | if (ret < 0) | |
5ba1821d | 467 | goto free_sd; |
62f516b8 | 468 | id = ret; |
ff77c352 DG |
469 | |
470 | desc->flags &= GPIO_FLAGS_MASK; | |
5ba1821d | 471 | desc->flags |= (unsigned long)id << ID_SHIFT; |
ff77c352 | 472 | |
5ba1821d | 473 | if (desc->flags >> ID_SHIFT != id) { |
ff77c352 DG |
474 | ret = -ERANGE; |
475 | goto free_id; | |
476 | } | |
ff77c352 DG |
477 | } |
478 | ||
364fadb3 | 479 | ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags, |
5ba1821d | 480 | "gpiolib", value_sd); |
364fadb3 | 481 | if (ret < 0) |
5ba1821d | 482 | goto free_id; |
ff77c352 | 483 | |
d468bf9e LW |
484 | ret = gpiod_lock_as_irq(desc); |
485 | if (ret < 0) { | |
486 | gpiod_warn(desc, "failed to flag the GPIO for IRQ\n"); | |
487 | goto free_id; | |
488 | } | |
489 | ||
ff77c352 DG |
490 | desc->flags |= gpio_flags; |
491 | return 0; | |
492 | ||
ff77c352 | 493 | free_id: |
5ba1821d | 494 | idr_remove(&dirent_idr, id); |
ff77c352 | 495 | desc->flags &= GPIO_FLAGS_MASK; |
5ba1821d DG |
496 | free_sd: |
497 | if (value_sd) | |
498 | sysfs_put(value_sd); | |
ff77c352 DG |
499 | err_out: |
500 | return ret; | |
501 | } | |
502 | ||
503 | static const struct { | |
504 | const char *name; | |
505 | unsigned long flags; | |
506 | } trigger_types[] = { | |
507 | { "none", 0 }, | |
508 | { "falling", BIT(FLAG_TRIG_FALL) }, | |
509 | { "rising", BIT(FLAG_TRIG_RISE) }, | |
510 | { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) }, | |
511 | }; | |
512 | ||
513 | static ssize_t gpio_edge_show(struct device *dev, | |
514 | struct device_attribute *attr, char *buf) | |
515 | { | |
516 | const struct gpio_desc *desc = dev_get_drvdata(dev); | |
517 | ssize_t status; | |
518 | ||
519 | mutex_lock(&sysfs_lock); | |
520 | ||
521 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
522 | status = -EIO; | |
523 | else { | |
524 | int i; | |
525 | ||
526 | status = 0; | |
527 | for (i = 0; i < ARRAY_SIZE(trigger_types); i++) | |
528 | if ((desc->flags & GPIO_TRIGGER_MASK) | |
529 | == trigger_types[i].flags) { | |
530 | status = sprintf(buf, "%s\n", | |
531 | trigger_types[i].name); | |
532 | break; | |
533 | } | |
534 | } | |
535 | ||
536 | mutex_unlock(&sysfs_lock); | |
537 | return status; | |
538 | } | |
539 | ||
540 | static ssize_t gpio_edge_store(struct device *dev, | |
541 | struct device_attribute *attr, const char *buf, size_t size) | |
542 | { | |
543 | struct gpio_desc *desc = dev_get_drvdata(dev); | |
544 | ssize_t status; | |
545 | int i; | |
546 | ||
547 | for (i = 0; i < ARRAY_SIZE(trigger_types); i++) | |
548 | if (sysfs_streq(trigger_types[i].name, buf)) | |
549 | goto found; | |
550 | return -EINVAL; | |
551 | ||
552 | found: | |
553 | mutex_lock(&sysfs_lock); | |
554 | ||
555 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
556 | status = -EIO; | |
557 | else { | |
558 | status = gpio_setup_irq(desc, dev, trigger_types[i].flags); | |
559 | if (!status) | |
560 | status = size; | |
561 | } | |
562 | ||
563 | mutex_unlock(&sysfs_lock); | |
564 | ||
565 | return status; | |
566 | } | |
567 | ||
568 | static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store); | |
569 | ||
07697461 JN |
570 | static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev, |
571 | int value) | |
572 | { | |
573 | int status = 0; | |
574 | ||
575 | if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value) | |
576 | return 0; | |
577 | ||
578 | if (value) | |
579 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); | |
580 | else | |
581 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); | |
582 | ||
583 | /* reconfigure poll(2) support if enabled on one edge only */ | |
584 | if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^ | |
585 | !!test_bit(FLAG_TRIG_FALL, &desc->flags))) { | |
586 | unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK; | |
587 | ||
588 | gpio_setup_irq(desc, dev, 0); | |
589 | status = gpio_setup_irq(desc, dev, trigger_flags); | |
590 | } | |
591 | ||
592 | return status; | |
593 | } | |
594 | ||
595 | static ssize_t gpio_active_low_show(struct device *dev, | |
596 | struct device_attribute *attr, char *buf) | |
597 | { | |
598 | const struct gpio_desc *desc = dev_get_drvdata(dev); | |
599 | ssize_t status; | |
600 | ||
601 | mutex_lock(&sysfs_lock); | |
602 | ||
603 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
604 | status = -EIO; | |
605 | else | |
606 | status = sprintf(buf, "%d\n", | |
607 | !!test_bit(FLAG_ACTIVE_LOW, &desc->flags)); | |
608 | ||
609 | mutex_unlock(&sysfs_lock); | |
610 | ||
611 | return status; | |
612 | } | |
613 | ||
614 | static ssize_t gpio_active_low_store(struct device *dev, | |
615 | struct device_attribute *attr, const char *buf, size_t size) | |
616 | { | |
617 | struct gpio_desc *desc = dev_get_drvdata(dev); | |
618 | ssize_t status; | |
619 | ||
620 | mutex_lock(&sysfs_lock); | |
621 | ||
622 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { | |
623 | status = -EIO; | |
624 | } else { | |
625 | long value; | |
626 | ||
a3d88c92 | 627 | status = kstrtol(buf, 0, &value); |
07697461 JN |
628 | if (status == 0) |
629 | status = sysfs_set_active_low(desc, dev, value != 0); | |
630 | } | |
631 | ||
632 | mutex_unlock(&sysfs_lock); | |
633 | ||
634 | return status ? : size; | |
635 | } | |
636 | ||
637 | static const DEVICE_ATTR(active_low, 0644, | |
638 | gpio_active_low_show, gpio_active_low_store); | |
639 | ||
d8f388d8 | 640 | static const struct attribute *gpio_attrs[] = { |
d8f388d8 | 641 | &dev_attr_value.attr, |
07697461 | 642 | &dev_attr_active_low.attr, |
d8f388d8 DB |
643 | NULL, |
644 | }; | |
645 | ||
646 | static const struct attribute_group gpio_attr_group = { | |
647 | .attrs = (struct attribute **) gpio_attrs, | |
648 | }; | |
649 | ||
650 | /* | |
651 | * /sys/class/gpio/gpiochipN/ | |
652 | * /base ... matching gpio_chip.base (N) | |
653 | * /label ... matching gpio_chip.label | |
654 | * /ngpio ... matching gpio_chip.ngpio | |
655 | */ | |
656 | ||
657 | static ssize_t chip_base_show(struct device *dev, | |
658 | struct device_attribute *attr, char *buf) | |
659 | { | |
660 | const struct gpio_chip *chip = dev_get_drvdata(dev); | |
661 | ||
662 | return sprintf(buf, "%d\n", chip->base); | |
663 | } | |
664 | static DEVICE_ATTR(base, 0444, chip_base_show, NULL); | |
665 | ||
666 | static ssize_t chip_label_show(struct device *dev, | |
667 | struct device_attribute *attr, char *buf) | |
668 | { | |
669 | const struct gpio_chip *chip = dev_get_drvdata(dev); | |
670 | ||
671 | return sprintf(buf, "%s\n", chip->label ? : ""); | |
672 | } | |
673 | static DEVICE_ATTR(label, 0444, chip_label_show, NULL); | |
674 | ||
675 | static ssize_t chip_ngpio_show(struct device *dev, | |
676 | struct device_attribute *attr, char *buf) | |
677 | { | |
678 | const struct gpio_chip *chip = dev_get_drvdata(dev); | |
679 | ||
680 | return sprintf(buf, "%u\n", chip->ngpio); | |
681 | } | |
682 | static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL); | |
683 | ||
684 | static const struct attribute *gpiochip_attrs[] = { | |
685 | &dev_attr_base.attr, | |
686 | &dev_attr_label.attr, | |
687 | &dev_attr_ngpio.attr, | |
688 | NULL, | |
689 | }; | |
690 | ||
691 | static const struct attribute_group gpiochip_attr_group = { | |
692 | .attrs = (struct attribute **) gpiochip_attrs, | |
693 | }; | |
694 | ||
695 | /* | |
696 | * /sys/class/gpio/export ... write-only | |
697 | * integer N ... number of GPIO to export (full access) | |
698 | * /sys/class/gpio/unexport ... write-only | |
699 | * integer N ... number of GPIO to unexport | |
700 | */ | |
28812fe1 AK |
701 | static ssize_t export_store(struct class *class, |
702 | struct class_attribute *attr, | |
703 | const char *buf, size_t len) | |
d8f388d8 | 704 | { |
372e722e AC |
705 | long gpio; |
706 | struct gpio_desc *desc; | |
707 | int status; | |
d8f388d8 | 708 | |
a3d88c92 | 709 | status = kstrtol(buf, 0, &gpio); |
d8f388d8 DB |
710 | if (status < 0) |
711 | goto done; | |
712 | ||
372e722e | 713 | desc = gpio_to_desc(gpio); |
bcabdef1 AC |
714 | /* reject invalid GPIOs */ |
715 | if (!desc) { | |
716 | pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); | |
717 | return -EINVAL; | |
718 | } | |
372e722e | 719 | |
d8f388d8 DB |
720 | /* No extra locking here; FLAG_SYSFS just signifies that the |
721 | * request and export were done by on behalf of userspace, so | |
722 | * they may be undone on its behalf too. | |
723 | */ | |
724 | ||
372e722e | 725 | status = gpiod_request(desc, "sysfs"); |
ad2fab36 MN |
726 | if (status < 0) { |
727 | if (status == -EPROBE_DEFER) | |
728 | status = -ENODEV; | |
d8f388d8 | 729 | goto done; |
ad2fab36 | 730 | } |
372e722e | 731 | status = gpiod_export(desc, true); |
d8f388d8 | 732 | if (status < 0) |
372e722e | 733 | gpiod_free(desc); |
d8f388d8 | 734 | else |
372e722e | 735 | set_bit(FLAG_SYSFS, &desc->flags); |
d8f388d8 DB |
736 | |
737 | done: | |
738 | if (status) | |
739 | pr_debug("%s: status %d\n", __func__, status); | |
740 | return status ? : len; | |
741 | } | |
742 | ||
28812fe1 AK |
743 | static ssize_t unexport_store(struct class *class, |
744 | struct class_attribute *attr, | |
745 | const char *buf, size_t len) | |
d8f388d8 | 746 | { |
372e722e AC |
747 | long gpio; |
748 | struct gpio_desc *desc; | |
749 | int status; | |
d8f388d8 | 750 | |
a3d88c92 | 751 | status = kstrtol(buf, 0, &gpio); |
d8f388d8 DB |
752 | if (status < 0) |
753 | goto done; | |
754 | ||
372e722e | 755 | desc = gpio_to_desc(gpio); |
d8f388d8 | 756 | /* reject bogus commands (gpio_unexport ignores them) */ |
bcabdef1 AC |
757 | if (!desc) { |
758 | pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); | |
759 | return -EINVAL; | |
760 | } | |
761 | ||
762 | status = -EINVAL; | |
d8f388d8 DB |
763 | |
764 | /* No extra locking here; FLAG_SYSFS just signifies that the | |
765 | * request and export were done by on behalf of userspace, so | |
766 | * they may be undone on its behalf too. | |
767 | */ | |
372e722e | 768 | if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) { |
d8f388d8 | 769 | status = 0; |
372e722e | 770 | gpiod_free(desc); |
d8f388d8 DB |
771 | } |
772 | done: | |
773 | if (status) | |
774 | pr_debug("%s: status %d\n", __func__, status); | |
775 | return status ? : len; | |
776 | } | |
777 | ||
778 | static struct class_attribute gpio_class_attrs[] = { | |
779 | __ATTR(export, 0200, NULL, export_store), | |
780 | __ATTR(unexport, 0200, NULL, unexport_store), | |
781 | __ATTR_NULL, | |
782 | }; | |
783 | ||
784 | static struct class gpio_class = { | |
785 | .name = "gpio", | |
786 | .owner = THIS_MODULE, | |
787 | ||
788 | .class_attrs = gpio_class_attrs, | |
789 | }; | |
790 | ||
791 | ||
792 | /** | |
793 | * gpio_export - export a GPIO through sysfs | |
794 | * @gpio: gpio to make available, already requested | |
795 | * @direction_may_change: true if userspace may change gpio direction | |
796 | * Context: arch_initcall or later | |
797 | * | |
798 | * When drivers want to make a GPIO accessible to userspace after they | |
799 | * have requested it -- perhaps while debugging, or as part of their | |
800 | * public interface -- they may use this routine. If the GPIO can | |
801 | * change direction (some can't) and the caller allows it, userspace | |
802 | * will see "direction" sysfs attribute which may be used to change | |
803 | * the gpio's direction. A "value" attribute will always be provided. | |
804 | * | |
805 | * Returns zero on success, else an error. | |
806 | */ | |
372e722e | 807 | static int gpiod_export(struct gpio_desc *desc, bool direction_may_change) |
d8f388d8 DB |
808 | { |
809 | unsigned long flags; | |
fc4e2514 | 810 | int status; |
62154991 | 811 | const char *ioname = NULL; |
fc4e2514 | 812 | struct device *dev; |
372e722e | 813 | int offset; |
d8f388d8 DB |
814 | |
815 | /* can't export until sysfs is available ... */ | |
816 | if (!gpio_class.p) { | |
817 | pr_debug("%s: called too early!\n", __func__); | |
818 | return -ENOENT; | |
819 | } | |
820 | ||
372e722e AC |
821 | if (!desc) { |
822 | pr_debug("%s: invalid gpio descriptor\n", __func__); | |
fc4e2514 RM |
823 | return -EINVAL; |
824 | } | |
d8f388d8 DB |
825 | |
826 | mutex_lock(&sysfs_lock); | |
827 | ||
828 | spin_lock_irqsave(&gpio_lock, flags); | |
fc4e2514 RM |
829 | if (!test_bit(FLAG_REQUESTED, &desc->flags) || |
830 | test_bit(FLAG_EXPORT, &desc->flags)) { | |
831 | spin_unlock_irqrestore(&gpio_lock, flags); | |
832 | pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n", | |
372e722e | 833 | __func__, desc_to_gpio(desc), |
fc4e2514 RM |
834 | test_bit(FLAG_REQUESTED, &desc->flags), |
835 | test_bit(FLAG_EXPORT, &desc->flags)); | |
529f2ad5 DC |
836 | status = -EPERM; |
837 | goto fail_unlock; | |
d8f388d8 | 838 | } |
fc4e2514 RM |
839 | |
840 | if (!desc->chip->direction_input || !desc->chip->direction_output) | |
841 | direction_may_change = false; | |
d8f388d8 DB |
842 | spin_unlock_irqrestore(&gpio_lock, flags); |
843 | ||
372e722e AC |
844 | offset = gpio_chip_hwgpio(desc); |
845 | if (desc->chip->names && desc->chip->names[offset]) | |
846 | ioname = desc->chip->names[offset]; | |
926b663c | 847 | |
fc4e2514 | 848 | dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0), |
372e722e AC |
849 | desc, ioname ? ioname : "gpio%u", |
850 | desc_to_gpio(desc)); | |
fc4e2514 RM |
851 | if (IS_ERR(dev)) { |
852 | status = PTR_ERR(dev); | |
853 | goto fail_unlock; | |
d8f388d8 DB |
854 | } |
855 | ||
fc4e2514 | 856 | status = sysfs_create_group(&dev->kobj, &gpio_attr_group); |
d8f388d8 | 857 | if (status) |
fc4e2514 | 858 | goto fail_unregister_device; |
d8f388d8 | 859 | |
fc4e2514 RM |
860 | if (direction_may_change) { |
861 | status = device_create_file(dev, &dev_attr_direction); | |
862 | if (status) | |
863 | goto fail_unregister_device; | |
864 | } | |
865 | ||
372e722e | 866 | if (gpiod_to_irq(desc) >= 0 && (direction_may_change || |
fc4e2514 RM |
867 | !test_bit(FLAG_IS_OUT, &desc->flags))) { |
868 | status = device_create_file(dev, &dev_attr_edge); | |
869 | if (status) | |
870 | goto fail_unregister_device; | |
871 | } | |
d8f388d8 | 872 | |
fc4e2514 RM |
873 | set_bit(FLAG_EXPORT, &desc->flags); |
874 | mutex_unlock(&sysfs_lock); | |
875 | return 0; | |
876 | ||
877 | fail_unregister_device: | |
878 | device_unregister(dev); | |
879 | fail_unlock: | |
880 | mutex_unlock(&sysfs_lock); | |
372e722e AC |
881 | pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), |
882 | status); | |
d8f388d8 DB |
883 | return status; |
884 | } | |
372e722e AC |
885 | |
886 | int gpio_export(unsigned gpio, bool direction_may_change) | |
887 | { | |
888 | return gpiod_export(gpio_to_desc(gpio), direction_may_change); | |
889 | } | |
d8f388d8 DB |
890 | EXPORT_SYMBOL_GPL(gpio_export); |
891 | ||
9f3b795a | 892 | static int match_export(struct device *dev, const void *data) |
d8f388d8 DB |
893 | { |
894 | return dev_get_drvdata(dev) == data; | |
895 | } | |
896 | ||
a4177ee7 JN |
897 | /** |
898 | * gpio_export_link - create a sysfs link to an exported GPIO node | |
899 | * @dev: device under which to create symlink | |
900 | * @name: name of the symlink | |
901 | * @gpio: gpio to create symlink to, already exported | |
902 | * | |
903 | * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN | |
904 | * node. Caller is responsible for unlinking. | |
905 | * | |
906 | * Returns zero on success, else an error. | |
907 | */ | |
372e722e AC |
908 | static int gpiod_export_link(struct device *dev, const char *name, |
909 | struct gpio_desc *desc) | |
a4177ee7 | 910 | { |
a4177ee7 JN |
911 | int status = -EINVAL; |
912 | ||
bcabdef1 AC |
913 | if (!desc) { |
914 | pr_warn("%s: invalid GPIO\n", __func__); | |
915 | return -EINVAL; | |
916 | } | |
a4177ee7 JN |
917 | |
918 | mutex_lock(&sysfs_lock); | |
919 | ||
a4177ee7 JN |
920 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
921 | struct device *tdev; | |
922 | ||
923 | tdev = class_find_device(&gpio_class, NULL, desc, match_export); | |
924 | if (tdev != NULL) { | |
925 | status = sysfs_create_link(&dev->kobj, &tdev->kobj, | |
926 | name); | |
927 | } else { | |
928 | status = -ENODEV; | |
929 | } | |
930 | } | |
931 | ||
932 | mutex_unlock(&sysfs_lock); | |
933 | ||
a4177ee7 | 934 | if (status) |
372e722e AC |
935 | pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), |
936 | status); | |
a4177ee7 JN |
937 | |
938 | return status; | |
939 | } | |
a4177ee7 | 940 | |
372e722e AC |
941 | int gpio_export_link(struct device *dev, const char *name, unsigned gpio) |
942 | { | |
943 | return gpiod_export_link(dev, name, gpio_to_desc(gpio)); | |
944 | } | |
945 | EXPORT_SYMBOL_GPL(gpio_export_link); | |
07697461 JN |
946 | |
947 | /** | |
948 | * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value | |
949 | * @gpio: gpio to change | |
950 | * @value: non-zero to use active low, i.e. inverted values | |
951 | * | |
952 | * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute. | |
953 | * The GPIO does not have to be exported yet. If poll(2) support has | |
954 | * been enabled for either rising or falling edge, it will be | |
955 | * reconfigured to follow the new polarity. | |
956 | * | |
957 | * Returns zero on success, else an error. | |
958 | */ | |
372e722e | 959 | static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) |
07697461 | 960 | { |
07697461 JN |
961 | struct device *dev = NULL; |
962 | int status = -EINVAL; | |
963 | ||
bcabdef1 AC |
964 | if (!desc) { |
965 | pr_warn("%s: invalid GPIO\n", __func__); | |
966 | return -EINVAL; | |
967 | } | |
07697461 JN |
968 | |
969 | mutex_lock(&sysfs_lock); | |
970 | ||
07697461 | 971 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
07697461 JN |
972 | dev = class_find_device(&gpio_class, NULL, desc, match_export); |
973 | if (dev == NULL) { | |
974 | status = -ENODEV; | |
975 | goto unlock; | |
976 | } | |
977 | } | |
978 | ||
979 | status = sysfs_set_active_low(desc, dev, value); | |
980 | ||
981 | unlock: | |
982 | mutex_unlock(&sysfs_lock); | |
983 | ||
07697461 | 984 | if (status) |
372e722e AC |
985 | pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), |
986 | status); | |
07697461 JN |
987 | |
988 | return status; | |
989 | } | |
372e722e AC |
990 | |
991 | int gpio_sysfs_set_active_low(unsigned gpio, int value) | |
992 | { | |
993 | return gpiod_sysfs_set_active_low(gpio_to_desc(gpio), value); | |
994 | } | |
07697461 JN |
995 | EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low); |
996 | ||
d8f388d8 DB |
997 | /** |
998 | * gpio_unexport - reverse effect of gpio_export() | |
999 | * @gpio: gpio to make unavailable | |
1000 | * | |
1001 | * This is implicit on gpio_free(). | |
1002 | */ | |
372e722e | 1003 | static void gpiod_unexport(struct gpio_desc *desc) |
d8f388d8 | 1004 | { |
6a99ad4a | 1005 | int status = 0; |
864533ce | 1006 | struct device *dev = NULL; |
d8f388d8 | 1007 | |
372e722e | 1008 | if (!desc) { |
bcabdef1 AC |
1009 | pr_warn("%s: invalid GPIO\n", __func__); |
1010 | return; | |
6a99ad4a | 1011 | } |
d8f388d8 DB |
1012 | |
1013 | mutex_lock(&sysfs_lock); | |
1014 | ||
d8f388d8 | 1015 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
d8f388d8 DB |
1016 | |
1017 | dev = class_find_device(&gpio_class, NULL, desc, match_export); | |
1018 | if (dev) { | |
ff77c352 | 1019 | gpio_setup_irq(desc, dev, 0); |
d8f388d8 | 1020 | clear_bit(FLAG_EXPORT, &desc->flags); |
d8f388d8 DB |
1021 | } else |
1022 | status = -ENODEV; | |
1023 | } | |
1024 | ||
1025 | mutex_unlock(&sysfs_lock); | |
372e722e | 1026 | |
864533ce ML |
1027 | if (dev) { |
1028 | device_unregister(dev); | |
1029 | put_device(dev); | |
1030 | } | |
bcabdef1 | 1031 | |
d8f388d8 | 1032 | if (status) |
372e722e AC |
1033 | pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), |
1034 | status); | |
1035 | } | |
1036 | ||
1037 | void gpio_unexport(unsigned gpio) | |
1038 | { | |
1039 | gpiod_unexport(gpio_to_desc(gpio)); | |
d8f388d8 DB |
1040 | } |
1041 | EXPORT_SYMBOL_GPL(gpio_unexport); | |
1042 | ||
1043 | static int gpiochip_export(struct gpio_chip *chip) | |
1044 | { | |
1045 | int status; | |
1046 | struct device *dev; | |
1047 | ||
1048 | /* Many systems register gpio chips for SOC support very early, | |
1049 | * before driver model support is available. In those cases we | |
1050 | * export this later, in gpiolib_sysfs_init() ... here we just | |
1051 | * verify that _some_ field of gpio_class got initialized. | |
1052 | */ | |
1053 | if (!gpio_class.p) | |
1054 | return 0; | |
1055 | ||
1056 | /* use chip->base for the ID; it's already known to be unique */ | |
1057 | mutex_lock(&sysfs_lock); | |
1058 | dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip, | |
1059 | "gpiochip%d", chip->base); | |
d62668e1 | 1060 | if (!IS_ERR(dev)) { |
d8f388d8 DB |
1061 | status = sysfs_create_group(&dev->kobj, |
1062 | &gpiochip_attr_group); | |
1063 | } else | |
d62668e1 | 1064 | status = PTR_ERR(dev); |
d8f388d8 DB |
1065 | chip->exported = (status == 0); |
1066 | mutex_unlock(&sysfs_lock); | |
1067 | ||
1068 | if (status) { | |
1069 | unsigned long flags; | |
1070 | unsigned gpio; | |
1071 | ||
1072 | spin_lock_irqsave(&gpio_lock, flags); | |
6c0b4e6c AC |
1073 | gpio = 0; |
1074 | while (gpio < chip->ngpio) | |
1075 | chip->desc[gpio++].chip = NULL; | |
d8f388d8 DB |
1076 | spin_unlock_irqrestore(&gpio_lock, flags); |
1077 | ||
1078 | pr_debug("%s: chip %s status %d\n", __func__, | |
1079 | chip->label, status); | |
1080 | } | |
1081 | ||
1082 | return status; | |
1083 | } | |
1084 | ||
1085 | static void gpiochip_unexport(struct gpio_chip *chip) | |
1086 | { | |
1087 | int status; | |
1088 | struct device *dev; | |
1089 | ||
1090 | mutex_lock(&sysfs_lock); | |
1091 | dev = class_find_device(&gpio_class, NULL, chip, match_export); | |
1092 | if (dev) { | |
1093 | put_device(dev); | |
1094 | device_unregister(dev); | |
1095 | chip->exported = 0; | |
1096 | status = 0; | |
1097 | } else | |
1098 | status = -ENODEV; | |
1099 | mutex_unlock(&sysfs_lock); | |
1100 | ||
1101 | if (status) | |
1102 | pr_debug("%s: chip %s status %d\n", __func__, | |
1103 | chip->label, status); | |
1104 | } | |
1105 | ||
1106 | static int __init gpiolib_sysfs_init(void) | |
1107 | { | |
1108 | int status; | |
1109 | unsigned long flags; | |
65493e3a | 1110 | struct gpio_chip *chip; |
d8f388d8 DB |
1111 | |
1112 | status = class_register(&gpio_class); | |
1113 | if (status < 0) | |
1114 | return status; | |
1115 | ||
1116 | /* Scan and register the gpio_chips which registered very | |
1117 | * early (e.g. before the class_register above was called). | |
1118 | * | |
1119 | * We run before arch_initcall() so chip->dev nodes can have | |
1120 | * registered, and so arch_initcall() can always gpio_export(). | |
1121 | */ | |
1122 | spin_lock_irqsave(&gpio_lock, flags); | |
65493e3a | 1123 | list_for_each_entry(chip, &gpio_chips, list) { |
d8f388d8 DB |
1124 | if (!chip || chip->exported) |
1125 | continue; | |
1126 | ||
1127 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1128 | status = gpiochip_export(chip); | |
1129 | spin_lock_irqsave(&gpio_lock, flags); | |
1130 | } | |
1131 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1132 | ||
1133 | ||
1134 | return status; | |
1135 | } | |
1136 | postcore_initcall(gpiolib_sysfs_init); | |
1137 | ||
1138 | #else | |
1139 | static inline int gpiochip_export(struct gpio_chip *chip) | |
1140 | { | |
1141 | return 0; | |
1142 | } | |
1143 | ||
1144 | static inline void gpiochip_unexport(struct gpio_chip *chip) | |
1145 | { | |
1146 | } | |
1147 | ||
372e722e AC |
1148 | static inline int gpiod_export(struct gpio_desc *desc, |
1149 | bool direction_may_change) | |
1150 | { | |
1151 | return -ENOSYS; | |
1152 | } | |
1153 | ||
1154 | static inline int gpiod_export_link(struct device *dev, const char *name, | |
1155 | struct gpio_desc *desc) | |
1156 | { | |
1157 | return -ENOSYS; | |
1158 | } | |
1159 | ||
1160 | static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) | |
1161 | { | |
1162 | return -ENOSYS; | |
1163 | } | |
1164 | ||
1165 | static inline void gpiod_unexport(struct gpio_desc *desc) | |
1166 | { | |
1167 | } | |
1168 | ||
d8f388d8 DB |
1169 | #endif /* CONFIG_GPIO_SYSFS */ |
1170 | ||
1a989d0f AC |
1171 | /* |
1172 | * Add a new chip to the global chips list, keeping the list of chips sorted | |
1173 | * by base order. | |
1174 | * | |
1175 | * Return -EBUSY if the new chip overlaps with some other chip's integer | |
1176 | * space. | |
1177 | */ | |
1178 | static int gpiochip_add_to_list(struct gpio_chip *chip) | |
1179 | { | |
1180 | struct list_head *pos = &gpio_chips; | |
1181 | struct gpio_chip *_chip; | |
1182 | int err = 0; | |
1183 | ||
1184 | /* find where to insert our chip */ | |
1185 | list_for_each(pos, &gpio_chips) { | |
1186 | _chip = list_entry(pos, struct gpio_chip, list); | |
1187 | /* shall we insert before _chip? */ | |
1188 | if (_chip->base >= chip->base + chip->ngpio) | |
1189 | break; | |
1190 | } | |
1191 | ||
1192 | /* are we stepping on the chip right before? */ | |
1193 | if (pos != &gpio_chips && pos->prev != &gpio_chips) { | |
1194 | _chip = list_entry(pos->prev, struct gpio_chip, list); | |
1195 | if (_chip->base + _chip->ngpio > chip->base) { | |
1196 | dev_err(chip->dev, | |
1197 | "GPIO integer space overlap, cannot add chip\n"); | |
1198 | err = -EBUSY; | |
1199 | } | |
1200 | } | |
1201 | ||
1202 | if (!err) | |
1203 | list_add_tail(&chip->list, pos); | |
1204 | ||
1205 | return err; | |
1206 | } | |
1207 | ||
d2876d08 DB |
1208 | /** |
1209 | * gpiochip_add() - register a gpio_chip | |
1210 | * @chip: the chip to register, with chip->base initialized | |
1211 | * Context: potentially before irqs or kmalloc will work | |
1212 | * | |
1213 | * Returns a negative errno if the chip can't be registered, such as | |
1214 | * because the chip->base is invalid or already associated with a | |
1215 | * different chip. Otherwise it returns zero as a success code. | |
8d0aab2f | 1216 | * |
d8f388d8 DB |
1217 | * When gpiochip_add() is called very early during boot, so that GPIOs |
1218 | * can be freely used, the chip->dev device must be registered before | |
1219 | * the gpio framework's arch_initcall(). Otherwise sysfs initialization | |
1220 | * for GPIOs will fail rudely. | |
1221 | * | |
8d0aab2f AV |
1222 | * If chip->base is negative, this requests dynamic assignment of |
1223 | * a range of valid GPIOs. | |
d2876d08 DB |
1224 | */ |
1225 | int gpiochip_add(struct gpio_chip *chip) | |
1226 | { | |
1227 | unsigned long flags; | |
1228 | int status = 0; | |
1229 | unsigned id; | |
8d0aab2f | 1230 | int base = chip->base; |
d2876d08 | 1231 | |
bff5fda9 | 1232 | if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1)) |
8d0aab2f | 1233 | && base >= 0) { |
d2876d08 DB |
1234 | status = -EINVAL; |
1235 | goto fail; | |
1236 | } | |
1237 | ||
1238 | spin_lock_irqsave(&gpio_lock, flags); | |
1239 | ||
8d0aab2f AV |
1240 | if (base < 0) { |
1241 | base = gpiochip_find_base(chip->ngpio); | |
1242 | if (base < 0) { | |
1243 | status = base; | |
d8f388d8 | 1244 | goto unlock; |
8d0aab2f AV |
1245 | } |
1246 | chip->base = base; | |
1247 | } | |
1248 | ||
1a989d0f AC |
1249 | status = gpiochip_add_to_list(chip); |
1250 | ||
d2876d08 | 1251 | if (status == 0) { |
6c0b4e6c AC |
1252 | chip->desc = &gpio_desc[chip->base]; |
1253 | ||
1254 | for (id = 0; id < chip->ngpio; id++) { | |
1255 | struct gpio_desc *desc = &chip->desc[id]; | |
1256 | desc->chip = chip; | |
d8f388d8 DB |
1257 | |
1258 | /* REVISIT: most hardware initializes GPIOs as | |
1259 | * inputs (often with pullups enabled) so power | |
1260 | * usage is minimized. Linux code should set the | |
1261 | * gpio direction first thing; but until it does, | |
80b0a602 | 1262 | * and in case chip->get_direction is not set, |
d8f388d8 DB |
1263 | * we may expose the wrong direction in sysfs. |
1264 | */ | |
6c0b4e6c | 1265 | desc->flags = !chip->direction_input |
d8f388d8 DB |
1266 | ? (1 << FLAG_IS_OUT) |
1267 | : 0; | |
d2876d08 DB |
1268 | } |
1269 | } | |
1270 | ||
3bae4811 ZG |
1271 | spin_unlock_irqrestore(&gpio_lock, flags); |
1272 | ||
f23f1516 SH |
1273 | #ifdef CONFIG_PINCTRL |
1274 | INIT_LIST_HEAD(&chip->pin_ranges); | |
1275 | #endif | |
1276 | ||
391c970c AV |
1277 | of_gpiochip_add(chip); |
1278 | ||
cedb1881 AV |
1279 | if (status) |
1280 | goto fail; | |
1281 | ||
1282 | status = gpiochip_export(chip); | |
1283 | if (status) | |
1284 | goto fail; | |
1285 | ||
ee1c1e7d | 1286 | pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n", |
64842aad GL |
1287 | chip->base, chip->base + chip->ngpio - 1, |
1288 | chip->label ? : "generic"); | |
1289 | ||
cedb1881 | 1290 | return 0; |
3bae4811 ZG |
1291 | |
1292 | unlock: | |
1293 | spin_unlock_irqrestore(&gpio_lock, flags); | |
d2876d08 DB |
1294 | fail: |
1295 | /* failures here can mean systems won't boot... */ | |
cedb1881 AV |
1296 | pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n", |
1297 | chip->base, chip->base + chip->ngpio - 1, | |
1298 | chip->label ? : "generic"); | |
d2876d08 DB |
1299 | return status; |
1300 | } | |
1301 | EXPORT_SYMBOL_GPL(gpiochip_add); | |
1302 | ||
1303 | /** | |
1304 | * gpiochip_remove() - unregister a gpio_chip | |
1305 | * @chip: the chip to unregister | |
1306 | * | |
1307 | * A gpio_chip with any GPIOs still requested may not be removed. | |
1308 | */ | |
1309 | int gpiochip_remove(struct gpio_chip *chip) | |
1310 | { | |
1311 | unsigned long flags; | |
1312 | int status = 0; | |
1313 | unsigned id; | |
1314 | ||
1315 | spin_lock_irqsave(&gpio_lock, flags); | |
1316 | ||
9ef0d6f7 | 1317 | gpiochip_remove_pin_ranges(chip); |
391c970c AV |
1318 | of_gpiochip_remove(chip); |
1319 | ||
6c0b4e6c AC |
1320 | for (id = 0; id < chip->ngpio; id++) { |
1321 | if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) { | |
d2876d08 DB |
1322 | status = -EBUSY; |
1323 | break; | |
1324 | } | |
1325 | } | |
1326 | if (status == 0) { | |
6c0b4e6c AC |
1327 | for (id = 0; id < chip->ngpio; id++) |
1328 | chip->desc[id].chip = NULL; | |
1a989d0f AC |
1329 | |
1330 | list_del(&chip->list); | |
d2876d08 DB |
1331 | } |
1332 | ||
1333 | spin_unlock_irqrestore(&gpio_lock, flags); | |
d8f388d8 DB |
1334 | |
1335 | if (status == 0) | |
1336 | gpiochip_unexport(chip); | |
1337 | ||
d2876d08 DB |
1338 | return status; |
1339 | } | |
1340 | EXPORT_SYMBOL_GPL(gpiochip_remove); | |
1341 | ||
594fa265 GL |
1342 | /** |
1343 | * gpiochip_find() - iterator for locating a specific gpio_chip | |
1344 | * @data: data to pass to match function | |
1345 | * @callback: Callback function to check gpio_chip | |
1346 | * | |
1347 | * Similar to bus_find_device. It returns a reference to a gpio_chip as | |
1348 | * determined by a user supplied @match callback. The callback should return | |
1349 | * 0 if the device doesn't match and non-zero if it does. If the callback is | |
1350 | * non-zero, this function will return to the caller and not iterate over any | |
1351 | * more gpio_chips. | |
1352 | */ | |
07ce8ec7 | 1353 | struct gpio_chip *gpiochip_find(void *data, |
6e2cf651 | 1354 | int (*match)(struct gpio_chip *chip, |
3d0f7cf0 | 1355 | void *data)) |
594fa265 | 1356 | { |
125eef96 | 1357 | struct gpio_chip *chip; |
594fa265 | 1358 | unsigned long flags; |
594fa265 GL |
1359 | |
1360 | spin_lock_irqsave(&gpio_lock, flags); | |
125eef96 AC |
1361 | list_for_each_entry(chip, &gpio_chips, list) |
1362 | if (match(chip, data)) | |
594fa265 | 1363 | break; |
125eef96 AC |
1364 | |
1365 | /* No match? */ | |
1366 | if (&chip->list == &gpio_chips) | |
1367 | chip = NULL; | |
594fa265 GL |
1368 | spin_unlock_irqrestore(&gpio_lock, flags); |
1369 | ||
1370 | return chip; | |
1371 | } | |
8fa0c9bf | 1372 | EXPORT_SYMBOL_GPL(gpiochip_find); |
d2876d08 | 1373 | |
f23f1516 | 1374 | #ifdef CONFIG_PINCTRL |
165adc9c | 1375 | |
3f0f8670 LW |
1376 | /** |
1377 | * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping | |
1378 | * @chip: the gpiochip to add the range for | |
1379 | * @pinctrl_name: the dev_name() of the pin controller to map to | |
316511c0 LW |
1380 | * @gpio_offset: the start offset in the current gpio_chip number space |
1381 | * @pin_offset: the start offset in the pin controller number space | |
3f0f8670 LW |
1382 | * @npins: the number of pins from the offset of each pin space (GPIO and |
1383 | * pin controller) to accumulate in this range | |
1384 | */ | |
1e63d7b9 | 1385 | int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name, |
316511c0 | 1386 | unsigned int gpio_offset, unsigned int pin_offset, |
3f0f8670 | 1387 | unsigned int npins) |
f23f1516 SH |
1388 | { |
1389 | struct gpio_pin_range *pin_range; | |
b4d4b1f0 | 1390 | int ret; |
f23f1516 | 1391 | |
3f0f8670 | 1392 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); |
f23f1516 SH |
1393 | if (!pin_range) { |
1394 | pr_err("%s: GPIO chip: failed to allocate pin ranges\n", | |
1395 | chip->label); | |
1e63d7b9 | 1396 | return -ENOMEM; |
f23f1516 SH |
1397 | } |
1398 | ||
3f0f8670 | 1399 | /* Use local offset as range ID */ |
316511c0 | 1400 | pin_range->range.id = gpio_offset; |
3f0f8670 | 1401 | pin_range->range.gc = chip; |
f23f1516 | 1402 | pin_range->range.name = chip->label; |
316511c0 LW |
1403 | pin_range->range.base = chip->base + gpio_offset; |
1404 | pin_range->range.pin_base = pin_offset; | |
f23f1516 | 1405 | pin_range->range.npins = npins; |
192c369c | 1406 | pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name, |
f23f1516 | 1407 | &pin_range->range); |
8f23ca1a | 1408 | if (IS_ERR(pin_range->pctldev)) { |
b4d4b1f0 | 1409 | ret = PTR_ERR(pin_range->pctldev); |
3f0f8670 LW |
1410 | pr_err("%s: GPIO chip: could not create pin range\n", |
1411 | chip->label); | |
1412 | kfree(pin_range); | |
b4d4b1f0 | 1413 | return ret; |
3f0f8670 | 1414 | } |
316511c0 LW |
1415 | pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n", |
1416 | chip->label, gpio_offset, gpio_offset + npins - 1, | |
1417 | pinctl_name, | |
1418 | pin_offset, pin_offset + npins - 1); | |
f23f1516 SH |
1419 | |
1420 | list_add_tail(&pin_range->node, &chip->pin_ranges); | |
1e63d7b9 LW |
1421 | |
1422 | return 0; | |
f23f1516 | 1423 | } |
165adc9c | 1424 | EXPORT_SYMBOL_GPL(gpiochip_add_pin_range); |
f23f1516 | 1425 | |
3f0f8670 LW |
1426 | /** |
1427 | * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings | |
1428 | * @chip: the chip to remove all the mappings for | |
1429 | */ | |
f23f1516 SH |
1430 | void gpiochip_remove_pin_ranges(struct gpio_chip *chip) |
1431 | { | |
1432 | struct gpio_pin_range *pin_range, *tmp; | |
1433 | ||
1434 | list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) { | |
1435 | list_del(&pin_range->node); | |
1436 | pinctrl_remove_gpio_range(pin_range->pctldev, | |
1437 | &pin_range->range); | |
3f0f8670 | 1438 | kfree(pin_range); |
f23f1516 SH |
1439 | } |
1440 | } | |
165adc9c LW |
1441 | EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges); |
1442 | ||
1443 | #endif /* CONFIG_PINCTRL */ | |
f23f1516 | 1444 | |
d2876d08 DB |
1445 | /* These "optional" allocation calls help prevent drivers from stomping |
1446 | * on each other, and help provide better diagnostics in debugfs. | |
1447 | * They're called even less than the "set direction" calls. | |
1448 | */ | |
372e722e | 1449 | static int gpiod_request(struct gpio_desc *desc, const char *label) |
d2876d08 | 1450 | { |
35e8bb51 | 1451 | struct gpio_chip *chip; |
e9354576 | 1452 | int status = -EPROBE_DEFER; |
d2876d08 DB |
1453 | unsigned long flags; |
1454 | ||
0204df47 | 1455 | if (!desc) { |
bcabdef1 AC |
1456 | pr_warn("%s: invalid GPIO\n", __func__); |
1457 | return -EINVAL; | |
ad2fab36 | 1458 | } |
bcabdef1 AC |
1459 | |
1460 | spin_lock_irqsave(&gpio_lock, flags); | |
1461 | ||
35e8bb51 | 1462 | chip = desc->chip; |
0204df47 AC |
1463 | if (chip == NULL) |
1464 | goto done; | |
d2876d08 | 1465 | |
35e8bb51 | 1466 | if (!try_module_get(chip->owner)) |
438d8908 GL |
1467 | goto done; |
1468 | ||
d2876d08 | 1469 | /* NOTE: gpio_request() can be called in early boot, |
35e8bb51 | 1470 | * before IRQs are enabled, for non-sleeping (SOC) GPIOs. |
d2876d08 DB |
1471 | */ |
1472 | ||
1473 | if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { | |
1474 | desc_set_label(desc, label ? : "?"); | |
1475 | status = 0; | |
438d8908 | 1476 | } else { |
d2876d08 | 1477 | status = -EBUSY; |
35e8bb51 | 1478 | module_put(chip->owner); |
7460db56 | 1479 | goto done; |
35e8bb51 DB |
1480 | } |
1481 | ||
1482 | if (chip->request) { | |
1483 | /* chip->request may sleep */ | |
1484 | spin_unlock_irqrestore(&gpio_lock, flags); | |
372e722e | 1485 | status = chip->request(chip, gpio_chip_hwgpio(desc)); |
35e8bb51 DB |
1486 | spin_lock_irqsave(&gpio_lock, flags); |
1487 | ||
1488 | if (status < 0) { | |
1489 | desc_set_label(desc, NULL); | |
1490 | module_put(chip->owner); | |
1491 | clear_bit(FLAG_REQUESTED, &desc->flags); | |
80b0a602 | 1492 | goto done; |
35e8bb51 | 1493 | } |
438d8908 | 1494 | } |
80b0a602 MN |
1495 | if (chip->get_direction) { |
1496 | /* chip->get_direction may sleep */ | |
1497 | spin_unlock_irqrestore(&gpio_lock, flags); | |
372e722e | 1498 | gpiod_get_direction(desc); |
80b0a602 MN |
1499 | spin_lock_irqsave(&gpio_lock, flags); |
1500 | } | |
d2876d08 DB |
1501 | done: |
1502 | if (status) | |
372e722e | 1503 | pr_debug("_gpio_request: gpio-%d (%s) status %d\n", |
bcabdef1 | 1504 | desc_to_gpio(desc), label ? : "?", status); |
d2876d08 DB |
1505 | spin_unlock_irqrestore(&gpio_lock, flags); |
1506 | return status; | |
1507 | } | |
372e722e AC |
1508 | |
1509 | int gpio_request(unsigned gpio, const char *label) | |
1510 | { | |
1511 | return gpiod_request(gpio_to_desc(gpio), label); | |
1512 | } | |
d2876d08 DB |
1513 | EXPORT_SYMBOL_GPL(gpio_request); |
1514 | ||
372e722e | 1515 | static void gpiod_free(struct gpio_desc *desc) |
d2876d08 DB |
1516 | { |
1517 | unsigned long flags; | |
35e8bb51 | 1518 | struct gpio_chip *chip; |
d2876d08 | 1519 | |
3d599d1c UKK |
1520 | might_sleep(); |
1521 | ||
372e722e | 1522 | if (!desc) { |
d2876d08 DB |
1523 | WARN_ON(extra_checks); |
1524 | return; | |
1525 | } | |
1526 | ||
372e722e | 1527 | gpiod_unexport(desc); |
d8f388d8 | 1528 | |
d2876d08 DB |
1529 | spin_lock_irqsave(&gpio_lock, flags); |
1530 | ||
35e8bb51 DB |
1531 | chip = desc->chip; |
1532 | if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) { | |
1533 | if (chip->free) { | |
1534 | spin_unlock_irqrestore(&gpio_lock, flags); | |
9c4ba946 | 1535 | might_sleep_if(chip->can_sleep); |
372e722e | 1536 | chip->free(chip, gpio_chip_hwgpio(desc)); |
35e8bb51 DB |
1537 | spin_lock_irqsave(&gpio_lock, flags); |
1538 | } | |
d2876d08 | 1539 | desc_set_label(desc, NULL); |
438d8908 | 1540 | module_put(desc->chip->owner); |
07697461 | 1541 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); |
35e8bb51 | 1542 | clear_bit(FLAG_REQUESTED, &desc->flags); |
aca5ce14 | 1543 | clear_bit(FLAG_OPEN_DRAIN, &desc->flags); |
25553ff0 | 1544 | clear_bit(FLAG_OPEN_SOURCE, &desc->flags); |
438d8908 | 1545 | } else |
d2876d08 DB |
1546 | WARN_ON(extra_checks); |
1547 | ||
1548 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1549 | } | |
372e722e AC |
1550 | |
1551 | void gpio_free(unsigned gpio) | |
1552 | { | |
1553 | gpiod_free(gpio_to_desc(gpio)); | |
1554 | } | |
d2876d08 DB |
1555 | EXPORT_SYMBOL_GPL(gpio_free); |
1556 | ||
3e45f1d1 EM |
1557 | /** |
1558 | * gpio_request_one - request a single GPIO with initial configuration | |
1559 | * @gpio: the GPIO number | |
1560 | * @flags: GPIO configuration as specified by GPIOF_* | |
1561 | * @label: a literal description string of this GPIO | |
1562 | */ | |
1563 | int gpio_request_one(unsigned gpio, unsigned long flags, const char *label) | |
1564 | { | |
372e722e | 1565 | struct gpio_desc *desc; |
3e45f1d1 EM |
1566 | int err; |
1567 | ||
372e722e AC |
1568 | desc = gpio_to_desc(gpio); |
1569 | ||
1570 | err = gpiod_request(desc, label); | |
3e45f1d1 EM |
1571 | if (err) |
1572 | return err; | |
1573 | ||
aca5ce14 | 1574 | if (flags & GPIOF_OPEN_DRAIN) |
372e722e | 1575 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); |
aca5ce14 | 1576 | |
25553ff0 | 1577 | if (flags & GPIOF_OPEN_SOURCE) |
372e722e | 1578 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); |
25553ff0 | 1579 | |
3e45f1d1 | 1580 | if (flags & GPIOF_DIR_IN) |
372e722e | 1581 | err = gpiod_direction_input(desc); |
3e45f1d1 | 1582 | else |
372e722e | 1583 | err = gpiod_direction_output(desc, |
3e45f1d1 EM |
1584 | (flags & GPIOF_INIT_HIGH) ? 1 : 0); |
1585 | ||
e254811c | 1586 | if (err) |
fc3a1f04 WS |
1587 | goto free_gpio; |
1588 | ||
1589 | if (flags & GPIOF_EXPORT) { | |
372e722e | 1590 | err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE); |
fc3a1f04 WS |
1591 | if (err) |
1592 | goto free_gpio; | |
1593 | } | |
1594 | ||
1595 | return 0; | |
e254811c | 1596 | |
fc3a1f04 | 1597 | free_gpio: |
372e722e | 1598 | gpiod_free(desc); |
3e45f1d1 EM |
1599 | return err; |
1600 | } | |
1601 | EXPORT_SYMBOL_GPL(gpio_request_one); | |
1602 | ||
1603 | /** | |
1604 | * gpio_request_array - request multiple GPIOs in a single call | |
1605 | * @array: array of the 'struct gpio' | |
1606 | * @num: how many GPIOs in the array | |
1607 | */ | |
7c295975 | 1608 | int gpio_request_array(const struct gpio *array, size_t num) |
3e45f1d1 EM |
1609 | { |
1610 | int i, err; | |
1611 | ||
1612 | for (i = 0; i < num; i++, array++) { | |
1613 | err = gpio_request_one(array->gpio, array->flags, array->label); | |
1614 | if (err) | |
1615 | goto err_free; | |
1616 | } | |
1617 | return 0; | |
1618 | ||
1619 | err_free: | |
1620 | while (i--) | |
1621 | gpio_free((--array)->gpio); | |
1622 | return err; | |
1623 | } | |
1624 | EXPORT_SYMBOL_GPL(gpio_request_array); | |
1625 | ||
1626 | /** | |
1627 | * gpio_free_array - release multiple GPIOs in a single call | |
1628 | * @array: array of the 'struct gpio' | |
1629 | * @num: how many GPIOs in the array | |
1630 | */ | |
7c295975 | 1631 | void gpio_free_array(const struct gpio *array, size_t num) |
3e45f1d1 EM |
1632 | { |
1633 | while (num--) | |
1634 | gpio_free((array++)->gpio); | |
1635 | } | |
1636 | EXPORT_SYMBOL_GPL(gpio_free_array); | |
d2876d08 DB |
1637 | |
1638 | /** | |
1639 | * gpiochip_is_requested - return string iff signal was requested | |
1640 | * @chip: controller managing the signal | |
1641 | * @offset: of signal within controller's 0..(ngpio - 1) range | |
1642 | * | |
1643 | * Returns NULL if the GPIO is not currently requested, else a string. | |
1644 | * If debugfs support is enabled, the string returned is the label passed | |
1645 | * to gpio_request(); otherwise it is a meaningless constant. | |
1646 | * | |
1647 | * This function is for use by GPIO controller drivers. The label can | |
1648 | * help with diagnostics, and knowing that the signal is used as a GPIO | |
1649 | * can help avoid accidentally multiplexing it to another controller. | |
1650 | */ | |
1651 | const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset) | |
1652 | { | |
6c0b4e6c | 1653 | struct gpio_desc *desc; |
d2876d08 | 1654 | |
6c0b4e6c | 1655 | if (!GPIO_OFFSET_VALID(chip, offset)) |
d2876d08 | 1656 | return NULL; |
6c0b4e6c AC |
1657 | |
1658 | desc = &chip->desc[offset]; | |
1659 | ||
372e722e | 1660 | if (test_bit(FLAG_REQUESTED, &desc->flags) == 0) |
d2876d08 DB |
1661 | return NULL; |
1662 | #ifdef CONFIG_DEBUG_FS | |
372e722e | 1663 | return desc->label; |
d2876d08 DB |
1664 | #else |
1665 | return "?"; | |
1666 | #endif | |
1667 | } | |
1668 | EXPORT_SYMBOL_GPL(gpiochip_is_requested); | |
1669 | ||
1670 | ||
1671 | /* Drivers MUST set GPIO direction before making get/set calls. In | |
1672 | * some cases this is done in early boot, before IRQs are enabled. | |
1673 | * | |
1674 | * As a rule these aren't called more than once (except for drivers | |
1675 | * using the open-drain emulation idiom) so these are natural places | |
1676 | * to accumulate extra debugging checks. Note that we can't (yet) | |
1677 | * rely on gpio_request() having been called beforehand. | |
1678 | */ | |
1679 | ||
372e722e | 1680 | static int gpiod_direction_input(struct gpio_desc *desc) |
d2876d08 DB |
1681 | { |
1682 | unsigned long flags; | |
1683 | struct gpio_chip *chip; | |
d2876d08 | 1684 | int status = -EINVAL; |
372e722e | 1685 | int offset; |
d2876d08 | 1686 | |
be1a4b13 | 1687 | if (!desc || !desc->chip) { |
bcabdef1 AC |
1688 | pr_warn("%s: invalid GPIO\n", __func__); |
1689 | return -EINVAL; | |
1690 | } | |
1691 | ||
be1a4b13 LW |
1692 | chip = desc->chip; |
1693 | if (!chip->get || !chip->direction_input) { | |
6424de5a MB |
1694 | gpiod_warn(desc, |
1695 | "%s: missing get() or direction_input() operations\n", | |
1696 | __func__); | |
be1a4b13 LW |
1697 | return -EIO; |
1698 | } | |
1699 | ||
d2876d08 DB |
1700 | spin_lock_irqsave(&gpio_lock, flags); |
1701 | ||
372e722e | 1702 | status = gpio_ensure_requested(desc); |
35e8bb51 DB |
1703 | if (status < 0) |
1704 | goto fail; | |
d2876d08 DB |
1705 | |
1706 | /* now we know the gpio is valid and chip won't vanish */ | |
1707 | ||
1708 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1709 | ||
9c4ba946 | 1710 | might_sleep_if(chip->can_sleep); |
d2876d08 | 1711 | |
372e722e | 1712 | offset = gpio_chip_hwgpio(desc); |
35e8bb51 | 1713 | if (status) { |
372e722e | 1714 | status = chip->request(chip, offset); |
35e8bb51 | 1715 | if (status < 0) { |
6424de5a | 1716 | gpiod_dbg(desc, "chip request fail, %d\n", status); |
35e8bb51 DB |
1717 | /* and it's not available to anyone else ... |
1718 | * gpio_request() is the fully clean solution. | |
1719 | */ | |
1720 | goto lose; | |
1721 | } | |
1722 | } | |
1723 | ||
372e722e | 1724 | status = chip->direction_input(chip, offset); |
d2876d08 DB |
1725 | if (status == 0) |
1726 | clear_bit(FLAG_IS_OUT, &desc->flags); | |
3f397c21 | 1727 | |
372e722e | 1728 | trace_gpio_direction(desc_to_gpio(desc), 1, status); |
35e8bb51 | 1729 | lose: |
d2876d08 DB |
1730 | return status; |
1731 | fail: | |
1732 | spin_unlock_irqrestore(&gpio_lock, flags); | |
bcabdef1 | 1733 | if (status) |
6424de5a | 1734 | gpiod_dbg(desc, "%s status %d\n", __func__, status); |
d2876d08 DB |
1735 | return status; |
1736 | } | |
372e722e AC |
1737 | |
1738 | int gpio_direction_input(unsigned gpio) | |
1739 | { | |
1740 | return gpiod_direction_input(gpio_to_desc(gpio)); | |
1741 | } | |
d2876d08 DB |
1742 | EXPORT_SYMBOL_GPL(gpio_direction_input); |
1743 | ||
372e722e | 1744 | static int gpiod_direction_output(struct gpio_desc *desc, int value) |
d2876d08 DB |
1745 | { |
1746 | unsigned long flags; | |
1747 | struct gpio_chip *chip; | |
d2876d08 | 1748 | int status = -EINVAL; |
372e722e | 1749 | int offset; |
d2876d08 | 1750 | |
be1a4b13 | 1751 | if (!desc || !desc->chip) { |
bcabdef1 AC |
1752 | pr_warn("%s: invalid GPIO\n", __func__); |
1753 | return -EINVAL; | |
1754 | } | |
1755 | ||
d468bf9e LW |
1756 | /* GPIOs used for IRQs shall not be set as output */ |
1757 | if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) { | |
1758 | gpiod_err(desc, | |
1759 | "%s: tried to set a GPIO tied to an IRQ as output\n", | |
1760 | __func__); | |
1761 | return -EIO; | |
1762 | } | |
1763 | ||
aca5ce14 LD |
1764 | /* Open drain pin should not be driven to 1 */ |
1765 | if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags)) | |
372e722e | 1766 | return gpiod_direction_input(desc); |
aca5ce14 | 1767 | |
25553ff0 LD |
1768 | /* Open source pin should not be driven to 0 */ |
1769 | if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags)) | |
372e722e | 1770 | return gpiod_direction_input(desc); |
25553ff0 | 1771 | |
be1a4b13 LW |
1772 | chip = desc->chip; |
1773 | if (!chip->set || !chip->direction_output) { | |
6424de5a MB |
1774 | gpiod_warn(desc, |
1775 | "%s: missing set() or direction_output() operations\n", | |
1776 | __func__); | |
be1a4b13 LW |
1777 | return -EIO; |
1778 | } | |
1779 | ||
d2876d08 DB |
1780 | spin_lock_irqsave(&gpio_lock, flags); |
1781 | ||
372e722e | 1782 | status = gpio_ensure_requested(desc); |
35e8bb51 DB |
1783 | if (status < 0) |
1784 | goto fail; | |
d2876d08 DB |
1785 | |
1786 | /* now we know the gpio is valid and chip won't vanish */ | |
1787 | ||
1788 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1789 | ||
9c4ba946 | 1790 | might_sleep_if(chip->can_sleep); |
d2876d08 | 1791 | |
372e722e | 1792 | offset = gpio_chip_hwgpio(desc); |
35e8bb51 | 1793 | if (status) { |
372e722e | 1794 | status = chip->request(chip, offset); |
35e8bb51 | 1795 | if (status < 0) { |
6424de5a | 1796 | gpiod_dbg(desc, "chip request fail, %d\n", status); |
35e8bb51 DB |
1797 | /* and it's not available to anyone else ... |
1798 | * gpio_request() is the fully clean solution. | |
1799 | */ | |
1800 | goto lose; | |
1801 | } | |
1802 | } | |
1803 | ||
372e722e | 1804 | status = chip->direction_output(chip, offset, value); |
d2876d08 DB |
1805 | if (status == 0) |
1806 | set_bit(FLAG_IS_OUT, &desc->flags); | |
372e722e AC |
1807 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
1808 | trace_gpio_direction(desc_to_gpio(desc), 0, status); | |
35e8bb51 | 1809 | lose: |
d2876d08 DB |
1810 | return status; |
1811 | fail: | |
1812 | spin_unlock_irqrestore(&gpio_lock, flags); | |
bcabdef1 | 1813 | if (status) |
6424de5a | 1814 | gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status); |
d2876d08 DB |
1815 | return status; |
1816 | } | |
372e722e AC |
1817 | |
1818 | int gpio_direction_output(unsigned gpio, int value) | |
1819 | { | |
1820 | return gpiod_direction_output(gpio_to_desc(gpio), value); | |
1821 | } | |
d2876d08 DB |
1822 | EXPORT_SYMBOL_GPL(gpio_direction_output); |
1823 | ||
c4b5be98 FB |
1824 | /** |
1825 | * gpio_set_debounce - sets @debounce time for a @gpio | |
1826 | * @gpio: the gpio to set debounce time | |
1827 | * @debounce: debounce time is microseconds | |
65d87656 LW |
1828 | * |
1829 | * returns -ENOTSUPP if the controller does not support setting | |
1830 | * debounce. | |
c4b5be98 | 1831 | */ |
372e722e | 1832 | static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) |
c4b5be98 FB |
1833 | { |
1834 | unsigned long flags; | |
1835 | struct gpio_chip *chip; | |
c4b5be98 | 1836 | int status = -EINVAL; |
372e722e | 1837 | int offset; |
c4b5be98 | 1838 | |
be1a4b13 | 1839 | if (!desc || !desc->chip) { |
bcabdef1 AC |
1840 | pr_warn("%s: invalid GPIO\n", __func__); |
1841 | return -EINVAL; | |
1842 | } | |
1843 | ||
c4b5be98 | 1844 | chip = desc->chip; |
be1a4b13 | 1845 | if (!chip->set || !chip->set_debounce) { |
6424de5a MB |
1846 | gpiod_dbg(desc, |
1847 | "%s: missing set() or set_debounce() operations\n", | |
1848 | __func__); | |
65d87656 | 1849 | return -ENOTSUPP; |
be1a4b13 LW |
1850 | } |
1851 | ||
1852 | spin_lock_irqsave(&gpio_lock, flags); | |
372e722e AC |
1853 | |
1854 | status = gpio_ensure_requested(desc); | |
c4b5be98 FB |
1855 | if (status < 0) |
1856 | goto fail; | |
1857 | ||
1858 | /* now we know the gpio is valid and chip won't vanish */ | |
1859 | ||
1860 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1861 | ||
9c4ba946 | 1862 | might_sleep_if(chip->can_sleep); |
c4b5be98 | 1863 | |
372e722e AC |
1864 | offset = gpio_chip_hwgpio(desc); |
1865 | return chip->set_debounce(chip, offset, debounce); | |
c4b5be98 FB |
1866 | |
1867 | fail: | |
1868 | spin_unlock_irqrestore(&gpio_lock, flags); | |
bcabdef1 | 1869 | if (status) |
6424de5a | 1870 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
c4b5be98 FB |
1871 | |
1872 | return status; | |
1873 | } | |
372e722e AC |
1874 | |
1875 | int gpio_set_debounce(unsigned gpio, unsigned debounce) | |
1876 | { | |
1877 | return gpiod_set_debounce(gpio_to_desc(gpio), debounce); | |
1878 | } | |
c4b5be98 | 1879 | EXPORT_SYMBOL_GPL(gpio_set_debounce); |
d2876d08 DB |
1880 | |
1881 | /* I/O calls are only valid after configuration completed; the relevant | |
1882 | * "is this a valid GPIO" error checks should already have been done. | |
1883 | * | |
1884 | * "Get" operations are often inlinable as reading a pin value register, | |
1885 | * and masking the relevant bit in that register. | |
1886 | * | |
1887 | * When "set" operations are inlinable, they involve writing that mask to | |
1888 | * one register to set a low value, or a different register to set it high. | |
1889 | * Otherwise locking is needed, so there may be little value to inlining. | |
1890 | * | |
1891 | *------------------------------------------------------------------------ | |
1892 | * | |
1893 | * IMPORTANT!!! The hot paths -- get/set value -- assume that callers | |
1894 | * have requested the GPIO. That can include implicit requesting by | |
1895 | * a direction setting call. Marking a gpio as requested locks its chip | |
1896 | * in memory, guaranteeing that these table lookups need no more locking | |
1897 | * and that gpiochip_remove() will fail. | |
1898 | * | |
1899 | * REVISIT when debugging, consider adding some instrumentation to ensure | |
1900 | * that the GPIO was actually requested. | |
1901 | */ | |
1902 | ||
d8e0ac08 AC |
1903 | static int _gpiod_get_value(const struct gpio_desc *desc) |
1904 | { | |
1905 | struct gpio_chip *chip; | |
1906 | int value; | |
1907 | int offset; | |
1908 | ||
1909 | chip = desc->chip; | |
1910 | offset = gpio_chip_hwgpio(desc); | |
1911 | value = chip->get ? chip->get(chip, offset) : 0; | |
1912 | trace_gpio_value(desc_to_gpio(desc), 1, value); | |
1913 | return value; | |
1914 | } | |
1915 | ||
d2876d08 DB |
1916 | /** |
1917 | * __gpio_get_value() - return a gpio's value | |
1918 | * @gpio: gpio whose value will be returned | |
1919 | * Context: any | |
1920 | * | |
1921 | * This is used directly or indirectly to implement gpio_get_value(). | |
1922 | * It returns the zero or nonzero value provided by the associated | |
1923 | * gpio_chip.get() method; or zero if no such method is provided. | |
1924 | */ | |
def63433 | 1925 | static int gpiod_get_value(const struct gpio_desc *desc) |
d2876d08 | 1926 | { |
bcabdef1 AC |
1927 | if (!desc) |
1928 | return 0; | |
e4e449e8 | 1929 | /* Should be using gpio_get_value_cansleep() */ |
d8e0ac08 AC |
1930 | WARN_ON(desc->chip->can_sleep); |
1931 | return _gpiod_get_value(desc); | |
d2876d08 | 1932 | } |
372e722e AC |
1933 | |
1934 | int __gpio_get_value(unsigned gpio) | |
1935 | { | |
1936 | return gpiod_get_value(gpio_to_desc(gpio)); | |
1937 | } | |
d2876d08 DB |
1938 | EXPORT_SYMBOL_GPL(__gpio_get_value); |
1939 | ||
aca5ce14 LD |
1940 | /* |
1941 | * _gpio_set_open_drain_value() - Set the open drain gpio's value. | |
1942 | * @gpio: Gpio whose state need to be set. | |
1943 | * @chip: Gpio chip. | |
1944 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. | |
1945 | */ | |
372e722e | 1946 | static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value) |
aca5ce14 LD |
1947 | { |
1948 | int err = 0; | |
372e722e AC |
1949 | struct gpio_chip *chip = desc->chip; |
1950 | int offset = gpio_chip_hwgpio(desc); | |
1951 | ||
aca5ce14 | 1952 | if (value) { |
372e722e | 1953 | err = chip->direction_input(chip, offset); |
aca5ce14 | 1954 | if (!err) |
372e722e | 1955 | clear_bit(FLAG_IS_OUT, &desc->flags); |
aca5ce14 | 1956 | } else { |
372e722e | 1957 | err = chip->direction_output(chip, offset, 0); |
aca5ce14 | 1958 | if (!err) |
372e722e | 1959 | set_bit(FLAG_IS_OUT, &desc->flags); |
aca5ce14 | 1960 | } |
372e722e | 1961 | trace_gpio_direction(desc_to_gpio(desc), value, err); |
aca5ce14 | 1962 | if (err < 0) |
6424de5a MB |
1963 | gpiod_err(desc, |
1964 | "%s: Error in set_value for open drain err %d\n", | |
1965 | __func__, err); | |
aca5ce14 LD |
1966 | } |
1967 | ||
25553ff0 LD |
1968 | /* |
1969 | * _gpio_set_open_source() - Set the open source gpio's value. | |
1970 | * @gpio: Gpio whose state need to be set. | |
1971 | * @chip: Gpio chip. | |
1972 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. | |
1973 | */ | |
372e722e | 1974 | static void _gpio_set_open_source_value(struct gpio_desc *desc, int value) |
25553ff0 LD |
1975 | { |
1976 | int err = 0; | |
372e722e AC |
1977 | struct gpio_chip *chip = desc->chip; |
1978 | int offset = gpio_chip_hwgpio(desc); | |
1979 | ||
25553ff0 | 1980 | if (value) { |
372e722e | 1981 | err = chip->direction_output(chip, offset, 1); |
25553ff0 | 1982 | if (!err) |
372e722e | 1983 | set_bit(FLAG_IS_OUT, &desc->flags); |
25553ff0 | 1984 | } else { |
372e722e | 1985 | err = chip->direction_input(chip, offset); |
25553ff0 | 1986 | if (!err) |
372e722e | 1987 | clear_bit(FLAG_IS_OUT, &desc->flags); |
25553ff0 | 1988 | } |
372e722e | 1989 | trace_gpio_direction(desc_to_gpio(desc), !value, err); |
25553ff0 | 1990 | if (err < 0) |
6424de5a MB |
1991 | gpiod_err(desc, |
1992 | "%s: Error in set_value for open source err %d\n", | |
1993 | __func__, err); | |
25553ff0 LD |
1994 | } |
1995 | ||
d8e0ac08 AC |
1996 | static void _gpiod_set_value(struct gpio_desc *desc, int value) |
1997 | { | |
1998 | struct gpio_chip *chip; | |
1999 | ||
2000 | chip = desc->chip; | |
2001 | trace_gpio_value(desc_to_gpio(desc), 0, value); | |
2002 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) | |
2003 | _gpio_set_open_drain_value(desc, value); | |
2004 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) | |
2005 | _gpio_set_open_source_value(desc, value); | |
2006 | else | |
2007 | chip->set(chip, gpio_chip_hwgpio(desc), value); | |
2008 | } | |
2009 | ||
d2876d08 DB |
2010 | /** |
2011 | * __gpio_set_value() - assign a gpio's value | |
2012 | * @gpio: gpio whose value will be assigned | |
2013 | * @value: value to assign | |
2014 | * Context: any | |
2015 | * | |
2016 | * This is used directly or indirectly to implement gpio_set_value(). | |
2017 | * It invokes the associated gpio_chip.set() method. | |
2018 | */ | |
372e722e | 2019 | static void gpiod_set_value(struct gpio_desc *desc, int value) |
d2876d08 | 2020 | { |
d2876d08 | 2021 | |
bcabdef1 AC |
2022 | if (!desc) |
2023 | return; | |
e4e449e8 | 2024 | /* Should be using gpio_set_value_cansleep() */ |
d8e0ac08 AC |
2025 | WARN_ON(desc->chip->can_sleep); |
2026 | _gpiod_set_value(desc, value); | |
372e722e AC |
2027 | } |
2028 | ||
2029 | void __gpio_set_value(unsigned gpio, int value) | |
2030 | { | |
2031 | return gpiod_set_value(gpio_to_desc(gpio), value); | |
d2876d08 DB |
2032 | } |
2033 | EXPORT_SYMBOL_GPL(__gpio_set_value); | |
2034 | ||
2035 | /** | |
2036 | * __gpio_cansleep() - report whether gpio value access will sleep | |
2037 | * @gpio: gpio in question | |
2038 | * Context: any | |
2039 | * | |
2040 | * This is used directly or indirectly to implement gpio_cansleep(). It | |
2041 | * returns nonzero if access reading or writing the GPIO value can sleep. | |
2042 | */ | |
def63433 | 2043 | static int gpiod_cansleep(const struct gpio_desc *desc) |
d2876d08 | 2044 | { |
bcabdef1 AC |
2045 | if (!desc) |
2046 | return 0; | |
d2876d08 | 2047 | /* only call this on GPIOs that are valid! */ |
372e722e AC |
2048 | return desc->chip->can_sleep; |
2049 | } | |
d2876d08 | 2050 | |
372e722e AC |
2051 | int __gpio_cansleep(unsigned gpio) |
2052 | { | |
2053 | return gpiod_cansleep(gpio_to_desc(gpio)); | |
d2876d08 DB |
2054 | } |
2055 | EXPORT_SYMBOL_GPL(__gpio_cansleep); | |
2056 | ||
0f6d504e DB |
2057 | /** |
2058 | * __gpio_to_irq() - return the IRQ corresponding to a GPIO | |
2059 | * @gpio: gpio whose IRQ will be returned (already requested) | |
2060 | * Context: any | |
2061 | * | |
2062 | * This is used directly or indirectly to implement gpio_to_irq(). | |
2063 | * It returns the number of the IRQ signaled by this (input) GPIO, | |
2064 | * or a negative errno. | |
2065 | */ | |
def63433 | 2066 | static int gpiod_to_irq(const struct gpio_desc *desc) |
0f6d504e DB |
2067 | { |
2068 | struct gpio_chip *chip; | |
372e722e | 2069 | int offset; |
0f6d504e | 2070 | |
bcabdef1 AC |
2071 | if (!desc) |
2072 | return -EINVAL; | |
372e722e AC |
2073 | chip = desc->chip; |
2074 | offset = gpio_chip_hwgpio(desc); | |
2075 | return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO; | |
0f6d504e | 2076 | } |
0f6d504e | 2077 | |
372e722e AC |
2078 | int __gpio_to_irq(unsigned gpio) |
2079 | { | |
2080 | return gpiod_to_irq(gpio_to_desc(gpio)); | |
2081 | } | |
2082 | EXPORT_SYMBOL_GPL(__gpio_to_irq); | |
d2876d08 | 2083 | |
d468bf9e LW |
2084 | /** |
2085 | * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ | |
2086 | * @gpio: the GPIO line to lock as used for IRQ | |
2087 | * | |
2088 | * This is used directly by GPIO drivers that want to lock down | |
2089 | * a certain GPIO line to be used as IRQs, for example in the | |
2090 | * .to_irq() callback of their gpio_chip, or in the .irq_enable() | |
2091 | * of its irq_chip implementation if the GPIO is known from that | |
2092 | * code. | |
2093 | */ | |
2094 | static int gpiod_lock_as_irq(struct gpio_desc *desc) | |
2095 | { | |
2096 | if (!desc) | |
2097 | return -EINVAL; | |
2098 | ||
2099 | if (test_bit(FLAG_IS_OUT, &desc->flags)) { | |
2100 | gpiod_err(desc, | |
2101 | "%s: tried to flag a GPIO set as output for IRQ\n", | |
2102 | __func__); | |
2103 | return -EIO; | |
2104 | } | |
2105 | ||
2106 | set_bit(FLAG_USED_AS_IRQ, &desc->flags); | |
2107 | return 0; | |
2108 | } | |
2109 | ||
2110 | int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset) | |
2111 | { | |
2112 | return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip, offset)); | |
2113 | } | |
2114 | EXPORT_SYMBOL_GPL(gpio_lock_as_irq); | |
2115 | ||
2116 | /** | |
2117 | * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ | |
2118 | * @gpio: the GPIO line to unlock from IRQ usage | |
2119 | * | |
2120 | * This is used directly by GPIO drivers that want to indicate | |
2121 | * that a certain GPIO is no longer used exclusively for IRQ. | |
2122 | */ | |
2123 | static void gpiod_unlock_as_irq(struct gpio_desc *desc) | |
2124 | { | |
2125 | if (!desc) | |
2126 | return; | |
2127 | ||
2128 | clear_bit(FLAG_USED_AS_IRQ, &desc->flags); | |
2129 | } | |
2130 | ||
2131 | void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset) | |
2132 | { | |
2133 | return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip, offset)); | |
2134 | } | |
2135 | EXPORT_SYMBOL_GPL(gpio_unlock_as_irq); | |
d2876d08 DB |
2136 | |
2137 | /* There's no value in making it easy to inline GPIO calls that may sleep. | |
2138 | * Common examples include ones connected to I2C or SPI chips. | |
2139 | */ | |
2140 | ||
def63433 | 2141 | static int gpiod_get_value_cansleep(const struct gpio_desc *desc) |
d2876d08 | 2142 | { |
d2876d08 | 2143 | might_sleep_if(extra_checks); |
bcabdef1 AC |
2144 | if (!desc) |
2145 | return 0; | |
d8e0ac08 | 2146 | return _gpiod_get_value(desc); |
d2876d08 | 2147 | } |
372e722e AC |
2148 | |
2149 | int gpio_get_value_cansleep(unsigned gpio) | |
2150 | { | |
2151 | return gpiod_get_value_cansleep(gpio_to_desc(gpio)); | |
2152 | } | |
d2876d08 DB |
2153 | EXPORT_SYMBOL_GPL(gpio_get_value_cansleep); |
2154 | ||
372e722e | 2155 | static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) |
d2876d08 | 2156 | { |
d2876d08 | 2157 | might_sleep_if(extra_checks); |
bcabdef1 AC |
2158 | if (!desc) |
2159 | return; | |
d8e0ac08 | 2160 | _gpiod_set_value(desc, value); |
d2876d08 | 2161 | } |
d2876d08 | 2162 | |
372e722e AC |
2163 | void gpio_set_value_cansleep(unsigned gpio, int value) |
2164 | { | |
2165 | return gpiod_set_value_cansleep(gpio_to_desc(gpio), value); | |
2166 | } | |
2167 | EXPORT_SYMBOL_GPL(gpio_set_value_cansleep); | |
d2876d08 DB |
2168 | |
2169 | #ifdef CONFIG_DEBUG_FS | |
2170 | ||
d2876d08 DB |
2171 | static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
2172 | { | |
2173 | unsigned i; | |
2174 | unsigned gpio = chip->base; | |
6c0b4e6c | 2175 | struct gpio_desc *gdesc = &chip->desc[0]; |
d2876d08 | 2176 | int is_out; |
d468bf9e | 2177 | int is_irq; |
d2876d08 DB |
2178 | |
2179 | for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) { | |
2180 | if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) | |
2181 | continue; | |
2182 | ||
372e722e | 2183 | gpiod_get_direction(gdesc); |
d2876d08 | 2184 | is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); |
d468bf9e LW |
2185 | is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags); |
2186 | seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s", | |
d2876d08 DB |
2187 | gpio, gdesc->label, |
2188 | is_out ? "out" : "in ", | |
2189 | chip->get | |
2190 | ? (chip->get(chip, i) ? "hi" : "lo") | |
d468bf9e LW |
2191 | : "? ", |
2192 | is_irq ? "IRQ" : " "); | |
d2876d08 DB |
2193 | seq_printf(s, "\n"); |
2194 | } | |
2195 | } | |
2196 | ||
f9c4a31f | 2197 | static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) |
d2876d08 | 2198 | { |
362432ae | 2199 | unsigned long flags; |
f9c4a31f | 2200 | struct gpio_chip *chip = NULL; |
cb1650d4 | 2201 | loff_t index = *pos; |
d2876d08 | 2202 | |
f9c4a31f | 2203 | s->private = ""; |
d2876d08 | 2204 | |
362432ae | 2205 | spin_lock_irqsave(&gpio_lock, flags); |
cb1650d4 | 2206 | list_for_each_entry(chip, &gpio_chips, list) |
362432ae GL |
2207 | if (index-- == 0) { |
2208 | spin_unlock_irqrestore(&gpio_lock, flags); | |
cb1650d4 | 2209 | return chip; |
f9c4a31f | 2210 | } |
362432ae | 2211 | spin_unlock_irqrestore(&gpio_lock, flags); |
f9c4a31f | 2212 | |
cb1650d4 | 2213 | return NULL; |
f9c4a31f TR |
2214 | } |
2215 | ||
2216 | static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
2217 | { | |
362432ae | 2218 | unsigned long flags; |
f9c4a31f | 2219 | struct gpio_chip *chip = v; |
f9c4a31f TR |
2220 | void *ret = NULL; |
2221 | ||
362432ae | 2222 | spin_lock_irqsave(&gpio_lock, flags); |
cb1650d4 AC |
2223 | if (list_is_last(&chip->list, &gpio_chips)) |
2224 | ret = NULL; | |
2225 | else | |
2226 | ret = list_entry(chip->list.next, struct gpio_chip, list); | |
362432ae | 2227 | spin_unlock_irqrestore(&gpio_lock, flags); |
f9c4a31f TR |
2228 | |
2229 | s->private = "\n"; | |
2230 | ++*pos; | |
2231 | ||
2232 | return ret; | |
2233 | } | |
2234 | ||
2235 | static void gpiolib_seq_stop(struct seq_file *s, void *v) | |
2236 | { | |
2237 | } | |
2238 | ||
2239 | static int gpiolib_seq_show(struct seq_file *s, void *v) | |
2240 | { | |
2241 | struct gpio_chip *chip = v; | |
2242 | struct device *dev; | |
2243 | ||
2244 | seq_printf(s, "%sGPIOs %d-%d", (char *)s->private, | |
2245 | chip->base, chip->base + chip->ngpio - 1); | |
2246 | dev = chip->dev; | |
2247 | if (dev) | |
2248 | seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus", | |
2249 | dev_name(dev)); | |
2250 | if (chip->label) | |
2251 | seq_printf(s, ", %s", chip->label); | |
2252 | if (chip->can_sleep) | |
2253 | seq_printf(s, ", can sleep"); | |
2254 | seq_printf(s, ":\n"); | |
2255 | ||
2256 | if (chip->dbg_show) | |
2257 | chip->dbg_show(s, chip); | |
2258 | else | |
2259 | gpiolib_dbg_show(s, chip); | |
2260 | ||
d2876d08 DB |
2261 | return 0; |
2262 | } | |
2263 | ||
f9c4a31f TR |
2264 | static const struct seq_operations gpiolib_seq_ops = { |
2265 | .start = gpiolib_seq_start, | |
2266 | .next = gpiolib_seq_next, | |
2267 | .stop = gpiolib_seq_stop, | |
2268 | .show = gpiolib_seq_show, | |
2269 | }; | |
2270 | ||
d2876d08 DB |
2271 | static int gpiolib_open(struct inode *inode, struct file *file) |
2272 | { | |
f9c4a31f | 2273 | return seq_open(file, &gpiolib_seq_ops); |
d2876d08 DB |
2274 | } |
2275 | ||
828c0950 | 2276 | static const struct file_operations gpiolib_operations = { |
f9c4a31f | 2277 | .owner = THIS_MODULE, |
d2876d08 DB |
2278 | .open = gpiolib_open, |
2279 | .read = seq_read, | |
2280 | .llseek = seq_lseek, | |
f9c4a31f | 2281 | .release = seq_release, |
d2876d08 DB |
2282 | }; |
2283 | ||
2284 | static int __init gpiolib_debugfs_init(void) | |
2285 | { | |
2286 | /* /sys/kernel/debug/gpio */ | |
2287 | (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO, | |
2288 | NULL, NULL, &gpiolib_operations); | |
2289 | return 0; | |
2290 | } | |
2291 | subsys_initcall(gpiolib_debugfs_init); | |
2292 | ||
2293 | #endif /* DEBUG_FS */ |