1 #include <linux/kernel.h>
2 #include <linux/module.h>
6 #include <linux/device.h>
13 #include <linux/slab.h>
15 #define CREATE_TRACE_POINTS
38 #define extra_checks 1
40 #define extra_checks 0
53 #define FLAG_REQUESTED 0
55 #define FLAG_RESERVED 2
58 #define FLAG_TRIG_FALL 5
59 #define FLAG_TRIG_RISE 6
60 #define FLAG_ACTIVE_LOW 7
61 #define FLAG_OPEN_DRAIN 8
62 #define FLAG_OPEN_SOURCE 9
66 #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
67 #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
69 #ifdef CONFIG_DEBUG_FS
75 #ifdef CONFIG_GPIO_SYSFS
79 static inline void desc_set_label(
struct gpio_desc *
d,
const char *
label)
81 #ifdef CONFIG_DEBUG_FS
99 const struct gpio_chip *
chip = desc->
chip;
103 "autorequest GPIO-%d\n", gpio)) {
104 if (!try_module_get(chip->owner)) {
105 pr_err(
"GPIO-%d: module can't be gotten \n", gpio);
110 desc_set_label(desc,
"[auto]");
125 static int gpiochip_find_base(
int ngpio)
133 struct gpio_chip *chip = desc->
chip;
137 if (spare == ngpio) {
144 i -= chip->ngpio - 1;
148 if (gpio_is_valid(base))
149 pr_debug(
"%s: found new base at %d\n", __func__, base);
170 if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1))
175 for (i = start; i < start + ngpio; i++) {
186 pr_debug(
"%s: reserved gpios from %d to %d\n",
187 __func__, start, start + ngpio - 1);
189 spin_unlock_irqrestore(&gpio_lock, flags);
194 #ifdef CONFIG_GPIO_SYSFS
262 return status ? :
size;
266 gpio_direction_show, gpio_direction_store);
272 unsigned gpio = desc - gpio_desc;
286 status =
sprintf(buf,
"%d\n", value);
297 unsigned gpio = desc - gpio_desc;
323 gpio_value_show, gpio_value_store);
333 static int gpio_setup_irq(
struct gpio_desc *desc,
struct device *dev,
334 unsigned long gpio_flags)
337 unsigned long irq_flags;
348 value_sd =
idr_find(&dirent_idr,
id);
394 "gpiolib", value_sd);
398 desc->
flags |= gpio_flags;
411 static const struct {
414 } trigger_types[] = {
435 for (i = 0; i <
ARRAY_SIZE(trigger_types); i++)
437 == trigger_types[
i].flags) {
439 trigger_types[i].
name);
455 for (i = 0; i <
ARRAY_SIZE(trigger_types); i++)
466 status = gpio_setup_irq(desc, dev, trigger_types[i].
flags);
476 static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
478 static int sysfs_set_active_low(
struct gpio_desc *desc,
struct device *dev,
496 gpio_setup_irq(desc, dev, 0);
497 status = gpio_setup_irq(desc, dev, trigger_flags);
537 status = sysfs_set_active_low(desc, dev, value != 0);
542 return status ? :
size;
546 gpio_active_low_show, gpio_active_low_store);
548 static const struct attribute *gpio_attrs[] = {
549 &dev_attr_value.attr,
550 &dev_attr_active_low.attr,
570 return sprintf(buf,
"%d\n", chip->base);
579 return sprintf(buf,
"%s\n", chip->label ? :
"");
588 return sprintf(buf,
"%u\n", chip->ngpio);
592 static const struct attribute *gpiochip_attrs[] = {
594 &dev_attr_label.attr,
595 &dev_attr_ngpio.attr,
609 static ssize_t export_store(
struct class *
class,
611 const char *buf,
size_t len)
631 status = gpio_export(gpio,
true);
639 pr_debug(
"%s: status %d\n", __func__, status);
640 return status ? : len;
643 static ssize_t unexport_store(
struct class *
class,
645 const char *buf,
size_t len)
657 if (!gpio_is_valid(gpio))
670 pr_debug(
"%s: status %d\n", __func__, status);
671 return status ? : len;
680 static struct class gpio_class = {
684 .class_attrs = gpio_class_attrs,
703 int gpio_export(
unsigned gpio,
bool direction_may_change)
706 struct gpio_desc *desc;
708 const char *ioname =
NULL;
712 pr_debug(
"%s: called too early!\n", __func__);
716 if (!gpio_is_valid(gpio))
722 desc = &gpio_desc[
gpio];
726 if (!desc->
chip->direction_input
727 || !desc->
chip->direction_output)
728 direction_may_change =
false;
730 spin_unlock_irqrestore(&gpio_lock, flags);
732 if (desc->
chip->names && desc->
chip->names[gpio - desc->
chip->base])
733 ioname = desc->
chip->names[gpio - desc->
chip->base];
739 desc, ioname ? ioname :
"gpio%u", gpio);
744 if (!status && direction_may_change)
746 &dev_attr_direction);
749 && (direction_may_change
758 status = PTR_ERR(dev);
767 pr_debug(
"%s: gpio%d status %d\n", __func__, gpio, status);
773 static int match_export(
struct device *dev,
void *
data)
789 int gpio_export_link(
struct device *dev,
const char *name,
unsigned gpio)
791 struct gpio_desc *desc;
794 if (!gpio_is_valid(gpio))
799 desc = &gpio_desc[
gpio];
817 pr_debug(
"%s: gpio%d status %d\n", __func__, gpio, status);
836 int gpio_sysfs_set_active_low(
unsigned gpio,
int value)
838 struct gpio_desc *desc;
842 if (!gpio_is_valid(gpio))
847 desc = &gpio_desc[
gpio];
857 status = sysfs_set_active_low(desc, dev, value);
864 pr_debug(
"%s: gpio%d status %d\n", __func__, gpio, status);
876 void gpio_unexport(
unsigned gpio)
878 struct gpio_desc *desc;
882 if (!gpio_is_valid(gpio)) {
889 desc = &gpio_desc[
gpio];
895 gpio_setup_irq(desc, dev, 0);
908 pr_debug(
"%s: gpio%d status %d\n", __func__, gpio, status);
912 static int gpiochip_export(
struct gpio_chip *chip)
928 "gpiochip%d", chip->base);
931 &gpiochip_attr_group);
933 status = PTR_ERR(dev);
934 chip->exported = (status == 0);
943 while (gpio_desc[gpio].chip == chip)
944 gpio_desc[gpio++].chip =
NULL;
945 spin_unlock_irqrestore(&gpio_lock, flags);
947 pr_debug(
"%s: chip %s status %d\n", __func__,
948 chip->label, status);
954 static void gpiochip_unexport(
struct gpio_chip *chip)
971 pr_debug(
"%s: chip %s status %d\n", __func__,
972 chip->label, status);
975 static int __init gpiolib_sysfs_init(
void)
993 struct gpio_chip *
chip;
995 chip = gpio_desc[
gpio].chip;
996 if (!chip || chip->exported)
999 spin_unlock_irqrestore(&gpio_lock, flags);
1000 status = gpiochip_export(chip);
1003 spin_unlock_irqrestore(&gpio_lock, flags);
1011 static inline int gpiochip_export(
struct gpio_chip *chip)
1016 static inline void gpiochip_unexport(
struct gpio_chip *chip)
1041 unsigned long flags;
1044 int base = chip->base;
1046 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
1055 base = gpiochip_find_base(chip->ngpio);
1064 for (
id = base;
id < base + chip->ngpio;
id++) {
1065 if (gpio_desc[
id].chip !=
NULL) {
1071 for (
id = base;
id < base + chip->ngpio;
id++) {
1072 gpio_desc[
id].chip =
chip;
1080 gpio_desc[
id].
flags = !chip->direction_input
1089 spin_unlock_irqrestore(&gpio_lock, flags);
1094 status = gpiochip_export(chip);
1098 pr_debug(
"gpiochip_add: registered GPIOs %d to %d on device: %s\n",
1099 chip->base, chip->base + chip->ngpio - 1,
1100 chip->label ? :
"generic");
1105 pr_err(
"gpiochip_add: gpios %d..%d (%s) failed to register\n",
1106 chip->base, chip->base + chip->ngpio - 1,
1107 chip->label ? :
"generic");
1120 unsigned long flags;
1128 for (
id = chip->base;
id < chip->base + chip->ngpio;
id++) {
1135 for (
id = chip->base;
id < chip->base + chip->ngpio;
id++)
1136 gpio_desc[
id].chip =
NULL;
1139 spin_unlock_irqrestore(&gpio_lock, flags);
1142 gpiochip_unexport(chip);
1160 int (*
match)(
struct gpio_chip *chip,
1163 struct gpio_chip *chip =
NULL;
1164 unsigned long flags;
1169 if (!gpio_desc[i].chip)
1172 if (
match(gpio_desc[i].chip, data)) {
1173 chip = gpio_desc[
i].chip;
1177 spin_unlock_irqrestore(&gpio_lock, flags);
1189 struct gpio_desc *desc;
1190 struct gpio_chip *
chip;
1192 unsigned long flags;
1196 if (!gpio_is_valid(gpio)) {
1200 desc = &gpio_desc[
gpio];
1205 if (!try_module_get(chip->owner))
1213 desc_set_label(desc, label ? :
"?");
1217 module_put(chip->owner);
1221 if (chip->request) {
1223 spin_unlock_irqrestore(&gpio_lock, flags);
1224 status = chip->request(chip, gpio - chip->base);
1228 desc_set_label(desc,
NULL);
1229 module_put(chip->owner);
1236 pr_debug(
"gpio_request: gpio-%d (%s) status %d\n",
1237 gpio, label ? :
"?", status);
1238 spin_unlock_irqrestore(&gpio_lock, flags);
1245 unsigned long flags;
1246 struct gpio_desc *desc;
1247 struct gpio_chip *
chip;
1251 if (!gpio_is_valid(gpio)) {
1256 gpio_unexport(gpio);
1260 desc = &gpio_desc[
gpio];
1264 spin_unlock_irqrestore(&gpio_lock, flags);
1266 chip->free(chip, gpio - chip->base);
1269 desc_set_label(desc,
NULL);
1270 module_put(desc->
chip->owner);
1278 spin_unlock_irqrestore(&gpio_lock, flags);
1334 for (i = 0; i < num; i++, array++) {
1375 unsigned gpio = chip->base +
offset;
1377 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
1381 #ifdef CONFIG_DEBUG_FS
1382 return gpio_desc[
gpio].label;
1401 unsigned long flags;
1402 struct gpio_chip *
chip;
1403 struct gpio_desc *desc = &gpio_desc[
gpio];
1408 if (!gpio_is_valid(gpio))
1411 if (!chip || !chip->get || !chip->direction_input)
1414 if (gpio >= chip->ngpio)
1416 status = gpio_ensure_requested(desc, gpio);
1422 spin_unlock_irqrestore(&gpio_lock, flags);
1427 status = chip->request(chip, gpio);
1429 pr_debug(
"GPIO-%d: chip request fail, %d\n",
1430 chip->base + gpio, status);
1438 status = chip->direction_input(chip, gpio);
1442 trace_gpio_direction(chip->base + gpio, 1, status);
1446 spin_unlock_irqrestore(&gpio_lock, flags);
1448 pr_debug(
"%s: gpio-%d status %d\n",
1449 __func__, gpio, status);
1456 unsigned long flags;
1457 struct gpio_chip *
chip;
1458 struct gpio_desc *desc = &gpio_desc[
gpio];
1471 if (!gpio_is_valid(gpio))
1474 if (!chip || !chip->set || !chip->direction_output)
1477 if (gpio >= chip->ngpio)
1479 status = gpio_ensure_requested(desc, gpio);
1485 spin_unlock_irqrestore(&gpio_lock, flags);
1490 status = chip->request(chip, gpio);
1492 pr_debug(
"GPIO-%d: chip request fail, %d\n",
1493 chip->base + gpio, status);
1501 status = chip->direction_output(chip, gpio, value);
1504 trace_gpio_value(chip->base + gpio, 0, value);
1505 trace_gpio_direction(chip->base + gpio, 0, status);
1509 spin_unlock_irqrestore(&gpio_lock, flags);
1511 pr_debug(
"%s: gpio-%d status %d\n",
1512 __func__, gpio, status);
1524 unsigned long flags;
1525 struct gpio_chip *
chip;
1526 struct gpio_desc *desc = &gpio_desc[
gpio];
1531 if (!gpio_is_valid(gpio))
1534 if (!chip || !chip->set || !chip->set_debounce)
1537 if (gpio >= chip->ngpio)
1539 status = gpio_ensure_requested(desc, gpio);
1545 spin_unlock_irqrestore(&gpio_lock, flags);
1549 return chip->set_debounce(chip, gpio, debounce);
1552 spin_unlock_irqrestore(&gpio_lock, flags);
1554 pr_debug(
"%s: gpio-%d status %d\n",
1555 __func__, gpio, status);
1594 struct gpio_chip *
chip;
1600 value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
1601 trace_gpio_value(gpio, 1, value);
1612 static void _gpio_set_open_drain_value(
unsigned gpio,
1613 struct gpio_chip *chip,
int value)
1617 err = chip->direction_input(chip, gpio - chip->base);
1621 err = chip->direction_output(chip, gpio - chip->base, 0);
1625 trace_gpio_direction(gpio, value, err);
1627 pr_err(
"%s: Error in set_value for open drain gpio%d err %d\n",
1628 __func__, gpio, err);
1637 static void _gpio_set_open_source_value(
unsigned gpio,
1638 struct gpio_chip *chip,
int value)
1642 err = chip->direction_output(chip, gpio - chip->base, 1);
1646 err = chip->direction_input(chip, gpio - chip->base);
1650 trace_gpio_direction(gpio, !value, err);
1652 pr_err(
"%s: Error in set_value for open source gpio%d err %d\n",
1653 __func__, gpio, err);
1668 struct gpio_chip *
chip;
1673 trace_gpio_value(gpio, 0, value);
1675 _gpio_set_open_drain_value(gpio, chip, value);
1677 _gpio_set_open_source_value(gpio, chip, value);
1679 chip->set(chip, gpio - chip->base, value);
1693 struct gpio_chip *
chip;
1698 return chip->can_sleep;
1713 struct gpio_chip *
chip;
1716 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -
ENXIO;
1728 struct gpio_chip *
chip;
1733 value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
1734 trace_gpio_value(gpio, 1, value);
1741 struct gpio_chip *
chip;
1745 trace_gpio_value(gpio, 0, value);
1747 _gpio_set_open_drain_value(gpio, chip, value);
1749 _gpio_set_open_source_value(gpio, chip, value);
1751 chip->set(chip, gpio - chip->base, value);
1756 #ifdef CONFIG_DEBUG_FS
1758 static void gpiolib_dbg_show(
struct seq_file *
s,
struct gpio_chip *chip)
1761 unsigned gpio = chip->base;
1762 struct gpio_desc *gdesc = &gpio_desc[
gpio];
1765 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1772 is_out ?
"out" :
"in ",
1774 ? (chip->get(chip, i) ?
"hi" :
"lo")
1780 static void *gpiolib_seq_start(
struct seq_file *s, loff_t *
pos)
1782 struct gpio_chip *chip =
NULL;
1789 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
1790 if (gpio_desc[gpio].chip == chip)
1793 chip = gpio_desc[
gpio].chip;
1797 if (index++ >= *pos) {
1808 static void *gpiolib_seq_next(
struct seq_file *s,
void *
v, loff_t *pos)
1810 struct gpio_chip *chip =
v;
1815 for (gpio = chip->base + chip->ngpio; gpio_is_valid(gpio); gpio++) {
1816 chip = gpio_desc[
gpio].chip;
1829 static void gpiolib_seq_stop(
struct seq_file *s,
void *v)
1833 static int gpiolib_seq_show(
struct seq_file *s,
void *v)
1835 struct gpio_chip *chip =
v;
1839 chip->base, chip->base + chip->ngpio - 1);
1846 if (chip->can_sleep)
1851 chip->dbg_show(s, chip);
1853 gpiolib_dbg_show(s, chip);
1859 .
start = gpiolib_seq_start,
1860 .next = gpiolib_seq_next,
1861 .stop = gpiolib_seq_stop,
1862 .show = gpiolib_seq_show,
1867 return seq_open(file, &gpiolib_seq_ops);
1872 .open = gpiolib_open,
1878 static int __init gpiolib_debugfs_init(
void)