diff options
Diffstat (limited to 'drivers/gpio/gpiolib.c')
-rw-r--r-- | drivers/gpio/gpiolib.c | 911 |
1 files changed, 4 insertions, 907 deletions
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 8ba9dbedcfdf..7b35e5093ef5 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c | |||
@@ -44,94 +44,21 @@ | |||
44 | * While any GPIO is requested, its gpio_chip is not removable; | 44 | * While any GPIO is requested, its gpio_chip is not removable; |
45 | * each GPIO's "requested" flag serves as a lock and refcount. | 45 | * each GPIO's "requested" flag serves as a lock and refcount. |
46 | */ | 46 | */ |
47 | static DEFINE_SPINLOCK(gpio_lock); | 47 | DEFINE_SPINLOCK(gpio_lock); |
48 | 48 | ||
49 | struct gpio_desc { | ||
50 | struct gpio_chip *chip; | ||
51 | unsigned long flags; | ||
52 | /* flag symbols are bit numbers */ | ||
53 | #define FLAG_REQUESTED 0 | ||
54 | #define FLAG_IS_OUT 1 | ||
55 | #define FLAG_EXPORT 2 /* protected by sysfs_lock */ | ||
56 | #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */ | ||
57 | #define FLAG_TRIG_FALL 4 /* trigger on falling edge */ | ||
58 | #define FLAG_TRIG_RISE 5 /* trigger on rising edge */ | ||
59 | #define FLAG_ACTIVE_LOW 6 /* value has active low */ | ||
60 | #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ | ||
61 | #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ | ||
62 | #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ | ||
63 | |||
64 | #define ID_SHIFT 16 /* add new flags before this one */ | ||
65 | |||
66 | #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1) | ||
67 | #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE)) | ||
68 | |||
69 | const char *label; | ||
70 | }; | ||
71 | static struct gpio_desc gpio_desc[ARCH_NR_GPIOS]; | 49 | static struct gpio_desc gpio_desc[ARCH_NR_GPIOS]; |
72 | 50 | ||
73 | #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio) | 51 | #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio) |
74 | 52 | ||
75 | static DEFINE_MUTEX(gpio_lookup_lock); | 53 | static DEFINE_MUTEX(gpio_lookup_lock); |
76 | static LIST_HEAD(gpio_lookup_list); | 54 | static LIST_HEAD(gpio_lookup_list); |
77 | static LIST_HEAD(gpio_chips); | 55 | LIST_HEAD(gpio_chips); |
78 | |||
79 | #ifdef CONFIG_GPIO_SYSFS | ||
80 | static DEFINE_IDR(dirent_idr); | ||
81 | #endif | ||
82 | |||
83 | static int gpiod_request(struct gpio_desc *desc, const char *label); | ||
84 | static void gpiod_free(struct gpio_desc *desc); | ||
85 | |||
86 | /* With descriptor prefix */ | ||
87 | |||
88 | #define gpiod_emerg(desc, fmt, ...) \ | ||
89 | pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ | ||
90 | ##__VA_ARGS__) | ||
91 | #define gpiod_crit(desc, fmt, ...) \ | ||
92 | pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ | ||
93 | ##__VA_ARGS__) | ||
94 | #define gpiod_err(desc, fmt, ...) \ | ||
95 | pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ | ||
96 | ##__VA_ARGS__) | ||
97 | #define gpiod_warn(desc, fmt, ...) \ | ||
98 | pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ | ||
99 | ##__VA_ARGS__) | ||
100 | #define gpiod_info(desc, fmt, ...) \ | ||
101 | pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ | ||
102 | ##__VA_ARGS__) | ||
103 | #define gpiod_dbg(desc, fmt, ...) \ | ||
104 | pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ | ||
105 | ##__VA_ARGS__) | ||
106 | |||
107 | /* With chip prefix */ | ||
108 | |||
109 | #define chip_emerg(chip, fmt, ...) \ | ||
110 | pr_emerg("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) | ||
111 | #define chip_crit(chip, fmt, ...) \ | ||
112 | pr_crit("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) | ||
113 | #define chip_err(chip, fmt, ...) \ | ||
114 | pr_err("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) | ||
115 | #define chip_warn(chip, fmt, ...) \ | ||
116 | pr_warn("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) | ||
117 | #define chip_info(chip, fmt, ...) \ | ||
118 | pr_info("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) | ||
119 | #define chip_dbg(chip, fmt, ...) \ | ||
120 | pr_debug("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) | ||
121 | 56 | ||
122 | static inline void desc_set_label(struct gpio_desc *d, const char *label) | 57 | static inline void desc_set_label(struct gpio_desc *d, const char *label) |
123 | { | 58 | { |
124 | d->label = label; | 59 | d->label = label; |
125 | } | 60 | } |
126 | 61 | ||
127 | /* | ||
128 | * Return the GPIO number of the passed descriptor relative to its chip | ||
129 | */ | ||
130 | static int gpio_chip_hwgpio(const struct gpio_desc *desc) | ||
131 | { | ||
132 | return desc - &desc->chip->desc[0]; | ||
133 | } | ||
134 | |||
135 | /** | 62 | /** |
136 | * Convert a GPIO number to its descriptor | 63 | * Convert a GPIO number to its descriptor |
137 | */ | 64 | */ |
@@ -272,836 +199,6 @@ int gpiod_get_direction(const struct gpio_desc *desc) | |||
272 | } | 199 | } |
273 | EXPORT_SYMBOL_GPL(gpiod_get_direction); | 200 | EXPORT_SYMBOL_GPL(gpiod_get_direction); |
274 | 201 | ||
275 | #ifdef CONFIG_GPIO_SYSFS | ||
276 | |||
277 | /* lock protects against unexport_gpio() being called while | ||
278 | * sysfs files are active. | ||
279 | */ | ||
280 | static DEFINE_MUTEX(sysfs_lock); | ||
281 | |||
282 | /* | ||
283 | * /sys/class/gpio/gpioN... only for GPIOs that are exported | ||
284 | * /direction | ||
285 | * * MAY BE OMITTED if kernel won't allow direction changes | ||
286 | * * is read/write as "in" or "out" | ||
287 | * * may also be written as "high" or "low", initializing | ||
288 | * output value as specified ("out" implies "low") | ||
289 | * /value | ||
290 | * * always readable, subject to hardware behavior | ||
291 | * * may be writable, as zero/nonzero | ||
292 | * /edge | ||
293 | * * configures behavior of poll(2) on /value | ||
294 | * * available only if pin can generate IRQs on input | ||
295 | * * is read/write as "none", "falling", "rising", or "both" | ||
296 | * /active_low | ||
297 | * * configures polarity of /value | ||
298 | * * is read/write as zero/nonzero | ||
299 | * * also affects existing and subsequent "falling" and "rising" | ||
300 | * /edge configuration | ||
301 | */ | ||
302 | |||
303 | static ssize_t gpio_direction_show(struct device *dev, | ||
304 | struct device_attribute *attr, char *buf) | ||
305 | { | ||
306 | const struct gpio_desc *desc = dev_get_drvdata(dev); | ||
307 | ssize_t status; | ||
308 | |||
309 | mutex_lock(&sysfs_lock); | ||
310 | |||
311 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { | ||
312 | status = -EIO; | ||
313 | } else { | ||
314 | gpiod_get_direction(desc); | ||
315 | status = sprintf(buf, "%s\n", | ||
316 | test_bit(FLAG_IS_OUT, &desc->flags) | ||
317 | ? "out" : "in"); | ||
318 | } | ||
319 | |||
320 | mutex_unlock(&sysfs_lock); | ||
321 | return status; | ||
322 | } | ||
323 | |||
324 | static ssize_t gpio_direction_store(struct device *dev, | ||
325 | struct device_attribute *attr, const char *buf, size_t size) | ||
326 | { | ||
327 | struct gpio_desc *desc = dev_get_drvdata(dev); | ||
328 | ssize_t status; | ||
329 | |||
330 | mutex_lock(&sysfs_lock); | ||
331 | |||
332 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | ||
333 | status = -EIO; | ||
334 | else if (sysfs_streq(buf, "high")) | ||
335 | status = gpiod_direction_output_raw(desc, 1); | ||
336 | else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low")) | ||
337 | status = gpiod_direction_output_raw(desc, 0); | ||
338 | else if (sysfs_streq(buf, "in")) | ||
339 | status = gpiod_direction_input(desc); | ||
340 | else | ||
341 | status = -EINVAL; | ||
342 | |||
343 | mutex_unlock(&sysfs_lock); | ||
344 | return status ? : size; | ||
345 | } | ||
346 | |||
347 | static /* const */ DEVICE_ATTR(direction, 0644, | ||
348 | gpio_direction_show, gpio_direction_store); | ||
349 | |||
350 | static ssize_t gpio_value_show(struct device *dev, | ||
351 | struct device_attribute *attr, char *buf) | ||
352 | { | ||
353 | struct gpio_desc *desc = dev_get_drvdata(dev); | ||
354 | ssize_t status; | ||
355 | |||
356 | mutex_lock(&sysfs_lock); | ||
357 | |||
358 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | ||
359 | status = -EIO; | ||
360 | else | ||
361 | status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc)); | ||
362 | |||
363 | mutex_unlock(&sysfs_lock); | ||
364 | return status; | ||
365 | } | ||
366 | |||
367 | static ssize_t gpio_value_store(struct device *dev, | ||
368 | struct device_attribute *attr, const char *buf, size_t size) | ||
369 | { | ||
370 | struct gpio_desc *desc = dev_get_drvdata(dev); | ||
371 | ssize_t status; | ||
372 | |||
373 | mutex_lock(&sysfs_lock); | ||
374 | |||
375 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | ||
376 | status = -EIO; | ||
377 | else if (!test_bit(FLAG_IS_OUT, &desc->flags)) | ||
378 | status = -EPERM; | ||
379 | else { | ||
380 | long value; | ||
381 | |||
382 | status = kstrtol(buf, 0, &value); | ||
383 | if (status == 0) { | ||
384 | gpiod_set_value_cansleep(desc, value); | ||
385 | status = size; | ||
386 | } | ||
387 | } | ||
388 | |||
389 | mutex_unlock(&sysfs_lock); | ||
390 | return status; | ||
391 | } | ||
392 | |||
393 | static const DEVICE_ATTR(value, 0644, | ||
394 | gpio_value_show, gpio_value_store); | ||
395 | |||
396 | static irqreturn_t gpio_sysfs_irq(int irq, void *priv) | ||
397 | { | ||
398 | struct kernfs_node *value_sd = priv; | ||
399 | |||
400 | sysfs_notify_dirent(value_sd); | ||
401 | return IRQ_HANDLED; | ||
402 | } | ||
403 | |||
404 | static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev, | ||
405 | unsigned long gpio_flags) | ||
406 | { | ||
407 | struct kernfs_node *value_sd; | ||
408 | unsigned long irq_flags; | ||
409 | int ret, irq, id; | ||
410 | |||
411 | if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags) | ||
412 | return 0; | ||
413 | |||
414 | irq = gpiod_to_irq(desc); | ||
415 | if (irq < 0) | ||
416 | return -EIO; | ||
417 | |||
418 | id = desc->flags >> ID_SHIFT; | ||
419 | value_sd = idr_find(&dirent_idr, id); | ||
420 | if (value_sd) | ||
421 | free_irq(irq, value_sd); | ||
422 | |||
423 | desc->flags &= ~GPIO_TRIGGER_MASK; | ||
424 | |||
425 | if (!gpio_flags) { | ||
426 | gpiod_unlock_as_irq(desc); | ||
427 | ret = 0; | ||
428 | goto free_id; | ||
429 | } | ||
430 | |||
431 | irq_flags = IRQF_SHARED; | ||
432 | if (test_bit(FLAG_TRIG_FALL, &gpio_flags)) | ||
433 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? | ||
434 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; | ||
435 | if (test_bit(FLAG_TRIG_RISE, &gpio_flags)) | ||
436 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? | ||
437 | IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; | ||
438 | |||
439 | if (!value_sd) { | ||
440 | value_sd = sysfs_get_dirent(dev->kobj.sd, "value"); | ||
441 | if (!value_sd) { | ||
442 | ret = -ENODEV; | ||
443 | goto err_out; | ||
444 | } | ||
445 | |||
446 | ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL); | ||
447 | if (ret < 0) | ||
448 | goto free_sd; | ||
449 | id = ret; | ||
450 | |||
451 | desc->flags &= GPIO_FLAGS_MASK; | ||
452 | desc->flags |= (unsigned long)id << ID_SHIFT; | ||
453 | |||
454 | if (desc->flags >> ID_SHIFT != id) { | ||
455 | ret = -ERANGE; | ||
456 | goto free_id; | ||
457 | } | ||
458 | } | ||
459 | |||
460 | ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags, | ||
461 | "gpiolib", value_sd); | ||
462 | if (ret < 0) | ||
463 | goto free_id; | ||
464 | |||
465 | ret = gpiod_lock_as_irq(desc); | ||
466 | if (ret < 0) { | ||
467 | gpiod_warn(desc, "failed to flag the GPIO for IRQ\n"); | ||
468 | goto free_id; | ||
469 | } | ||
470 | |||
471 | desc->flags |= gpio_flags; | ||
472 | return 0; | ||
473 | |||
474 | free_id: | ||
475 | idr_remove(&dirent_idr, id); | ||
476 | desc->flags &= GPIO_FLAGS_MASK; | ||
477 | free_sd: | ||
478 | if (value_sd) | ||
479 | sysfs_put(value_sd); | ||
480 | err_out: | ||
481 | return ret; | ||
482 | } | ||
483 | |||
484 | static const struct { | ||
485 | const char *name; | ||
486 | unsigned long flags; | ||
487 | } trigger_types[] = { | ||
488 | { "none", 0 }, | ||
489 | { "falling", BIT(FLAG_TRIG_FALL) }, | ||
490 | { "rising", BIT(FLAG_TRIG_RISE) }, | ||
491 | { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) }, | ||
492 | }; | ||
493 | |||
494 | static ssize_t gpio_edge_show(struct device *dev, | ||
495 | struct device_attribute *attr, char *buf) | ||
496 | { | ||
497 | const struct gpio_desc *desc = dev_get_drvdata(dev); | ||
498 | ssize_t status; | ||
499 | |||
500 | mutex_lock(&sysfs_lock); | ||
501 | |||
502 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | ||
503 | status = -EIO; | ||
504 | else { | ||
505 | int i; | ||
506 | |||
507 | status = 0; | ||
508 | for (i = 0; i < ARRAY_SIZE(trigger_types); i++) | ||
509 | if ((desc->flags & GPIO_TRIGGER_MASK) | ||
510 | == trigger_types[i].flags) { | ||
511 | status = sprintf(buf, "%s\n", | ||
512 | trigger_types[i].name); | ||
513 | break; | ||
514 | } | ||
515 | } | ||
516 | |||
517 | mutex_unlock(&sysfs_lock); | ||
518 | return status; | ||
519 | } | ||
520 | |||
521 | static ssize_t gpio_edge_store(struct device *dev, | ||
522 | struct device_attribute *attr, const char *buf, size_t size) | ||
523 | { | ||
524 | struct gpio_desc *desc = dev_get_drvdata(dev); | ||
525 | ssize_t status; | ||
526 | int i; | ||
527 | |||
528 | for (i = 0; i < ARRAY_SIZE(trigger_types); i++) | ||
529 | if (sysfs_streq(trigger_types[i].name, buf)) | ||
530 | goto found; | ||
531 | return -EINVAL; | ||
532 | |||
533 | found: | ||
534 | mutex_lock(&sysfs_lock); | ||
535 | |||
536 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | ||
537 | status = -EIO; | ||
538 | else { | ||
539 | status = gpio_setup_irq(desc, dev, trigger_types[i].flags); | ||
540 | if (!status) | ||
541 | status = size; | ||
542 | } | ||
543 | |||
544 | mutex_unlock(&sysfs_lock); | ||
545 | |||
546 | return status; | ||
547 | } | ||
548 | |||
549 | static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store); | ||
550 | |||
551 | static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev, | ||
552 | int value) | ||
553 | { | ||
554 | int status = 0; | ||
555 | |||
556 | if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value) | ||
557 | return 0; | ||
558 | |||
559 | if (value) | ||
560 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); | ||
561 | else | ||
562 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); | ||
563 | |||
564 | /* reconfigure poll(2) support if enabled on one edge only */ | ||
565 | if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^ | ||
566 | !!test_bit(FLAG_TRIG_FALL, &desc->flags))) { | ||
567 | unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK; | ||
568 | |||
569 | gpio_setup_irq(desc, dev, 0); | ||
570 | status = gpio_setup_irq(desc, dev, trigger_flags); | ||
571 | } | ||
572 | |||
573 | return status; | ||
574 | } | ||
575 | |||
576 | static ssize_t gpio_active_low_show(struct device *dev, | ||
577 | struct device_attribute *attr, char *buf) | ||
578 | { | ||
579 | const struct gpio_desc *desc = dev_get_drvdata(dev); | ||
580 | ssize_t status; | ||
581 | |||
582 | mutex_lock(&sysfs_lock); | ||
583 | |||
584 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | ||
585 | status = -EIO; | ||
586 | else | ||
587 | status = sprintf(buf, "%d\n", | ||
588 | !!test_bit(FLAG_ACTIVE_LOW, &desc->flags)); | ||
589 | |||
590 | mutex_unlock(&sysfs_lock); | ||
591 | |||
592 | return status; | ||
593 | } | ||
594 | |||
595 | static ssize_t gpio_active_low_store(struct device *dev, | ||
596 | struct device_attribute *attr, const char *buf, size_t size) | ||
597 | { | ||
598 | 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 | long value; | ||
607 | |||
608 | status = kstrtol(buf, 0, &value); | ||
609 | if (status == 0) | ||
610 | status = sysfs_set_active_low(desc, dev, value != 0); | ||
611 | } | ||
612 | |||
613 | mutex_unlock(&sysfs_lock); | ||
614 | |||
615 | return status ? : size; | ||
616 | } | ||
617 | |||
618 | static const DEVICE_ATTR(active_low, 0644, | ||
619 | gpio_active_low_show, gpio_active_low_store); | ||
620 | |||
621 | static const struct attribute *gpio_attrs[] = { | ||
622 | &dev_attr_value.attr, | ||
623 | &dev_attr_active_low.attr, | ||
624 | NULL, | ||
625 | }; | ||
626 | |||
627 | static const struct attribute_group gpio_attr_group = { | ||
628 | .attrs = (struct attribute **) gpio_attrs, | ||
629 | }; | ||
630 | |||
631 | /* | ||
632 | * /sys/class/gpio/gpiochipN/ | ||
633 | * /base ... matching gpio_chip.base (N) | ||
634 | * /label ... matching gpio_chip.label | ||
635 | * /ngpio ... matching gpio_chip.ngpio | ||
636 | */ | ||
637 | |||
638 | static ssize_t chip_base_show(struct device *dev, | ||
639 | struct device_attribute *attr, char *buf) | ||
640 | { | ||
641 | const struct gpio_chip *chip = dev_get_drvdata(dev); | ||
642 | |||
643 | return sprintf(buf, "%d\n", chip->base); | ||
644 | } | ||
645 | static DEVICE_ATTR(base, 0444, chip_base_show, NULL); | ||
646 | |||
647 | static ssize_t chip_label_show(struct device *dev, | ||
648 | struct device_attribute *attr, char *buf) | ||
649 | { | ||
650 | const struct gpio_chip *chip = dev_get_drvdata(dev); | ||
651 | |||
652 | return sprintf(buf, "%s\n", chip->label ? : ""); | ||
653 | } | ||
654 | static DEVICE_ATTR(label, 0444, chip_label_show, NULL); | ||
655 | |||
656 | static ssize_t chip_ngpio_show(struct device *dev, | ||
657 | struct device_attribute *attr, char *buf) | ||
658 | { | ||
659 | const struct gpio_chip *chip = dev_get_drvdata(dev); | ||
660 | |||
661 | return sprintf(buf, "%u\n", chip->ngpio); | ||
662 | } | ||
663 | static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL); | ||
664 | |||
665 | static const struct attribute *gpiochip_attrs[] = { | ||
666 | &dev_attr_base.attr, | ||
667 | &dev_attr_label.attr, | ||
668 | &dev_attr_ngpio.attr, | ||
669 | NULL, | ||
670 | }; | ||
671 | |||
672 | static const struct attribute_group gpiochip_attr_group = { | ||
673 | .attrs = (struct attribute **) gpiochip_attrs, | ||
674 | }; | ||
675 | |||
676 | /* | ||
677 | * /sys/class/gpio/export ... write-only | ||
678 | * integer N ... number of GPIO to export (full access) | ||
679 | * /sys/class/gpio/unexport ... write-only | ||
680 | * integer N ... number of GPIO to unexport | ||
681 | */ | ||
682 | static ssize_t export_store(struct class *class, | ||
683 | struct class_attribute *attr, | ||
684 | const char *buf, size_t len) | ||
685 | { | ||
686 | long gpio; | ||
687 | struct gpio_desc *desc; | ||
688 | int status; | ||
689 | |||
690 | status = kstrtol(buf, 0, &gpio); | ||
691 | if (status < 0) | ||
692 | goto done; | ||
693 | |||
694 | desc = gpio_to_desc(gpio); | ||
695 | /* reject invalid GPIOs */ | ||
696 | if (!desc) { | ||
697 | pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); | ||
698 | return -EINVAL; | ||
699 | } | ||
700 | |||
701 | /* No extra locking here; FLAG_SYSFS just signifies that the | ||
702 | * request and export were done by on behalf of userspace, so | ||
703 | * they may be undone on its behalf too. | ||
704 | */ | ||
705 | |||
706 | status = gpiod_request(desc, "sysfs"); | ||
707 | if (status < 0) { | ||
708 | if (status == -EPROBE_DEFER) | ||
709 | status = -ENODEV; | ||
710 | goto done; | ||
711 | } | ||
712 | status = gpiod_export(desc, true); | ||
713 | if (status < 0) | ||
714 | gpiod_free(desc); | ||
715 | else | ||
716 | set_bit(FLAG_SYSFS, &desc->flags); | ||
717 | |||
718 | done: | ||
719 | if (status) | ||
720 | pr_debug("%s: status %d\n", __func__, status); | ||
721 | return status ? : len; | ||
722 | } | ||
723 | |||
724 | static ssize_t unexport_store(struct class *class, | ||
725 | struct class_attribute *attr, | ||
726 | const char *buf, size_t len) | ||
727 | { | ||
728 | long gpio; | ||
729 | struct gpio_desc *desc; | ||
730 | int status; | ||
731 | |||
732 | status = kstrtol(buf, 0, &gpio); | ||
733 | if (status < 0) | ||
734 | goto done; | ||
735 | |||
736 | desc = gpio_to_desc(gpio); | ||
737 | /* reject bogus commands (gpio_unexport ignores them) */ | ||
738 | if (!desc) { | ||
739 | pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); | ||
740 | return -EINVAL; | ||
741 | } | ||
742 | |||
743 | status = -EINVAL; | ||
744 | |||
745 | /* No extra locking here; FLAG_SYSFS just signifies that the | ||
746 | * request and export were done by on behalf of userspace, so | ||
747 | * they may be undone on its behalf too. | ||
748 | */ | ||
749 | if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) { | ||
750 | status = 0; | ||
751 | gpiod_free(desc); | ||
752 | } | ||
753 | done: | ||
754 | if (status) | ||
755 | pr_debug("%s: status %d\n", __func__, status); | ||
756 | return status ? : len; | ||
757 | } | ||
758 | |||
759 | static struct class_attribute gpio_class_attrs[] = { | ||
760 | __ATTR(export, 0200, NULL, export_store), | ||
761 | __ATTR(unexport, 0200, NULL, unexport_store), | ||
762 | __ATTR_NULL, | ||
763 | }; | ||
764 | |||
765 | static struct class gpio_class = { | ||
766 | .name = "gpio", | ||
767 | .owner = THIS_MODULE, | ||
768 | |||
769 | .class_attrs = gpio_class_attrs, | ||
770 | }; | ||
771 | |||
772 | |||
773 | /** | ||
774 | * gpiod_export - export a GPIO through sysfs | ||
775 | * @gpio: gpio to make available, already requested | ||
776 | * @direction_may_change: true if userspace may change gpio direction | ||
777 | * Context: arch_initcall or later | ||
778 | * | ||
779 | * When drivers want to make a GPIO accessible to userspace after they | ||
780 | * have requested it -- perhaps while debugging, or as part of their | ||
781 | * public interface -- they may use this routine. If the GPIO can | ||
782 | * change direction (some can't) and the caller allows it, userspace | ||
783 | * will see "direction" sysfs attribute which may be used to change | ||
784 | * the gpio's direction. A "value" attribute will always be provided. | ||
785 | * | ||
786 | * Returns zero on success, else an error. | ||
787 | */ | ||
788 | int gpiod_export(struct gpio_desc *desc, bool direction_may_change) | ||
789 | { | ||
790 | unsigned long flags; | ||
791 | int status; | ||
792 | const char *ioname = NULL; | ||
793 | struct device *dev; | ||
794 | int offset; | ||
795 | |||
796 | /* can't export until sysfs is available ... */ | ||
797 | if (!gpio_class.p) { | ||
798 | pr_debug("%s: called too early!\n", __func__); | ||
799 | return -ENOENT; | ||
800 | } | ||
801 | |||
802 | if (!desc) { | ||
803 | pr_debug("%s: invalid gpio descriptor\n", __func__); | ||
804 | return -EINVAL; | ||
805 | } | ||
806 | |||
807 | mutex_lock(&sysfs_lock); | ||
808 | |||
809 | spin_lock_irqsave(&gpio_lock, flags); | ||
810 | if (!test_bit(FLAG_REQUESTED, &desc->flags) || | ||
811 | test_bit(FLAG_EXPORT, &desc->flags)) { | ||
812 | spin_unlock_irqrestore(&gpio_lock, flags); | ||
813 | gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n", | ||
814 | __func__, | ||
815 | test_bit(FLAG_REQUESTED, &desc->flags), | ||
816 | test_bit(FLAG_EXPORT, &desc->flags)); | ||
817 | status = -EPERM; | ||
818 | goto fail_unlock; | ||
819 | } | ||
820 | |||
821 | if (!desc->chip->direction_input || !desc->chip->direction_output) | ||
822 | direction_may_change = false; | ||
823 | spin_unlock_irqrestore(&gpio_lock, flags); | ||
824 | |||
825 | offset = gpio_chip_hwgpio(desc); | ||
826 | if (desc->chip->names && desc->chip->names[offset]) | ||
827 | ioname = desc->chip->names[offset]; | ||
828 | |||
829 | dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0), | ||
830 | desc, ioname ? ioname : "gpio%u", | ||
831 | desc_to_gpio(desc)); | ||
832 | if (IS_ERR(dev)) { | ||
833 | status = PTR_ERR(dev); | ||
834 | goto fail_unlock; | ||
835 | } | ||
836 | |||
837 | status = sysfs_create_group(&dev->kobj, &gpio_attr_group); | ||
838 | if (status) | ||
839 | goto fail_unregister_device; | ||
840 | |||
841 | if (direction_may_change) { | ||
842 | status = device_create_file(dev, &dev_attr_direction); | ||
843 | if (status) | ||
844 | goto fail_unregister_device; | ||
845 | } | ||
846 | |||
847 | if (gpiod_to_irq(desc) >= 0 && (direction_may_change || | ||
848 | !test_bit(FLAG_IS_OUT, &desc->flags))) { | ||
849 | status = device_create_file(dev, &dev_attr_edge); | ||
850 | if (status) | ||
851 | goto fail_unregister_device; | ||
852 | } | ||
853 | |||
854 | set_bit(FLAG_EXPORT, &desc->flags); | ||
855 | mutex_unlock(&sysfs_lock); | ||
856 | return 0; | ||
857 | |||
858 | fail_unregister_device: | ||
859 | device_unregister(dev); | ||
860 | fail_unlock: | ||
861 | mutex_unlock(&sysfs_lock); | ||
862 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); | ||
863 | return status; | ||
864 | } | ||
865 | EXPORT_SYMBOL_GPL(gpiod_export); | ||
866 | |||
867 | static int match_export(struct device *dev, const void *data) | ||
868 | { | ||
869 | return dev_get_drvdata(dev) == data; | ||
870 | } | ||
871 | |||
872 | /** | ||
873 | * gpiod_export_link - create a sysfs link to an exported GPIO node | ||
874 | * @dev: device under which to create symlink | ||
875 | * @name: name of the symlink | ||
876 | * @gpio: gpio to create symlink to, already exported | ||
877 | * | ||
878 | * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN | ||
879 | * node. Caller is responsible for unlinking. | ||
880 | * | ||
881 | * Returns zero on success, else an error. | ||
882 | */ | ||
883 | int gpiod_export_link(struct device *dev, const char *name, | ||
884 | struct gpio_desc *desc) | ||
885 | { | ||
886 | int status = -EINVAL; | ||
887 | |||
888 | if (!desc) { | ||
889 | pr_warn("%s: invalid GPIO\n", __func__); | ||
890 | return -EINVAL; | ||
891 | } | ||
892 | |||
893 | mutex_lock(&sysfs_lock); | ||
894 | |||
895 | if (test_bit(FLAG_EXPORT, &desc->flags)) { | ||
896 | struct device *tdev; | ||
897 | |||
898 | tdev = class_find_device(&gpio_class, NULL, desc, match_export); | ||
899 | if (tdev != NULL) { | ||
900 | status = sysfs_create_link(&dev->kobj, &tdev->kobj, | ||
901 | name); | ||
902 | } else { | ||
903 | status = -ENODEV; | ||
904 | } | ||
905 | } | ||
906 | |||
907 | mutex_unlock(&sysfs_lock); | ||
908 | |||
909 | if (status) | ||
910 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); | ||
911 | |||
912 | return status; | ||
913 | } | ||
914 | EXPORT_SYMBOL_GPL(gpiod_export_link); | ||
915 | |||
916 | /** | ||
917 | * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value | ||
918 | * @gpio: gpio to change | ||
919 | * @value: non-zero to use active low, i.e. inverted values | ||
920 | * | ||
921 | * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute. | ||
922 | * The GPIO does not have to be exported yet. If poll(2) support has | ||
923 | * been enabled for either rising or falling edge, it will be | ||
924 | * reconfigured to follow the new polarity. | ||
925 | * | ||
926 | * Returns zero on success, else an error. | ||
927 | */ | ||
928 | int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) | ||
929 | { | ||
930 | struct device *dev = NULL; | ||
931 | int status = -EINVAL; | ||
932 | |||
933 | if (!desc) { | ||
934 | pr_warn("%s: invalid GPIO\n", __func__); | ||
935 | return -EINVAL; | ||
936 | } | ||
937 | |||
938 | mutex_lock(&sysfs_lock); | ||
939 | |||
940 | if (test_bit(FLAG_EXPORT, &desc->flags)) { | ||
941 | dev = class_find_device(&gpio_class, NULL, desc, match_export); | ||
942 | if (dev == NULL) { | ||
943 | status = -ENODEV; | ||
944 | goto unlock; | ||
945 | } | ||
946 | } | ||
947 | |||
948 | status = sysfs_set_active_low(desc, dev, value); | ||
949 | |||
950 | unlock: | ||
951 | mutex_unlock(&sysfs_lock); | ||
952 | |||
953 | if (status) | ||
954 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); | ||
955 | |||
956 | return status; | ||
957 | } | ||
958 | EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low); | ||
959 | |||
960 | /** | ||
961 | * gpiod_unexport - reverse effect of gpio_export() | ||
962 | * @gpio: gpio to make unavailable | ||
963 | * | ||
964 | * This is implicit on gpio_free(). | ||
965 | */ | ||
966 | void gpiod_unexport(struct gpio_desc *desc) | ||
967 | { | ||
968 | int status = 0; | ||
969 | struct device *dev = NULL; | ||
970 | |||
971 | if (!desc) { | ||
972 | pr_warn("%s: invalid GPIO\n", __func__); | ||
973 | return; | ||
974 | } | ||
975 | |||
976 | mutex_lock(&sysfs_lock); | ||
977 | |||
978 | if (test_bit(FLAG_EXPORT, &desc->flags)) { | ||
979 | |||
980 | dev = class_find_device(&gpio_class, NULL, desc, match_export); | ||
981 | if (dev) { | ||
982 | gpio_setup_irq(desc, dev, 0); | ||
983 | clear_bit(FLAG_EXPORT, &desc->flags); | ||
984 | } else | ||
985 | status = -ENODEV; | ||
986 | } | ||
987 | |||
988 | mutex_unlock(&sysfs_lock); | ||
989 | |||
990 | if (dev) { | ||
991 | device_unregister(dev); | ||
992 | put_device(dev); | ||
993 | } | ||
994 | |||
995 | if (status) | ||
996 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); | ||
997 | } | ||
998 | EXPORT_SYMBOL_GPL(gpiod_unexport); | ||
999 | |||
1000 | static int gpiochip_export(struct gpio_chip *chip) | ||
1001 | { | ||
1002 | int status; | ||
1003 | struct device *dev; | ||
1004 | |||
1005 | /* Many systems register gpio chips for SOC support very early, | ||
1006 | * before driver model support is available. In those cases we | ||
1007 | * export this later, in gpiolib_sysfs_init() ... here we just | ||
1008 | * verify that _some_ field of gpio_class got initialized. | ||
1009 | */ | ||
1010 | if (!gpio_class.p) | ||
1011 | return 0; | ||
1012 | |||
1013 | /* use chip->base for the ID; it's already known to be unique */ | ||
1014 | mutex_lock(&sysfs_lock); | ||
1015 | dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip, | ||
1016 | "gpiochip%d", chip->base); | ||
1017 | if (!IS_ERR(dev)) { | ||
1018 | status = sysfs_create_group(&dev->kobj, | ||
1019 | &gpiochip_attr_group); | ||
1020 | } else | ||
1021 | status = PTR_ERR(dev); | ||
1022 | chip->exported = (status == 0); | ||
1023 | mutex_unlock(&sysfs_lock); | ||
1024 | |||
1025 | if (status) { | ||
1026 | unsigned long flags; | ||
1027 | unsigned gpio; | ||
1028 | |||
1029 | spin_lock_irqsave(&gpio_lock, flags); | ||
1030 | gpio = 0; | ||
1031 | while (gpio < chip->ngpio) | ||
1032 | chip->desc[gpio++].chip = NULL; | ||
1033 | spin_unlock_irqrestore(&gpio_lock, flags); | ||
1034 | |||
1035 | chip_dbg(chip, "%s: status %d\n", __func__, status); | ||
1036 | } | ||
1037 | |||
1038 | return status; | ||
1039 | } | ||
1040 | |||
1041 | static void gpiochip_unexport(struct gpio_chip *chip) | ||
1042 | { | ||
1043 | int status; | ||
1044 | struct device *dev; | ||
1045 | |||
1046 | mutex_lock(&sysfs_lock); | ||
1047 | dev = class_find_device(&gpio_class, NULL, chip, match_export); | ||
1048 | if (dev) { | ||
1049 | put_device(dev); | ||
1050 | device_unregister(dev); | ||
1051 | chip->exported = false; | ||
1052 | status = 0; | ||
1053 | } else | ||
1054 | status = -ENODEV; | ||
1055 | mutex_unlock(&sysfs_lock); | ||
1056 | |||
1057 | if (status) | ||
1058 | chip_dbg(chip, "%s: status %d\n", __func__, status); | ||
1059 | } | ||
1060 | |||
1061 | static int __init gpiolib_sysfs_init(void) | ||
1062 | { | ||
1063 | int status; | ||
1064 | unsigned long flags; | ||
1065 | struct gpio_chip *chip; | ||
1066 | |||
1067 | status = class_register(&gpio_class); | ||
1068 | if (status < 0) | ||
1069 | return status; | ||
1070 | |||
1071 | /* Scan and register the gpio_chips which registered very | ||
1072 | * early (e.g. before the class_register above was called). | ||
1073 | * | ||
1074 | * We run before arch_initcall() so chip->dev nodes can have | ||
1075 | * registered, and so arch_initcall() can always gpio_export(). | ||
1076 | */ | ||
1077 | spin_lock_irqsave(&gpio_lock, flags); | ||
1078 | list_for_each_entry(chip, &gpio_chips, list) { | ||
1079 | if (!chip || chip->exported) | ||
1080 | continue; | ||
1081 | |||
1082 | spin_unlock_irqrestore(&gpio_lock, flags); | ||
1083 | status = gpiochip_export(chip); | ||
1084 | spin_lock_irqsave(&gpio_lock, flags); | ||
1085 | } | ||
1086 | spin_unlock_irqrestore(&gpio_lock, flags); | ||
1087 | |||
1088 | |||
1089 | return status; | ||
1090 | } | ||
1091 | postcore_initcall(gpiolib_sysfs_init); | ||
1092 | |||
1093 | #else | ||
1094 | static inline int gpiochip_export(struct gpio_chip *chip) | ||
1095 | { | ||
1096 | return 0; | ||
1097 | } | ||
1098 | |||
1099 | static inline void gpiochip_unexport(struct gpio_chip *chip) | ||
1100 | { | ||
1101 | } | ||
1102 | |||
1103 | #endif /* CONFIG_GPIO_SYSFS */ | ||
1104 | |||
1105 | /* | 202 | /* |
1106 | * Add a new chip to the global chips list, keeping the list of chips sorted | 203 | * Add a new chip to the global chips list, keeping the list of chips sorted |
1107 | * by base order. | 204 | * by base order. |
@@ -1721,7 +818,7 @@ done: | |||
1721 | return status; | 818 | return status; |
1722 | } | 819 | } |
1723 | 820 | ||
1724 | static int gpiod_request(struct gpio_desc *desc, const char *label) | 821 | int gpiod_request(struct gpio_desc *desc, const char *label) |
1725 | { | 822 | { |
1726 | int status = -EPROBE_DEFER; | 823 | int status = -EPROBE_DEFER; |
1727 | struct gpio_chip *chip; | 824 | struct gpio_chip *chip; |
@@ -1786,7 +883,7 @@ static bool __gpiod_free(struct gpio_desc *desc) | |||
1786 | return ret; | 883 | return ret; |
1787 | } | 884 | } |
1788 | 885 | ||
1789 | static void gpiod_free(struct gpio_desc *desc) | 886 | void gpiod_free(struct gpio_desc *desc) |
1790 | { | 887 | { |
1791 | if (desc && __gpiod_free(desc)) | 888 | if (desc && __gpiod_free(desc)) |
1792 | module_put(desc->chip->owner); | 889 | module_put(desc->chip->owner); |