diff options
| -rw-r--r-- | drivers/gpio/gpiolib.c | 422 | ||||
| -rw-r--r-- | include/asm-generic/gpio.h | 222 | ||||
| -rw-r--r-- | include/linux/gpio.h | 11 | ||||
| -rw-r--r-- | include/linux/gpio/consumer.h | 238 | ||||
| -rw-r--r-- | include/linux/gpio/driver.h | 127 |
5 files changed, 658 insertions, 362 deletions
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index d66139dc410d..224abdc4b095 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c | |||
| @@ -16,16 +16,11 @@ | |||
| 16 | #define CREATE_TRACE_POINTS | 16 | #define CREATE_TRACE_POINTS |
| 17 | #include <trace/events/gpio.h> | 17 | #include <trace/events/gpio.h> |
| 18 | 18 | ||
| 19 | /* Optional implementation infrastructure for GPIO interfaces. | 19 | /* Implementation infrastructure for GPIO interfaces. |
| 20 | * | 20 | * |
| 21 | * Platforms may want to use this if they tend to use very many GPIOs | 21 | * The GPIO programming interface allows for inlining speed-critical |
| 22 | * that aren't part of a System-On-Chip core; or across I2C/SPI/etc. | 22 | * get/set operations for common cases, so that access to SOC-integrated |
| 23 | * | 23 | * GPIOs can sometimes cost only an instruction or two per bit. |
| 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 | */ | 24 | */ |
| 30 | 25 | ||
| 31 | 26 | ||
| @@ -57,7 +52,7 @@ struct gpio_desc { | |||
| 57 | #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */ | 52 | #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */ |
| 58 | #define FLAG_TRIG_FALL 4 /* trigger on falling edge */ | 53 | #define FLAG_TRIG_FALL 4 /* trigger on falling edge */ |
| 59 | #define FLAG_TRIG_RISE 5 /* trigger on rising edge */ | 54 | #define FLAG_TRIG_RISE 5 /* trigger on rising edge */ |
| 60 | #define FLAG_ACTIVE_LOW 6 /* sysfs value has active low */ | 55 | #define FLAG_ACTIVE_LOW 6 /* value has active low */ |
| 61 | #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ | 56 | #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ |
| 62 | #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ | 57 | #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ |
| 63 | #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ | 58 | #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ |
| @@ -81,29 +76,8 @@ static LIST_HEAD(gpio_chips); | |||
| 81 | static DEFINE_IDR(dirent_idr); | 76 | static DEFINE_IDR(dirent_idr); |
| 82 | #endif | 77 | #endif |
| 83 | 78 | ||
| 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); | 79 | static int gpiod_request(struct gpio_desc *desc, const char *label); |
| 89 | static void gpiod_free(struct gpio_desc *desc); | 80 | 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); | ||
| 92 | static int gpiod_get_direction(const struct gpio_desc *desc); | ||
| 93 | static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce); | ||
| 94 | static int gpiod_get_value_cansleep(const struct gpio_desc *desc); | ||
| 95 | static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value); | ||
| 96 | static int gpiod_get_value(const struct gpio_desc *desc); | ||
| 97 | static void gpiod_set_value(struct gpio_desc *desc, int value); | ||
| 98 | static int gpiod_cansleep(const struct gpio_desc *desc); | ||
| 99 | static int gpiod_to_irq(const struct gpio_desc *desc); | ||
| 100 | static int gpiod_lock_as_irq(struct gpio_desc *desc); | ||
| 101 | static void gpiod_unlock_as_irq(struct gpio_desc *desc); | ||
| 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 | 81 | ||
| 108 | #ifdef CONFIG_DEBUG_FS | 82 | #ifdef CONFIG_DEBUG_FS |
| 109 | #define gpiod_emerg(desc, fmt, ...) \ | 83 | #define gpiod_emerg(desc, fmt, ...) \ |
| @@ -157,13 +131,14 @@ static int gpio_chip_hwgpio(const struct gpio_desc *desc) | |||
| 157 | /** | 131 | /** |
| 158 | * Convert a GPIO number to its descriptor | 132 | * Convert a GPIO number to its descriptor |
| 159 | */ | 133 | */ |
| 160 | static struct gpio_desc *gpio_to_desc(unsigned gpio) | 134 | struct gpio_desc *gpio_to_desc(unsigned gpio) |
| 161 | { | 135 | { |
| 162 | if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio)) | 136 | if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio)) |
| 163 | return NULL; | 137 | return NULL; |
| 164 | else | 138 | else |
| 165 | return &gpio_desc[gpio]; | 139 | return &gpio_desc[gpio]; |
| 166 | } | 140 | } |
| 141 | EXPORT_SYMBOL_GPL(gpio_to_desc); | ||
| 167 | 142 | ||
| 168 | /** | 143 | /** |
| 169 | * Convert an offset on a certain chip to a corresponding descriptor | 144 | * Convert an offset on a certain chip to a corresponding descriptor |
| @@ -181,10 +156,11 @@ static struct gpio_desc *gpiochip_offset_to_desc(struct gpio_chip *chip, | |||
| 181 | * This should disappear in the future but is needed since we still | 156 | * This should disappear in the future but is needed since we still |
| 182 | * use GPIO numbers for error messages and sysfs nodes | 157 | * use GPIO numbers for error messages and sysfs nodes |
| 183 | */ | 158 | */ |
| 184 | static int desc_to_gpio(const struct gpio_desc *desc) | 159 | int desc_to_gpio(const struct gpio_desc *desc) |
| 185 | { | 160 | { |
| 186 | return desc - &gpio_desc[0]; | 161 | return desc - &gpio_desc[0]; |
| 187 | } | 162 | } |
| 163 | EXPORT_SYMBOL_GPL(desc_to_gpio); | ||
| 188 | 164 | ||
| 189 | 165 | ||
| 190 | /* Warn when drivers omit gpio_request() calls -- legal but ill-advised | 166 | /* Warn when drivers omit gpio_request() calls -- legal but ill-advised |
| @@ -219,16 +195,15 @@ static int gpio_ensure_requested(struct gpio_desc *desc) | |||
| 219 | return 0; | 195 | return 0; |
| 220 | } | 196 | } |
| 221 | 197 | ||
| 222 | static struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) | 198 | /** |
| 199 | * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs | ||
| 200 | * @desc: descriptor to return the chip of | ||
| 201 | */ | ||
| 202 | struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) | ||
| 223 | { | 203 | { |
| 224 | return desc ? desc->chip : NULL; | 204 | return desc ? desc->chip : NULL; |
| 225 | } | 205 | } |
| 226 | 206 | EXPORT_SYMBOL_GPL(gpiod_to_chip); | |
| 227 | /* caller holds gpio_lock *OR* gpio is marked as requested */ | ||
| 228 | struct gpio_chip *gpio_to_chip(unsigned gpio) | ||
| 229 | { | ||
| 230 | return gpiod_to_chip(gpio_to_desc(gpio)); | ||
| 231 | } | ||
| 232 | 207 | ||
| 233 | /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ | 208 | /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ |
| 234 | static int gpiochip_find_base(int ngpio) | 209 | static int gpiochip_find_base(int ngpio) |
| @@ -254,8 +229,15 @@ static int gpiochip_find_base(int ngpio) | |||
| 254 | } | 229 | } |
| 255 | } | 230 | } |
| 256 | 231 | ||
| 257 | /* caller ensures gpio is valid and requested, chip->get_direction may sleep */ | 232 | /** |
| 258 | static int gpiod_get_direction(const struct gpio_desc *desc) | 233 | * gpiod_get_direction - return the current direction of a GPIO |
| 234 | * @desc: GPIO to get the direction of | ||
| 235 | * | ||
| 236 | * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error. | ||
| 237 | * | ||
| 238 | * This function may sleep if gpiod_cansleep() is true. | ||
| 239 | */ | ||
| 240 | int gpiod_get_direction(const struct gpio_desc *desc) | ||
| 259 | { | 241 | { |
| 260 | struct gpio_chip *chip; | 242 | struct gpio_chip *chip; |
| 261 | unsigned offset; | 243 | unsigned offset; |
| @@ -281,6 +263,7 @@ static int gpiod_get_direction(const struct gpio_desc *desc) | |||
| 281 | } | 263 | } |
| 282 | return status; | 264 | return status; |
| 283 | } | 265 | } |
| 266 | EXPORT_SYMBOL_GPL(gpiod_get_direction); | ||
| 284 | 267 | ||
| 285 | #ifdef CONFIG_GPIO_SYSFS | 268 | #ifdef CONFIG_GPIO_SYSFS |
| 286 | 269 | ||
| @@ -365,17 +348,10 @@ static ssize_t gpio_value_show(struct device *dev, | |||
| 365 | 348 | ||
| 366 | mutex_lock(&sysfs_lock); | 349 | mutex_lock(&sysfs_lock); |
| 367 | 350 | ||
| 368 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { | 351 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 369 | status = -EIO; | 352 | status = -EIO; |
| 370 | } else { | 353 | else |
| 371 | int value; | 354 | status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc)); |
| 372 | |||
| 373 | value = !!gpiod_get_value_cansleep(desc); | ||
| 374 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | ||
| 375 | value = !value; | ||
| 376 | |||
| 377 | status = sprintf(buf, "%d\n", value); | ||
| 378 | } | ||
| 379 | 355 | ||
| 380 | mutex_unlock(&sysfs_lock); | 356 | mutex_unlock(&sysfs_lock); |
| 381 | return status; | 357 | return status; |
| @@ -398,9 +374,7 @@ static ssize_t gpio_value_store(struct device *dev, | |||
| 398 | 374 | ||
| 399 | status = kstrtol(buf, 0, &value); | 375 | status = kstrtol(buf, 0, &value); |
| 400 | if (status == 0) { | 376 | if (status == 0) { |
| 401 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | 377 | gpiod_set_value_cansleep(desc, value); |
| 402 | value = !value; | ||
| 403 | gpiod_set_value_cansleep(desc, value != 0); | ||
| 404 | status = size; | 378 | status = size; |
| 405 | } | 379 | } |
| 406 | } | 380 | } |
| @@ -790,7 +764,7 @@ static struct class gpio_class = { | |||
| 790 | 764 | ||
| 791 | 765 | ||
| 792 | /** | 766 | /** |
| 793 | * gpio_export - export a GPIO through sysfs | 767 | * gpiod_export - export a GPIO through sysfs |
| 794 | * @gpio: gpio to make available, already requested | 768 | * @gpio: gpio to make available, already requested |
| 795 | * @direction_may_change: true if userspace may change gpio direction | 769 | * @direction_may_change: true if userspace may change gpio direction |
| 796 | * Context: arch_initcall or later | 770 | * Context: arch_initcall or later |
| @@ -804,7 +778,7 @@ static struct class gpio_class = { | |||
| 804 | * | 778 | * |
| 805 | * Returns zero on success, else an error. | 779 | * Returns zero on success, else an error. |
| 806 | */ | 780 | */ |
| 807 | static int gpiod_export(struct gpio_desc *desc, bool direction_may_change) | 781 | int gpiod_export(struct gpio_desc *desc, bool direction_may_change) |
| 808 | { | 782 | { |
| 809 | unsigned long flags; | 783 | unsigned long flags; |
| 810 | int status; | 784 | int status; |
| @@ -882,12 +856,7 @@ fail_unlock: | |||
| 882 | status); | 856 | status); |
| 883 | return status; | 857 | return status; |
| 884 | } | 858 | } |
| 885 | 859 | EXPORT_SYMBOL_GPL(gpiod_export); | |
| 886 | int gpio_export(unsigned gpio, bool direction_may_change) | ||
| 887 | { | ||
| 888 | return gpiod_export(gpio_to_desc(gpio), direction_may_change); | ||
| 889 | } | ||
| 890 | EXPORT_SYMBOL_GPL(gpio_export); | ||
| 891 | 860 | ||
| 892 | static int match_export(struct device *dev, const void *data) | 861 | static int match_export(struct device *dev, const void *data) |
| 893 | { | 862 | { |
| @@ -895,7 +864,7 @@ static int match_export(struct device *dev, const void *data) | |||
| 895 | } | 864 | } |
| 896 | 865 | ||
| 897 | /** | 866 | /** |
| 898 | * gpio_export_link - create a sysfs link to an exported GPIO node | 867 | * gpiod_export_link - create a sysfs link to an exported GPIO node |
| 899 | * @dev: device under which to create symlink | 868 | * @dev: device under which to create symlink |
| 900 | * @name: name of the symlink | 869 | * @name: name of the symlink |
| 901 | * @gpio: gpio to create symlink to, already exported | 870 | * @gpio: gpio to create symlink to, already exported |
| @@ -905,8 +874,8 @@ static int match_export(struct device *dev, const void *data) | |||
| 905 | * | 874 | * |
| 906 | * Returns zero on success, else an error. | 875 | * Returns zero on success, else an error. |
| 907 | */ | 876 | */ |
| 908 | static int gpiod_export_link(struct device *dev, const char *name, | 877 | int gpiod_export_link(struct device *dev, const char *name, |
| 909 | struct gpio_desc *desc) | 878 | struct gpio_desc *desc) |
| 910 | { | 879 | { |
| 911 | int status = -EINVAL; | 880 | int status = -EINVAL; |
| 912 | 881 | ||
| @@ -937,15 +906,10 @@ static int gpiod_export_link(struct device *dev, const char *name, | |||
| 937 | 906 | ||
| 938 | return status; | 907 | return status; |
| 939 | } | 908 | } |
| 940 | 909 | EXPORT_SYMBOL_GPL(gpiod_export_link); | |
| 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); | ||
| 946 | 910 | ||
| 947 | /** | 911 | /** |
| 948 | * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value | 912 | * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value |
| 949 | * @gpio: gpio to change | 913 | * @gpio: gpio to change |
| 950 | * @value: non-zero to use active low, i.e. inverted values | 914 | * @value: non-zero to use active low, i.e. inverted values |
| 951 | * | 915 | * |
| @@ -956,7 +920,7 @@ EXPORT_SYMBOL_GPL(gpio_export_link); | |||
| 956 | * | 920 | * |
| 957 | * Returns zero on success, else an error. | 921 | * Returns zero on success, else an error. |
| 958 | */ | 922 | */ |
| 959 | static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) | 923 | int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) |
| 960 | { | 924 | { |
| 961 | struct device *dev = NULL; | 925 | struct device *dev = NULL; |
| 962 | int status = -EINVAL; | 926 | int status = -EINVAL; |
| @@ -987,20 +951,15 @@ unlock: | |||
| 987 | 951 | ||
| 988 | return status; | 952 | return status; |
| 989 | } | 953 | } |
| 990 | 954 | EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low); | |
| 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 | } | ||
| 995 | EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low); | ||
| 996 | 955 | ||
| 997 | /** | 956 | /** |
| 998 | * gpio_unexport - reverse effect of gpio_export() | 957 | * gpiod_unexport - reverse effect of gpio_export() |
| 999 | * @gpio: gpio to make unavailable | 958 | * @gpio: gpio to make unavailable |
| 1000 | * | 959 | * |
| 1001 | * This is implicit on gpio_free(). | 960 | * This is implicit on gpio_free(). |
| 1002 | */ | 961 | */ |
| 1003 | static void gpiod_unexport(struct gpio_desc *desc) | 962 | void gpiod_unexport(struct gpio_desc *desc) |
| 1004 | { | 963 | { |
| 1005 | int status = 0; | 964 | int status = 0; |
| 1006 | struct device *dev = NULL; | 965 | struct device *dev = NULL; |
| @@ -1033,12 +992,7 @@ static void gpiod_unexport(struct gpio_desc *desc) | |||
| 1033 | pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), | 992 | pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), |
| 1034 | status); | 993 | status); |
| 1035 | } | 994 | } |
| 1036 | 995 | EXPORT_SYMBOL_GPL(gpiod_unexport); | |
| 1037 | void gpio_unexport(unsigned gpio) | ||
| 1038 | { | ||
| 1039 | gpiod_unexport(gpio_to_desc(gpio)); | ||
| 1040 | } | ||
| 1041 | EXPORT_SYMBOL_GPL(gpio_unexport); | ||
| 1042 | 996 | ||
| 1043 | static int gpiochip_export(struct gpio_chip *chip) | 997 | static int gpiochip_export(struct gpio_chip *chip) |
| 1044 | { | 998 | { |
| @@ -1145,27 +1099,6 @@ static inline void gpiochip_unexport(struct gpio_chip *chip) | |||
| 1145 | { | 1099 | { |
| 1146 | } | 1100 | } |
| 1147 | 1101 | ||
| 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 | |||
| 1169 | #endif /* CONFIG_GPIO_SYSFS */ | 1102 | #endif /* CONFIG_GPIO_SYSFS */ |
| 1170 | 1103 | ||
| 1171 | /* | 1104 | /* |
| @@ -1677,7 +1610,16 @@ EXPORT_SYMBOL_GPL(gpiochip_is_requested); | |||
| 1677 | * rely on gpio_request() having been called beforehand. | 1610 | * rely on gpio_request() having been called beforehand. |
| 1678 | */ | 1611 | */ |
| 1679 | 1612 | ||
| 1680 | static int gpiod_direction_input(struct gpio_desc *desc) | 1613 | /** |
| 1614 | * gpiod_direction_input - set the GPIO direction to input | ||
| 1615 | * @desc: GPIO to set to input | ||
| 1616 | * | ||
| 1617 | * Set the direction of the passed GPIO to input, such as gpiod_get_value() can | ||
| 1618 | * be called safely on it. | ||
| 1619 | * | ||
| 1620 | * Return 0 in case of success, else an error code. | ||
| 1621 | */ | ||
| 1622 | int gpiod_direction_input(struct gpio_desc *desc) | ||
| 1681 | { | 1623 | { |
| 1682 | unsigned long flags; | 1624 | unsigned long flags; |
| 1683 | struct gpio_chip *chip; | 1625 | struct gpio_chip *chip; |
| @@ -1734,14 +1676,19 @@ fail: | |||
| 1734 | gpiod_dbg(desc, "%s status %d\n", __func__, status); | 1676 | gpiod_dbg(desc, "%s status %d\n", __func__, status); |
| 1735 | return status; | 1677 | return status; |
| 1736 | } | 1678 | } |
| 1679 | EXPORT_SYMBOL_GPL(gpiod_direction_input); | ||
| 1737 | 1680 | ||
| 1738 | int gpio_direction_input(unsigned gpio) | 1681 | /** |
| 1739 | { | 1682 | * gpiod_direction_output - set the GPIO direction to input |
| 1740 | return gpiod_direction_input(gpio_to_desc(gpio)); | 1683 | * @desc: GPIO to set to output |
| 1741 | } | 1684 | * @value: initial output value of the GPIO |
| 1742 | EXPORT_SYMBOL_GPL(gpio_direction_input); | 1685 | * |
| 1743 | 1686 | * Set the direction of the passed GPIO to output, such as gpiod_set_value() can | |
| 1744 | static int gpiod_direction_output(struct gpio_desc *desc, int value) | 1687 | * be called safely on it. The initial value of the output must be specified. |
| 1688 | * | ||
| 1689 | * Return 0 in case of success, else an error code. | ||
| 1690 | */ | ||
| 1691 | int gpiod_direction_output(struct gpio_desc *desc, int value) | ||
| 1745 | { | 1692 | { |
| 1746 | unsigned long flags; | 1693 | unsigned long flags; |
| 1747 | struct gpio_chip *chip; | 1694 | struct gpio_chip *chip; |
| @@ -1814,22 +1761,17 @@ fail: | |||
| 1814 | gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status); | 1761 | gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status); |
| 1815 | return status; | 1762 | return status; |
| 1816 | } | 1763 | } |
| 1817 | 1764 | EXPORT_SYMBOL_GPL(gpiod_direction_output); | |
| 1818 | int gpio_direction_output(unsigned gpio, int value) | ||
| 1819 | { | ||
| 1820 | return gpiod_direction_output(gpio_to_desc(gpio), value); | ||
| 1821 | } | ||
| 1822 | EXPORT_SYMBOL_GPL(gpio_direction_output); | ||
| 1823 | 1765 | ||
| 1824 | /** | 1766 | /** |
| 1825 | * gpio_set_debounce - sets @debounce time for a @gpio | 1767 | * gpiod_set_debounce - sets @debounce time for a @gpio |
| 1826 | * @gpio: the gpio to set debounce time | 1768 | * @gpio: the gpio to set debounce time |
| 1827 | * @debounce: debounce time is microseconds | 1769 | * @debounce: debounce time is microseconds |
| 1828 | * | 1770 | * |
| 1829 | * returns -ENOTSUPP if the controller does not support setting | 1771 | * returns -ENOTSUPP if the controller does not support setting |
| 1830 | * debounce. | 1772 | * debounce. |
| 1831 | */ | 1773 | */ |
| 1832 | static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) | 1774 | int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) |
| 1833 | { | 1775 | { |
| 1834 | unsigned long flags; | 1776 | unsigned long flags; |
| 1835 | struct gpio_chip *chip; | 1777 | struct gpio_chip *chip; |
| @@ -1871,12 +1813,19 @@ fail: | |||
| 1871 | 1813 | ||
| 1872 | return status; | 1814 | return status; |
| 1873 | } | 1815 | } |
| 1816 | EXPORT_SYMBOL_GPL(gpiod_set_debounce); | ||
| 1874 | 1817 | ||
| 1875 | int gpio_set_debounce(unsigned gpio, unsigned debounce) | 1818 | /** |
| 1819 | * gpiod_is_active_low - test whether a GPIO is active-low or not | ||
| 1820 | * @desc: the gpio descriptor to test | ||
| 1821 | * | ||
| 1822 | * Returns 1 if the GPIO is active-low, 0 otherwise. | ||
| 1823 | */ | ||
| 1824 | int gpiod_is_active_low(const struct gpio_desc *desc) | ||
| 1876 | { | 1825 | { |
| 1877 | return gpiod_set_debounce(gpio_to_desc(gpio), debounce); | 1826 | return test_bit(FLAG_ACTIVE_LOW, &desc->flags); |
| 1878 | } | 1827 | } |
| 1879 | EXPORT_SYMBOL_GPL(gpio_set_debounce); | 1828 | EXPORT_SYMBOL_GPL(gpiod_is_active_low); |
| 1880 | 1829 | ||
| 1881 | /* I/O calls are only valid after configuration completed; the relevant | 1830 | /* I/O calls are only valid after configuration completed; the relevant |
| 1882 | * "is this a valid GPIO" error checks should already have been done. | 1831 | * "is this a valid GPIO" error checks should already have been done. |
| @@ -1900,7 +1849,7 @@ EXPORT_SYMBOL_GPL(gpio_set_debounce); | |||
| 1900 | * that the GPIO was actually requested. | 1849 | * that the GPIO was actually requested. |
| 1901 | */ | 1850 | */ |
| 1902 | 1851 | ||
| 1903 | static int _gpiod_get_value(const struct gpio_desc *desc) | 1852 | static int _gpiod_get_raw_value(const struct gpio_desc *desc) |
| 1904 | { | 1853 | { |
| 1905 | struct gpio_chip *chip; | 1854 | struct gpio_chip *chip; |
| 1906 | int value; | 1855 | int value; |
| @@ -1914,33 +1863,54 @@ static int _gpiod_get_value(const struct gpio_desc *desc) | |||
| 1914 | } | 1863 | } |
| 1915 | 1864 | ||
| 1916 | /** | 1865 | /** |
| 1917 | * __gpio_get_value() - return a gpio's value | 1866 | * gpiod_get_raw_value() - return a gpio's raw value |
| 1918 | * @gpio: gpio whose value will be returned | 1867 | * @desc: gpio whose value will be returned |
| 1919 | * Context: any | ||
| 1920 | * | 1868 | * |
| 1921 | * This is used directly or indirectly to implement gpio_get_value(). | 1869 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding |
| 1922 | * It returns the zero or nonzero value provided by the associated | 1870 | * its ACTIVE_LOW status. |
| 1923 | * gpio_chip.get() method; or zero if no such method is provided. | 1871 | * |
| 1872 | * This function should be called from contexts where we cannot sleep, and will | ||
| 1873 | * complain if the GPIO chip functions potentially sleep. | ||
| 1924 | */ | 1874 | */ |
| 1925 | static int gpiod_get_value(const struct gpio_desc *desc) | 1875 | int gpiod_get_raw_value(const struct gpio_desc *desc) |
| 1926 | { | 1876 | { |
| 1927 | if (!desc) | 1877 | if (!desc) |
| 1928 | return 0; | 1878 | return 0; |
| 1929 | /* Should be using gpio_get_value_cansleep() */ | 1879 | /* Should be using gpio_get_value_cansleep() */ |
| 1930 | WARN_ON(desc->chip->can_sleep); | 1880 | WARN_ON(desc->chip->can_sleep); |
| 1931 | return _gpiod_get_value(desc); | 1881 | return _gpiod_get_raw_value(desc); |
| 1932 | } | 1882 | } |
| 1883 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value); | ||
| 1933 | 1884 | ||
| 1934 | int __gpio_get_value(unsigned gpio) | 1885 | /** |
| 1886 | * gpiod_get_value() - return a gpio's value | ||
| 1887 | * @desc: gpio whose value will be returned | ||
| 1888 | * | ||
| 1889 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into | ||
| 1890 | * account. | ||
| 1891 | * | ||
| 1892 | * This function should be called from contexts where we cannot sleep, and will | ||
| 1893 | * complain if the GPIO chip functions potentially sleep. | ||
| 1894 | */ | ||
| 1895 | int gpiod_get_value(const struct gpio_desc *desc) | ||
| 1935 | { | 1896 | { |
| 1936 | return gpiod_get_value(gpio_to_desc(gpio)); | 1897 | int value; |
| 1898 | if (!desc) | ||
| 1899 | return 0; | ||
| 1900 | /* Should be using gpio_get_value_cansleep() */ | ||
| 1901 | WARN_ON(desc->chip->can_sleep); | ||
| 1902 | |||
| 1903 | value = _gpiod_get_raw_value(desc); | ||
| 1904 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | ||
| 1905 | value = !value; | ||
| 1906 | |||
| 1907 | return value; | ||
| 1937 | } | 1908 | } |
| 1938 | EXPORT_SYMBOL_GPL(__gpio_get_value); | 1909 | EXPORT_SYMBOL_GPL(gpiod_get_value); |
| 1939 | 1910 | ||
| 1940 | /* | 1911 | /* |
| 1941 | * _gpio_set_open_drain_value() - Set the open drain gpio's value. | 1912 | * _gpio_set_open_drain_value() - Set the open drain gpio's value. |
| 1942 | * @gpio: Gpio whose state need to be set. | 1913 | * @desc: gpio descriptor whose state need to be set. |
| 1943 | * @chip: Gpio chip. | ||
| 1944 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. | 1914 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. |
| 1945 | */ | 1915 | */ |
| 1946 | static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value) | 1916 | static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value) |
| @@ -1966,9 +1936,8 @@ static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value) | |||
| 1966 | } | 1936 | } |
| 1967 | 1937 | ||
| 1968 | /* | 1938 | /* |
| 1969 | * _gpio_set_open_source() - Set the open source gpio's value. | 1939 | * _gpio_set_open_source_value() - Set the open source gpio's value. |
| 1970 | * @gpio: Gpio whose state need to be set. | 1940 | * @desc: gpio descriptor whose state need to be set. |
| 1971 | * @chip: Gpio chip. | ||
| 1972 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. | 1941 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. |
| 1973 | */ | 1942 | */ |
| 1974 | static void _gpio_set_open_source_value(struct gpio_desc *desc, int value) | 1943 | static void _gpio_set_open_source_value(struct gpio_desc *desc, int value) |
| @@ -1993,7 +1962,7 @@ static void _gpio_set_open_source_value(struct gpio_desc *desc, int value) | |||
| 1993 | __func__, err); | 1962 | __func__, err); |
| 1994 | } | 1963 | } |
| 1995 | 1964 | ||
| 1996 | static void _gpiod_set_value(struct gpio_desc *desc, int value) | 1965 | static void _gpiod_set_raw_value(struct gpio_desc *desc, int value) |
| 1997 | { | 1966 | { |
| 1998 | struct gpio_chip *chip; | 1967 | struct gpio_chip *chip; |
| 1999 | 1968 | ||
| @@ -2008,62 +1977,70 @@ static void _gpiod_set_value(struct gpio_desc *desc, int value) | |||
| 2008 | } | 1977 | } |
| 2009 | 1978 | ||
| 2010 | /** | 1979 | /** |
| 2011 | * __gpio_set_value() - assign a gpio's value | 1980 | * gpiod_set_raw_value() - assign a gpio's raw value |
| 2012 | * @gpio: gpio whose value will be assigned | 1981 | * @desc: gpio whose value will be assigned |
| 2013 | * @value: value to assign | 1982 | * @value: value to assign |
| 2014 | * Context: any | ||
| 2015 | * | 1983 | * |
| 2016 | * This is used directly or indirectly to implement gpio_set_value(). | 1984 | * Set the raw value of the GPIO, i.e. the value of its physical line without |
| 2017 | * It invokes the associated gpio_chip.set() method. | 1985 | * regard for its ACTIVE_LOW status. |
| 1986 | * | ||
| 1987 | * This function should be called from contexts where we cannot sleep, and will | ||
| 1988 | * complain if the GPIO chip functions potentially sleep. | ||
| 2018 | */ | 1989 | */ |
| 2019 | static void gpiod_set_value(struct gpio_desc *desc, int value) | 1990 | void gpiod_set_raw_value(struct gpio_desc *desc, int value) |
| 2020 | { | 1991 | { |
| 2021 | |||
| 2022 | if (!desc) | 1992 | if (!desc) |
| 2023 | return; | 1993 | return; |
| 2024 | /* Should be using gpio_set_value_cansleep() */ | 1994 | /* Should be using gpio_set_value_cansleep() */ |
| 2025 | WARN_ON(desc->chip->can_sleep); | 1995 | WARN_ON(desc->chip->can_sleep); |
| 2026 | _gpiod_set_value(desc, value); | 1996 | _gpiod_set_raw_value(desc, value); |
| 2027 | } | 1997 | } |
| 1998 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value); | ||
| 2028 | 1999 | ||
| 2029 | void __gpio_set_value(unsigned gpio, int value) | 2000 | /** |
| 2001 | * gpiod_set_value() - assign a gpio's value | ||
| 2002 | * @desc: gpio whose value will be assigned | ||
| 2003 | * @value: value to assign | ||
| 2004 | * | ||
| 2005 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into | ||
| 2006 | * account | ||
| 2007 | * | ||
| 2008 | * This function should be called from contexts where we cannot sleep, and will | ||
| 2009 | * complain if the GPIO chip functions potentially sleep. | ||
| 2010 | */ | ||
| 2011 | void gpiod_set_value(struct gpio_desc *desc, int value) | ||
| 2030 | { | 2012 | { |
| 2031 | return gpiod_set_value(gpio_to_desc(gpio), value); | 2013 | if (!desc) |
| 2014 | return; | ||
| 2015 | /* Should be using gpio_set_value_cansleep() */ | ||
| 2016 | WARN_ON(desc->chip->can_sleep); | ||
| 2017 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | ||
| 2018 | value = !value; | ||
| 2019 | _gpiod_set_raw_value(desc, value); | ||
| 2032 | } | 2020 | } |
| 2033 | EXPORT_SYMBOL_GPL(__gpio_set_value); | 2021 | EXPORT_SYMBOL_GPL(gpiod_set_value); |
| 2034 | 2022 | ||
| 2035 | /** | 2023 | /** |
| 2036 | * __gpio_cansleep() - report whether gpio value access will sleep | 2024 | * gpiod_cansleep() - report whether gpio value access may sleep |
| 2037 | * @gpio: gpio in question | 2025 | * @desc: gpio to check |
| 2038 | * Context: any | ||
| 2039 | * | 2026 | * |
| 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 | */ | 2027 | */ |
| 2043 | static int gpiod_cansleep(const struct gpio_desc *desc) | 2028 | int gpiod_cansleep(const struct gpio_desc *desc) |
| 2044 | { | 2029 | { |
| 2045 | if (!desc) | 2030 | if (!desc) |
| 2046 | return 0; | 2031 | return 0; |
| 2047 | /* only call this on GPIOs that are valid! */ | ||
| 2048 | return desc->chip->can_sleep; | 2032 | return desc->chip->can_sleep; |
| 2049 | } | 2033 | } |
| 2050 | 2034 | EXPORT_SYMBOL_GPL(gpiod_cansleep); | |
| 2051 | int __gpio_cansleep(unsigned gpio) | ||
| 2052 | { | ||
| 2053 | return gpiod_cansleep(gpio_to_desc(gpio)); | ||
| 2054 | } | ||
| 2055 | EXPORT_SYMBOL_GPL(__gpio_cansleep); | ||
| 2056 | 2035 | ||
| 2057 | /** | 2036 | /** |
| 2058 | * __gpio_to_irq() - return the IRQ corresponding to a GPIO | 2037 | * gpiod_to_irq() - return the IRQ corresponding to a GPIO |
| 2059 | * @gpio: gpio whose IRQ will be returned (already requested) | 2038 | * @desc: gpio whose IRQ will be returned (already requested) |
| 2060 | * Context: any | ||
| 2061 | * | 2039 | * |
| 2062 | * This is used directly or indirectly to implement gpio_to_irq(). | 2040 | * Return the IRQ corresponding to the passed GPIO, or an error code in case of |
| 2063 | * It returns the number of the IRQ signaled by this (input) GPIO, | 2041 | * error. |
| 2064 | * or a negative errno. | ||
| 2065 | */ | 2042 | */ |
| 2066 | static int gpiod_to_irq(const struct gpio_desc *desc) | 2043 | int gpiod_to_irq(const struct gpio_desc *desc) |
| 2067 | { | 2044 | { |
| 2068 | struct gpio_chip *chip; | 2045 | struct gpio_chip *chip; |
| 2069 | int offset; | 2046 | int offset; |
| @@ -2074,12 +2051,7 @@ static int gpiod_to_irq(const struct gpio_desc *desc) | |||
| 2074 | offset = gpio_chip_hwgpio(desc); | 2051 | offset = gpio_chip_hwgpio(desc); |
| 2075 | return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO; | 2052 | return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO; |
| 2076 | } | 2053 | } |
| 2077 | 2054 | EXPORT_SYMBOL_GPL(gpiod_to_irq); | |
| 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); | ||
| 2083 | 2055 | ||
| 2084 | /** | 2056 | /** |
| 2085 | * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ | 2057 | * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ |
| @@ -2091,7 +2063,7 @@ EXPORT_SYMBOL_GPL(__gpio_to_irq); | |||
| 2091 | * of its irq_chip implementation if the GPIO is known from that | 2063 | * of its irq_chip implementation if the GPIO is known from that |
| 2092 | * code. | 2064 | * code. |
| 2093 | */ | 2065 | */ |
| 2094 | static int gpiod_lock_as_irq(struct gpio_desc *desc) | 2066 | int gpiod_lock_as_irq(struct gpio_desc *desc) |
| 2095 | { | 2067 | { |
| 2096 | if (!desc) | 2068 | if (!desc) |
| 2097 | return -EINVAL; | 2069 | return -EINVAL; |
| @@ -2106,6 +2078,7 @@ static int gpiod_lock_as_irq(struct gpio_desc *desc) | |||
| 2106 | set_bit(FLAG_USED_AS_IRQ, &desc->flags); | 2078 | set_bit(FLAG_USED_AS_IRQ, &desc->flags); |
| 2107 | return 0; | 2079 | return 0; |
| 2108 | } | 2080 | } |
| 2081 | EXPORT_SYMBOL_GPL(gpiod_lock_as_irq); | ||
| 2109 | 2082 | ||
| 2110 | int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset) | 2083 | int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset) |
| 2111 | { | 2084 | { |
| @@ -2120,13 +2093,14 @@ EXPORT_SYMBOL_GPL(gpio_lock_as_irq); | |||
| 2120 | * This is used directly by GPIO drivers that want to indicate | 2093 | * This is used directly by GPIO drivers that want to indicate |
| 2121 | * that a certain GPIO is no longer used exclusively for IRQ. | 2094 | * that a certain GPIO is no longer used exclusively for IRQ. |
| 2122 | */ | 2095 | */ |
| 2123 | static void gpiod_unlock_as_irq(struct gpio_desc *desc) | 2096 | void gpiod_unlock_as_irq(struct gpio_desc *desc) |
| 2124 | { | 2097 | { |
| 2125 | if (!desc) | 2098 | if (!desc) |
| 2126 | return; | 2099 | return; |
| 2127 | 2100 | ||
| 2128 | clear_bit(FLAG_USED_AS_IRQ, &desc->flags); | 2101 | clear_bit(FLAG_USED_AS_IRQ, &desc->flags); |
| 2129 | } | 2102 | } |
| 2103 | EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq); | ||
| 2130 | 2104 | ||
| 2131 | void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset) | 2105 | void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset) |
| 2132 | { | 2106 | { |
| @@ -2134,37 +2108,89 @@ void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset) | |||
| 2134 | } | 2108 | } |
| 2135 | EXPORT_SYMBOL_GPL(gpio_unlock_as_irq); | 2109 | EXPORT_SYMBOL_GPL(gpio_unlock_as_irq); |
| 2136 | 2110 | ||
| 2137 | /* There's no value in making it easy to inline GPIO calls that may sleep. | 2111 | /** |
| 2138 | * Common examples include ones connected to I2C or SPI chips. | 2112 | * gpiod_get_raw_value_cansleep() - return a gpio's raw value |
| 2113 | * @desc: gpio whose value will be returned | ||
| 2114 | * | ||
| 2115 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding | ||
| 2116 | * its ACTIVE_LOW status. | ||
| 2117 | * | ||
| 2118 | * This function is to be called from contexts that can sleep. | ||
| 2139 | */ | 2119 | */ |
| 2140 | 2120 | int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) | |
| 2141 | static int gpiod_get_value_cansleep(const struct gpio_desc *desc) | ||
| 2142 | { | 2121 | { |
| 2143 | might_sleep_if(extra_checks); | 2122 | might_sleep_if(extra_checks); |
| 2144 | if (!desc) | 2123 | if (!desc) |
| 2145 | return 0; | 2124 | return 0; |
| 2146 | return _gpiod_get_value(desc); | 2125 | return _gpiod_get_raw_value(desc); |
| 2147 | } | 2126 | } |
| 2127 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep); | ||
| 2148 | 2128 | ||
| 2149 | int gpio_get_value_cansleep(unsigned gpio) | 2129 | /** |
| 2130 | * gpiod_get_value_cansleep() - return a gpio's value | ||
| 2131 | * @desc: gpio whose value will be returned | ||
| 2132 | * | ||
| 2133 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into | ||
| 2134 | * account. | ||
| 2135 | * | ||
| 2136 | * This function is to be called from contexts that can sleep. | ||
| 2137 | */ | ||
| 2138 | int gpiod_get_value_cansleep(const struct gpio_desc *desc) | ||
| 2150 | { | 2139 | { |
| 2151 | return gpiod_get_value_cansleep(gpio_to_desc(gpio)); | 2140 | int value; |
| 2141 | |||
| 2142 | might_sleep_if(extra_checks); | ||
| 2143 | if (!desc) | ||
| 2144 | return 0; | ||
| 2145 | |||
| 2146 | value = _gpiod_get_raw_value(desc); | ||
| 2147 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | ||
| 2148 | value = !value; | ||
| 2149 | |||
| 2150 | return value; | ||
| 2152 | } | 2151 | } |
| 2153 | EXPORT_SYMBOL_GPL(gpio_get_value_cansleep); | 2152 | EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep); |
| 2154 | 2153 | ||
| 2155 | static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) | 2154 | /** |
| 2155 | * gpiod_set_raw_value_cansleep() - assign a gpio's raw value | ||
| 2156 | * @desc: gpio whose value will be assigned | ||
| 2157 | * @value: value to assign | ||
| 2158 | * | ||
| 2159 | * Set the raw value of the GPIO, i.e. the value of its physical line without | ||
| 2160 | * regard for its ACTIVE_LOW status. | ||
| 2161 | * | ||
| 2162 | * This function is to be called from contexts that can sleep. | ||
| 2163 | */ | ||
| 2164 | void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value) | ||
| 2156 | { | 2165 | { |
| 2157 | might_sleep_if(extra_checks); | 2166 | might_sleep_if(extra_checks); |
| 2158 | if (!desc) | 2167 | if (!desc) |
| 2159 | return; | 2168 | return; |
| 2160 | _gpiod_set_value(desc, value); | 2169 | _gpiod_set_raw_value(desc, value); |
| 2161 | } | 2170 | } |
| 2171 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep); | ||
| 2162 | 2172 | ||
| 2163 | void gpio_set_value_cansleep(unsigned gpio, int value) | 2173 | /** |
| 2174 | * gpiod_set_value_cansleep() - assign a gpio's value | ||
| 2175 | * @desc: gpio whose value will be assigned | ||
| 2176 | * @value: value to assign | ||
| 2177 | * | ||
| 2178 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into | ||
| 2179 | * account | ||
| 2180 | * | ||
| 2181 | * This function is to be called from contexts that can sleep. | ||
| 2182 | */ | ||
| 2183 | void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) | ||
| 2164 | { | 2184 | { |
| 2165 | return gpiod_set_value_cansleep(gpio_to_desc(gpio), value); | 2185 | might_sleep_if(extra_checks); |
| 2186 | if (!desc) | ||
| 2187 | return; | ||
| 2188 | |||
| 2189 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | ||
| 2190 | value = !value; | ||
| 2191 | _gpiod_set_raw_value(desc, value); | ||
| 2166 | } | 2192 | } |
| 2167 | EXPORT_SYMBOL_GPL(gpio_set_value_cansleep); | 2193 | EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep); |
| 2168 | 2194 | ||
| 2169 | #ifdef CONFIG_DEBUG_FS | 2195 | #ifdef CONFIG_DEBUG_FS |
| 2170 | 2196 | ||
diff --git a/include/asm-generic/gpio.h b/include/asm-generic/gpio.h index b309a5c0019e..00f8f0a9edcd 100644 --- a/include/asm-generic/gpio.h +++ b/include/asm-generic/gpio.h | |||
| @@ -10,6 +10,8 @@ | |||
| 10 | #ifdef CONFIG_GPIOLIB | 10 | #ifdef CONFIG_GPIOLIB |
| 11 | 11 | ||
| 12 | #include <linux/compiler.h> | 12 | #include <linux/compiler.h> |
| 13 | #include <linux/gpio/driver.h> | ||
| 14 | #include <linux/gpio/consumer.h> | ||
| 13 | 15 | ||
| 14 | /* Platforms may implement their GPIO interface with library code, | 16 | /* Platforms may implement their GPIO interface with library code, |
| 15 | * at a small performance cost for non-inlined operations and some | 17 | * at a small performance cost for non-inlined operations and some |
| @@ -49,122 +51,11 @@ struct module; | |||
| 49 | struct device_node; | 51 | struct device_node; |
| 50 | struct gpio_desc; | 52 | struct gpio_desc; |
| 51 | 53 | ||
| 52 | /** | 54 | /* caller holds gpio_lock *OR* gpio is marked as requested */ |
| 53 | * struct gpio_chip - abstract a GPIO controller | 55 | static inline struct gpio_chip *gpio_to_chip(unsigned gpio) |
| 54 | * @label: for diagnostics | 56 | { |
| 55 | * @dev: optional device providing the GPIOs | 57 | return gpiod_to_chip(gpio_to_desc(gpio)); |
| 56 | * @owner: helps prevent removal of modules exporting active GPIOs | 58 | } |
| 57 | * @list: links gpio_chips together for traversal | ||
| 58 | * @request: optional hook for chip-specific activation, such as | ||
| 59 | * enabling module power and clock; may sleep | ||
| 60 | * @free: optional hook for chip-specific deactivation, such as | ||
| 61 | * disabling module power and clock; may sleep | ||
| 62 | * @get_direction: returns direction for signal "offset", 0=out, 1=in, | ||
| 63 | * (same as GPIOF_DIR_XXX), or negative error | ||
| 64 | * @direction_input: configures signal "offset" as input, or returns error | ||
| 65 | * @get: returns value for signal "offset"; for output signals this | ||
| 66 | * returns either the value actually sensed, or zero | ||
| 67 | * @direction_output: configures signal "offset" as output, or returns error | ||
| 68 | * @set_debounce: optional hook for setting debounce time for specified gpio in | ||
| 69 | * interrupt triggered gpio chips | ||
| 70 | * @set: assigns output value for signal "offset" | ||
| 71 | * @to_irq: optional hook supporting non-static gpio_to_irq() mappings; | ||
| 72 | * implementation may not sleep | ||
| 73 | * @dbg_show: optional routine to show contents in debugfs; default code | ||
| 74 | * will be used when this is omitted, but custom code can show extra | ||
| 75 | * state (such as pullup/pulldown configuration). | ||
| 76 | * @base: identifies the first GPIO number handled by this chip; or, if | ||
| 77 | * negative during registration, requests dynamic ID allocation. | ||
| 78 | * @ngpio: the number of GPIOs handled by this controller; the last GPIO | ||
| 79 | * handled is (base + ngpio - 1). | ||
| 80 | * @desc: array of ngpio descriptors. Private. | ||
| 81 | * @can_sleep: flag must be set iff get()/set() methods sleep, as they | ||
| 82 | * must while accessing GPIO expander chips over I2C or SPI | ||
| 83 | * @names: if set, must be an array of strings to use as alternative | ||
| 84 | * names for the GPIOs in this chip. Any entry in the array | ||
| 85 | * may be NULL if there is no alias for the GPIO, however the | ||
| 86 | * array must be @ngpio entries long. A name can include a single printk | ||
| 87 | * format specifier for an unsigned int. It is substituted by the actual | ||
| 88 | * number of the gpio. | ||
| 89 | * | ||
| 90 | * A gpio_chip can help platforms abstract various sources of GPIOs so | ||
| 91 | * they can all be accessed through a common programing interface. | ||
| 92 | * Example sources would be SOC controllers, FPGAs, multifunction | ||
| 93 | * chips, dedicated GPIO expanders, and so on. | ||
| 94 | * | ||
| 95 | * Each chip controls a number of signals, identified in method calls | ||
| 96 | * by "offset" values in the range 0..(@ngpio - 1). When those signals | ||
| 97 | * are referenced through calls like gpio_get_value(gpio), the offset | ||
| 98 | * is calculated by subtracting @base from the gpio number. | ||
| 99 | */ | ||
| 100 | struct gpio_chip { | ||
| 101 | const char *label; | ||
| 102 | struct device *dev; | ||
| 103 | struct module *owner; | ||
| 104 | struct list_head list; | ||
| 105 | |||
| 106 | int (*request)(struct gpio_chip *chip, | ||
| 107 | unsigned offset); | ||
| 108 | void (*free)(struct gpio_chip *chip, | ||
| 109 | unsigned offset); | ||
| 110 | int (*get_direction)(struct gpio_chip *chip, | ||
| 111 | unsigned offset); | ||
| 112 | int (*direction_input)(struct gpio_chip *chip, | ||
| 113 | unsigned offset); | ||
| 114 | int (*get)(struct gpio_chip *chip, | ||
| 115 | unsigned offset); | ||
| 116 | int (*direction_output)(struct gpio_chip *chip, | ||
| 117 | unsigned offset, int value); | ||
| 118 | int (*set_debounce)(struct gpio_chip *chip, | ||
| 119 | unsigned offset, unsigned debounce); | ||
| 120 | |||
| 121 | void (*set)(struct gpio_chip *chip, | ||
| 122 | unsigned offset, int value); | ||
| 123 | |||
| 124 | int (*to_irq)(struct gpio_chip *chip, | ||
| 125 | unsigned offset); | ||
| 126 | |||
| 127 | void (*dbg_show)(struct seq_file *s, | ||
| 128 | struct gpio_chip *chip); | ||
| 129 | int base; | ||
| 130 | u16 ngpio; | ||
| 131 | struct gpio_desc *desc; | ||
| 132 | const char *const *names; | ||
| 133 | unsigned can_sleep:1; | ||
| 134 | unsigned exported:1; | ||
| 135 | |||
| 136 | #if defined(CONFIG_OF_GPIO) | ||
| 137 | /* | ||
| 138 | * If CONFIG_OF is enabled, then all GPIO controllers described in the | ||
| 139 | * device tree automatically may have an OF translation | ||
| 140 | */ | ||
| 141 | struct device_node *of_node; | ||
| 142 | int of_gpio_n_cells; | ||
| 143 | int (*of_xlate)(struct gpio_chip *gc, | ||
| 144 | const struct of_phandle_args *gpiospec, u32 *flags); | ||
| 145 | #endif | ||
| 146 | #ifdef CONFIG_PINCTRL | ||
| 147 | /* | ||
| 148 | * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally | ||
| 149 | * describe the actual pin range which they serve in an SoC. This | ||
| 150 | * information would be used by pinctrl subsystem to configure | ||
| 151 | * corresponding pins for gpio usage. | ||
| 152 | */ | ||
| 153 | struct list_head pin_ranges; | ||
| 154 | #endif | ||
| 155 | }; | ||
| 156 | |||
| 157 | extern const char *gpiochip_is_requested(struct gpio_chip *chip, | ||
| 158 | unsigned offset); | ||
| 159 | extern struct gpio_chip *gpio_to_chip(unsigned gpio); | ||
| 160 | |||
| 161 | /* add/remove chips */ | ||
| 162 | extern int gpiochip_add(struct gpio_chip *chip); | ||
| 163 | extern int __must_check gpiochip_remove(struct gpio_chip *chip); | ||
| 164 | extern struct gpio_chip *gpiochip_find(void *data, | ||
| 165 | int (*match)(struct gpio_chip *chip, | ||
| 166 | void *data)); | ||
| 167 | |||
| 168 | 59 | ||
| 169 | /* Always use the library code for GPIO management calls, | 60 | /* Always use the library code for GPIO management calls, |
| 170 | * or when sleeping may be involved. | 61 | * or when sleeping may be involved. |
| @@ -172,25 +63,52 @@ extern struct gpio_chip *gpiochip_find(void *data, | |||
| 172 | extern int gpio_request(unsigned gpio, const char *label); | 63 | extern int gpio_request(unsigned gpio, const char *label); |
| 173 | extern void gpio_free(unsigned gpio); | 64 | extern void gpio_free(unsigned gpio); |
| 174 | 65 | ||
| 175 | extern int gpio_direction_input(unsigned gpio); | 66 | static inline int gpio_direction_input(unsigned gpio) |
| 176 | extern int gpio_direction_output(unsigned gpio, int value); | 67 | { |
| 68 | return gpiod_direction_input(gpio_to_desc(gpio)); | ||
| 69 | } | ||
| 70 | static inline int gpio_direction_output(unsigned gpio, int value) | ||
| 71 | { | ||
| 72 | return gpiod_direction_output(gpio_to_desc(gpio), value); | ||
| 73 | } | ||
| 177 | 74 | ||
| 178 | extern int gpio_set_debounce(unsigned gpio, unsigned debounce); | 75 | static inline int gpio_set_debounce(unsigned gpio, unsigned debounce) |
| 76 | { | ||
| 77 | return gpiod_set_debounce(gpio_to_desc(gpio), debounce); | ||
| 78 | } | ||
| 179 | 79 | ||
| 180 | extern int gpio_get_value_cansleep(unsigned gpio); | 80 | static inline int gpio_get_value_cansleep(unsigned gpio) |
| 181 | extern void gpio_set_value_cansleep(unsigned gpio, int value); | 81 | { |
| 82 | return gpiod_get_raw_value_cansleep(gpio_to_desc(gpio)); | ||
| 83 | } | ||
| 84 | static inline void gpio_set_value_cansleep(unsigned gpio, int value) | ||
| 85 | { | ||
| 86 | return gpiod_set_raw_value_cansleep(gpio_to_desc(gpio), value); | ||
| 87 | } | ||
| 182 | 88 | ||
| 183 | 89 | ||
| 184 | /* A platform's <asm/gpio.h> code may want to inline the I/O calls when | 90 | /* A platform's <asm/gpio.h> code may want to inline the I/O calls when |
| 185 | * the GPIO is constant and refers to some always-present controller, | 91 | * the GPIO is constant and refers to some always-present controller, |
| 186 | * giving direct access to chip registers and tight bitbanging loops. | 92 | * giving direct access to chip registers and tight bitbanging loops. |
| 187 | */ | 93 | */ |
| 188 | extern int __gpio_get_value(unsigned gpio); | 94 | static inline int __gpio_get_value(unsigned gpio) |
| 189 | extern void __gpio_set_value(unsigned gpio, int value); | 95 | { |
| 96 | return gpiod_get_raw_value(gpio_to_desc(gpio)); | ||
| 97 | } | ||
| 98 | static inline void __gpio_set_value(unsigned gpio, int value) | ||
| 99 | { | ||
| 100 | return gpiod_set_raw_value(gpio_to_desc(gpio), value); | ||
| 101 | } | ||
| 190 | 102 | ||
| 191 | extern int __gpio_cansleep(unsigned gpio); | 103 | static inline int __gpio_cansleep(unsigned gpio) |
| 104 | { | ||
| 105 | return gpiod_cansleep(gpio_to_desc(gpio)); | ||
| 106 | } | ||
| 192 | 107 | ||
| 193 | extern int __gpio_to_irq(unsigned gpio); | 108 | static inline int __gpio_to_irq(unsigned gpio) |
| 109 | { | ||
| 110 | return gpiod_to_irq(gpio_to_desc(gpio)); | ||
| 111 | } | ||
| 194 | 112 | ||
| 195 | extern int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset); | 113 | extern int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset); |
| 196 | extern void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset); | 114 | extern void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset); |
| @@ -199,19 +117,30 @@ extern int gpio_request_one(unsigned gpio, unsigned long flags, const char *labe | |||
| 199 | extern int gpio_request_array(const struct gpio *array, size_t num); | 117 | extern int gpio_request_array(const struct gpio *array, size_t num); |
| 200 | extern void gpio_free_array(const struct gpio *array, size_t num); | 118 | extern void gpio_free_array(const struct gpio *array, size_t num); |
| 201 | 119 | ||
| 202 | #ifdef CONFIG_GPIO_SYSFS | ||
| 203 | |||
| 204 | /* | 120 | /* |
| 205 | * A sysfs interface can be exported by individual drivers if they want, | 121 | * A sysfs interface can be exported by individual drivers if they want, |
| 206 | * but more typically is configured entirely from userspace. | 122 | * but more typically is configured entirely from userspace. |
| 207 | */ | 123 | */ |
| 208 | extern int gpio_export(unsigned gpio, bool direction_may_change); | 124 | static inline int gpio_export(unsigned gpio, bool direction_may_change) |
| 209 | extern int gpio_export_link(struct device *dev, const char *name, | 125 | { |
| 210 | unsigned gpio); | 126 | return gpiod_export(gpio_to_desc(gpio), direction_may_change); |
| 211 | extern int gpio_sysfs_set_active_low(unsigned gpio, int value); | 127 | } |
| 212 | extern void gpio_unexport(unsigned gpio); | ||
| 213 | 128 | ||
| 214 | #endif /* CONFIG_GPIO_SYSFS */ | 129 | static inline int gpio_export_link(struct device *dev, const char *name, |
| 130 | unsigned gpio) | ||
| 131 | { | ||
| 132 | return gpiod_export_link(dev, name, gpio_to_desc(gpio)); | ||
| 133 | } | ||
| 134 | |||
| 135 | static inline int gpio_sysfs_set_active_low(unsigned gpio, int value) | ||
| 136 | { | ||
| 137 | return gpiod_sysfs_set_active_low(gpio_to_desc(gpio), value); | ||
| 138 | } | ||
| 139 | |||
| 140 | static inline void gpio_unexport(unsigned gpio) | ||
| 141 | { | ||
| 142 | gpiod_unexport(gpio_to_desc(gpio)); | ||
| 143 | } | ||
| 215 | 144 | ||
| 216 | #ifdef CONFIG_PINCTRL | 145 | #ifdef CONFIG_PINCTRL |
| 217 | 146 | ||
| @@ -281,31 +210,4 @@ static inline void gpio_set_value_cansleep(unsigned gpio, int value) | |||
| 281 | 210 | ||
| 282 | #endif /* !CONFIG_GPIOLIB */ | 211 | #endif /* !CONFIG_GPIOLIB */ |
| 283 | 212 | ||
| 284 | #ifndef CONFIG_GPIO_SYSFS | ||
| 285 | |||
| 286 | struct device; | ||
| 287 | |||
| 288 | /* sysfs support is only available with gpiolib, where it's optional */ | ||
| 289 | |||
| 290 | static inline int gpio_export(unsigned gpio, bool direction_may_change) | ||
| 291 | { | ||
| 292 | return -ENOSYS; | ||
| 293 | } | ||
| 294 | |||
| 295 | static inline int gpio_export_link(struct device *dev, const char *name, | ||
| 296 | unsigned gpio) | ||
| 297 | { | ||
| 298 | return -ENOSYS; | ||
| 299 | } | ||
| 300 | |||
| 301 | static inline int gpio_sysfs_set_active_low(unsigned gpio, int value) | ||
| 302 | { | ||
| 303 | return -ENOSYS; | ||
| 304 | } | ||
| 305 | |||
| 306 | static inline void gpio_unexport(unsigned gpio) | ||
| 307 | { | ||
| 308 | } | ||
| 309 | #endif /* CONFIG_GPIO_SYSFS */ | ||
| 310 | |||
| 311 | #endif /* _ASM_GENERIC_GPIO_H */ | 213 | #endif /* _ASM_GENERIC_GPIO_H */ |
diff --git a/include/linux/gpio.h b/include/linux/gpio.h index a06ec3e85ba3..c691df044458 100644 --- a/include/linux/gpio.h +++ b/include/linux/gpio.h | |||
| @@ -16,14 +16,17 @@ | |||
| 16 | #define GPIOF_OUT_INIT_LOW (GPIOF_DIR_OUT | GPIOF_INIT_LOW) | 16 | #define GPIOF_OUT_INIT_LOW (GPIOF_DIR_OUT | GPIOF_INIT_LOW) |
| 17 | #define GPIOF_OUT_INIT_HIGH (GPIOF_DIR_OUT | GPIOF_INIT_HIGH) | 17 | #define GPIOF_OUT_INIT_HIGH (GPIOF_DIR_OUT | GPIOF_INIT_HIGH) |
| 18 | 18 | ||
| 19 | /* Gpio pin is active-low */ | ||
| 20 | #define GPIOF_ACTIVE_LOW (1 << 2) | ||
| 21 | |||
| 19 | /* Gpio pin is open drain */ | 22 | /* Gpio pin is open drain */ |
| 20 | #define GPIOF_OPEN_DRAIN (1 << 2) | 23 | #define GPIOF_OPEN_DRAIN (1 << 3) |
| 21 | 24 | ||
| 22 | /* Gpio pin is open source */ | 25 | /* Gpio pin is open source */ |
| 23 | #define GPIOF_OPEN_SOURCE (1 << 3) | 26 | #define GPIOF_OPEN_SOURCE (1 << 4) |
| 24 | 27 | ||
| 25 | #define GPIOF_EXPORT (1 << 4) | 28 | #define GPIOF_EXPORT (1 << 5) |
| 26 | #define GPIOF_EXPORT_CHANGEABLE (1 << 5) | 29 | #define GPIOF_EXPORT_CHANGEABLE (1 << 6) |
| 27 | #define GPIOF_EXPORT_DIR_FIXED (GPIOF_EXPORT) | 30 | #define GPIOF_EXPORT_DIR_FIXED (GPIOF_EXPORT) |
| 28 | #define GPIOF_EXPORT_DIR_CHANGEABLE (GPIOF_EXPORT | GPIOF_EXPORT_CHANGEABLE) | 31 | #define GPIOF_EXPORT_DIR_CHANGEABLE (GPIOF_EXPORT | GPIOF_EXPORT_CHANGEABLE) |
| 29 | 32 | ||
diff --git a/include/linux/gpio/consumer.h b/include/linux/gpio/consumer.h new file mode 100644 index 000000000000..2088eb50421c --- /dev/null +++ b/include/linux/gpio/consumer.h | |||
| @@ -0,0 +1,238 @@ | |||
| 1 | #ifndef __LINUX_GPIO_CONSUMER_H | ||
| 2 | #define __LINUX_GPIO_CONSUMER_H | ||
| 3 | |||
| 4 | #include <linux/err.h> | ||
| 5 | #include <linux/kernel.h> | ||
| 6 | |||
| 7 | #ifdef CONFIG_GPIOLIB | ||
| 8 | |||
| 9 | struct device; | ||
| 10 | struct gpio_chip; | ||
| 11 | |||
| 12 | /** | ||
| 13 | * Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are | ||
| 14 | * preferable to the old integer-based handles. | ||
| 15 | * | ||
| 16 | * Contrary to integers, a pointer to a gpio_desc is guaranteed to be valid | ||
| 17 | * until the GPIO is released. | ||
| 18 | */ | ||
| 19 | struct gpio_desc; | ||
| 20 | |||
| 21 | int gpiod_get_direction(const struct gpio_desc *desc); | ||
| 22 | int gpiod_direction_input(struct gpio_desc *desc); | ||
| 23 | int gpiod_direction_output(struct gpio_desc *desc, int value); | ||
| 24 | |||
| 25 | /* Value get/set from non-sleeping context */ | ||
| 26 | int gpiod_get_value(const struct gpio_desc *desc); | ||
| 27 | void gpiod_set_value(struct gpio_desc *desc, int value); | ||
| 28 | int gpiod_get_raw_value(const struct gpio_desc *desc); | ||
| 29 | void gpiod_set_raw_value(struct gpio_desc *desc, int value); | ||
| 30 | |||
| 31 | /* Value get/set from sleeping context */ | ||
| 32 | int gpiod_get_value_cansleep(const struct gpio_desc *desc); | ||
| 33 | void gpiod_set_value_cansleep(struct gpio_desc *desc, int value); | ||
| 34 | int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc); | ||
| 35 | void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value); | ||
| 36 | |||
| 37 | int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce); | ||
| 38 | |||
| 39 | int gpiod_is_active_low(const struct gpio_desc *desc); | ||
| 40 | int gpiod_cansleep(const struct gpio_desc *desc); | ||
| 41 | |||
| 42 | int gpiod_to_irq(const struct gpio_desc *desc); | ||
| 43 | |||
| 44 | /* Convert between the old gpio_ and new gpiod_ interfaces */ | ||
| 45 | struct gpio_desc *gpio_to_desc(unsigned gpio); | ||
| 46 | int desc_to_gpio(const struct gpio_desc *desc); | ||
| 47 | struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc); | ||
| 48 | |||
| 49 | #else /* CONFIG_GPIOLIB */ | ||
| 50 | |||
| 51 | static inline struct gpio_desc *__must_check gpiod_get(struct device *dev, | ||
| 52 | const char *con_id) | ||
| 53 | { | ||
| 54 | return ERR_PTR(-ENOSYS); | ||
| 55 | } | ||
| 56 | static inline struct gpio_desc *__must_check gpiod_get_index(struct device *dev, | ||
| 57 | const char *con_id, | ||
| 58 | unsigned int idx) | ||
| 59 | { | ||
| 60 | return ERR_PTR(-ENOSYS); | ||
| 61 | } | ||
| 62 | static inline void gpiod_put(struct gpio_desc *desc) | ||
| 63 | { | ||
| 64 | might_sleep(); | ||
| 65 | |||
| 66 | /* GPIO can never have been requested */ | ||
| 67 | WARN_ON(1); | ||
| 68 | } | ||
| 69 | |||
| 70 | static inline struct gpio_desc *__must_check devm_gpiod_get(struct device *dev, | ||
| 71 | const char *con_id) | ||
| 72 | { | ||
| 73 | return ERR_PTR(-ENOSYS); | ||
| 74 | } | ||
| 75 | static inline | ||
| 76 | struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, | ||
| 77 | const char *con_id, | ||
| 78 | unsigned int idx) | ||
| 79 | { | ||
| 80 | return ERR_PTR(-ENOSYS); | ||
| 81 | } | ||
| 82 | static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc) | ||
| 83 | { | ||
| 84 | might_sleep(); | ||
| 85 | |||
| 86 | /* GPIO can never have been requested */ | ||
| 87 | WARN_ON(1); | ||
| 88 | } | ||
| 89 | |||
| 90 | |||
| 91 | static inline int gpiod_get_direction(const struct gpio_desc *desc) | ||
| 92 | { | ||
| 93 | /* GPIO can never have been requested */ | ||
| 94 | WARN_ON(1); | ||
| 95 | return -ENOSYS; | ||
| 96 | } | ||
| 97 | static inline int gpiod_direction_input(struct gpio_desc *desc) | ||
| 98 | { | ||
| 99 | /* GPIO can never have been requested */ | ||
| 100 | WARN_ON(1); | ||
| 101 | return -ENOSYS; | ||
| 102 | } | ||
| 103 | static inline int gpiod_direction_output(struct gpio_desc *desc, int value) | ||
| 104 | { | ||
| 105 | /* GPIO can never have been requested */ | ||
| 106 | WARN_ON(1); | ||
| 107 | return -ENOSYS; | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | static inline int gpiod_get_value(const struct gpio_desc *desc) | ||
| 112 | { | ||
| 113 | /* GPIO can never have been requested */ | ||
| 114 | WARN_ON(1); | ||
| 115 | return 0; | ||
| 116 | } | ||
| 117 | static inline void gpiod_set_value(struct gpio_desc *desc, int value) | ||
| 118 | { | ||
| 119 | /* GPIO can never have been requested */ | ||
| 120 | WARN_ON(1); | ||
| 121 | } | ||
| 122 | static inline int gpiod_get_raw_value(const struct gpio_desc *desc) | ||
| 123 | { | ||
| 124 | /* GPIO can never have been requested */ | ||
| 125 | WARN_ON(1); | ||
| 126 | return 0; | ||
| 127 | } | ||
| 128 | static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value) | ||
| 129 | { | ||
| 130 | /* GPIO can never have been requested */ | ||
| 131 | WARN_ON(1); | ||
| 132 | } | ||
| 133 | |||
| 134 | static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc) | ||
| 135 | { | ||
| 136 | /* GPIO can never have been requested */ | ||
| 137 | WARN_ON(1); | ||
| 138 | return 0; | ||
| 139 | } | ||
| 140 | static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) | ||
| 141 | { | ||
| 142 | /* GPIO can never have been requested */ | ||
| 143 | WARN_ON(1); | ||
| 144 | } | ||
| 145 | static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) | ||
| 146 | { | ||
| 147 | /* GPIO can never have been requested */ | ||
| 148 | WARN_ON(1); | ||
| 149 | return 0; | ||
| 150 | } | ||
| 151 | static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, | ||
| 152 | int value) | ||
| 153 | { | ||
| 154 | /* GPIO can never have been requested */ | ||
| 155 | WARN_ON(1); | ||
| 156 | } | ||
| 157 | |||
| 158 | static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) | ||
| 159 | { | ||
| 160 | /* GPIO can never have been requested */ | ||
| 161 | WARN_ON(1); | ||
| 162 | return -ENOSYS; | ||
| 163 | } | ||
| 164 | |||
| 165 | static inline int gpiod_is_active_low(const struct gpio_desc *desc) | ||
| 166 | { | ||
| 167 | /* GPIO can never have been requested */ | ||
| 168 | WARN_ON(1); | ||
| 169 | return 0; | ||
| 170 | } | ||
| 171 | static inline int gpiod_cansleep(const struct gpio_desc *desc) | ||
| 172 | { | ||
| 173 | /* GPIO can never have been requested */ | ||
| 174 | WARN_ON(1); | ||
| 175 | return 0; | ||
| 176 | } | ||
| 177 | |||
| 178 | static inline int gpiod_to_irq(const struct gpio_desc *desc) | ||
| 179 | { | ||
| 180 | /* GPIO can never have been requested */ | ||
| 181 | WARN_ON(1); | ||
| 182 | return -EINVAL; | ||
| 183 | } | ||
| 184 | |||
| 185 | static inline struct gpio_desc *gpio_to_desc(unsigned gpio) | ||
| 186 | { | ||
| 187 | return ERR_PTR(-EINVAL); | ||
| 188 | } | ||
| 189 | static inline int desc_to_gpio(const struct gpio_desc *desc) | ||
| 190 | { | ||
| 191 | /* GPIO can never have been requested */ | ||
| 192 | WARN_ON(1); | ||
| 193 | return -EINVAL; | ||
| 194 | } | ||
| 195 | static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) | ||
| 196 | { | ||
| 197 | /* GPIO can never have been requested */ | ||
| 198 | WARN_ON(1); | ||
| 199 | return ERR_PTR(-ENODEV); | ||
| 200 | } | ||
| 201 | |||
| 202 | |||
| 203 | #endif /* CONFIG_GPIOLIB */ | ||
| 204 | |||
| 205 | #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS) | ||
| 206 | |||
| 207 | int gpiod_export(struct gpio_desc *desc, bool direction_may_change); | ||
| 208 | int gpiod_export_link(struct device *dev, const char *name, | ||
| 209 | struct gpio_desc *desc); | ||
| 210 | int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value); | ||
| 211 | void gpiod_unexport(struct gpio_desc *desc); | ||
| 212 | |||
| 213 | #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ | ||
| 214 | |||
| 215 | static inline int gpiod_export(struct gpio_desc *desc, | ||
| 216 | bool direction_may_change) | ||
| 217 | { | ||
| 218 | return -ENOSYS; | ||
| 219 | } | ||
| 220 | |||
| 221 | static inline int gpiod_export_link(struct device *dev, const char *name, | ||
| 222 | struct gpio_desc *desc) | ||
| 223 | { | ||
| 224 | return -ENOSYS; | ||
| 225 | } | ||
| 226 | |||
| 227 | static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) | ||
| 228 | { | ||
| 229 | return -ENOSYS; | ||
| 230 | } | ||
| 231 | |||
| 232 | static inline void gpiod_unexport(struct gpio_desc *desc) | ||
| 233 | { | ||
| 234 | } | ||
| 235 | |||
| 236 | #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ | ||
| 237 | |||
| 238 | #endif | ||
diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h new file mode 100644 index 000000000000..5dc172c72f0f --- /dev/null +++ b/include/linux/gpio/driver.h | |||
| @@ -0,0 +1,127 @@ | |||
| 1 | #ifndef __LINUX_GPIO_DRIVER_H | ||
| 2 | #define __LINUX_GPIO_DRIVER_H | ||
| 3 | |||
| 4 | #include <linux/types.h> | ||
| 5 | |||
| 6 | struct device; | ||
| 7 | struct gpio_desc; | ||
| 8 | |||
| 9 | /** | ||
| 10 | * struct gpio_chip - abstract a GPIO controller | ||
| 11 | * @label: for diagnostics | ||
| 12 | * @dev: optional device providing the GPIOs | ||
| 13 | * @owner: helps prevent removal of modules exporting active GPIOs | ||
| 14 | * @list: links gpio_chips together for traversal | ||
| 15 | * @request: optional hook for chip-specific activation, such as | ||
| 16 | * enabling module power and clock; may sleep | ||
| 17 | * @free: optional hook for chip-specific deactivation, such as | ||
| 18 | * disabling module power and clock; may sleep | ||
| 19 | * @get_direction: returns direction for signal "offset", 0=out, 1=in, | ||
| 20 | * (same as GPIOF_DIR_XXX), or negative error | ||
| 21 | * @direction_input: configures signal "offset" as input, or returns error | ||
| 22 | * @direction_output: configures signal "offset" as output, or returns error | ||
| 23 | * @get: returns value for signal "offset"; for output signals this | ||
| 24 | * returns either the value actually sensed, or zero | ||
| 25 | * @set: assigns output value for signal "offset" | ||
| 26 | * @set_debounce: optional hook for setting debounce time for specified gpio in | ||
| 27 | * interrupt triggered gpio chips | ||
| 28 | * @to_irq: optional hook supporting non-static gpio_to_irq() mappings; | ||
| 29 | * implementation may not sleep | ||
| 30 | * @dbg_show: optional routine to show contents in debugfs; default code | ||
| 31 | * will be used when this is omitted, but custom code can show extra | ||
| 32 | * state (such as pullup/pulldown configuration). | ||
| 33 | * @base: identifies the first GPIO number handled by this chip; or, if | ||
| 34 | * negative during registration, requests dynamic ID allocation. | ||
| 35 | * @ngpio: the number of GPIOs handled by this controller; the last GPIO | ||
| 36 | * handled is (base + ngpio - 1). | ||
| 37 | * @desc: array of ngpio descriptors. Private. | ||
| 38 | * @can_sleep: flag must be set iff get()/set() methods sleep, as they | ||
| 39 | * must while accessing GPIO expander chips over I2C or SPI | ||
| 40 | * @names: if set, must be an array of strings to use as alternative | ||
| 41 | * names for the GPIOs in this chip. Any entry in the array | ||
| 42 | * may be NULL if there is no alias for the GPIO, however the | ||
| 43 | * array must be @ngpio entries long. A name can include a single printk | ||
| 44 | * format specifier for an unsigned int. It is substituted by the actual | ||
| 45 | * number of the gpio. | ||
| 46 | * | ||
| 47 | * A gpio_chip can help platforms abstract various sources of GPIOs so | ||
| 48 | * they can all be accessed through a common programing interface. | ||
| 49 | * Example sources would be SOC controllers, FPGAs, multifunction | ||
| 50 | * chips, dedicated GPIO expanders, and so on. | ||
| 51 | * | ||
| 52 | * Each chip controls a number of signals, identified in method calls | ||
| 53 | * by "offset" values in the range 0..(@ngpio - 1). When those signals | ||
| 54 | * are referenced through calls like gpio_get_value(gpio), the offset | ||
| 55 | * is calculated by subtracting @base from the gpio number. | ||
| 56 | */ | ||
| 57 | struct gpio_chip { | ||
| 58 | const char *label; | ||
| 59 | struct device *dev; | ||
| 60 | struct module *owner; | ||
| 61 | struct list_head list; | ||
| 62 | |||
| 63 | int (*request)(struct gpio_chip *chip, | ||
| 64 | unsigned offset); | ||
| 65 | void (*free)(struct gpio_chip *chip, | ||
| 66 | unsigned offset); | ||
| 67 | int (*get_direction)(struct gpio_chip *chip, | ||
| 68 | unsigned offset); | ||
| 69 | int (*direction_input)(struct gpio_chip *chip, | ||
| 70 | unsigned offset); | ||
| 71 | int (*direction_output)(struct gpio_chip *chip, | ||
| 72 | unsigned offset, int value); | ||
| 73 | int (*get)(struct gpio_chip *chip, | ||
| 74 | unsigned offset); | ||
| 75 | void (*set)(struct gpio_chip *chip, | ||
| 76 | unsigned offset, int value); | ||
| 77 | int (*set_debounce)(struct gpio_chip *chip, | ||
| 78 | unsigned offset, | ||
| 79 | unsigned debounce); | ||
| 80 | |||
| 81 | int (*to_irq)(struct gpio_chip *chip, | ||
| 82 | unsigned offset); | ||
| 83 | |||
| 84 | void (*dbg_show)(struct seq_file *s, | ||
| 85 | struct gpio_chip *chip); | ||
| 86 | int base; | ||
| 87 | u16 ngpio; | ||
| 88 | struct gpio_desc *desc; | ||
| 89 | const char *const *names; | ||
| 90 | unsigned can_sleep:1; | ||
| 91 | unsigned exported:1; | ||
| 92 | |||
| 93 | #if defined(CONFIG_OF_GPIO) | ||
| 94 | /* | ||
| 95 | * If CONFIG_OF is enabled, then all GPIO controllers described in the | ||
| 96 | * device tree automatically may have an OF translation | ||
| 97 | */ | ||
| 98 | struct device_node *of_node; | ||
| 99 | int of_gpio_n_cells; | ||
| 100 | int (*of_xlate)(struct gpio_chip *gc, | ||
| 101 | const struct of_phandle_args *gpiospec, u32 *flags); | ||
| 102 | #endif | ||
| 103 | #ifdef CONFIG_PINCTRL | ||
| 104 | /* | ||
| 105 | * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally | ||
| 106 | * describe the actual pin range which they serve in an SoC. This | ||
| 107 | * information would be used by pinctrl subsystem to configure | ||
| 108 | * corresponding pins for gpio usage. | ||
| 109 | */ | ||
| 110 | struct list_head pin_ranges; | ||
| 111 | #endif | ||
| 112 | }; | ||
| 113 | |||
| 114 | extern const char *gpiochip_is_requested(struct gpio_chip *chip, | ||
| 115 | unsigned offset); | ||
| 116 | |||
| 117 | /* add/remove chips */ | ||
| 118 | extern int gpiochip_add(struct gpio_chip *chip); | ||
| 119 | extern int __must_check gpiochip_remove(struct gpio_chip *chip); | ||
| 120 | extern struct gpio_chip *gpiochip_find(void *data, | ||
| 121 | int (*match)(struct gpio_chip *chip, void *data)); | ||
| 122 | |||
| 123 | /* lock/unlock as IRQ */ | ||
| 124 | int gpiod_lock_as_irq(struct gpio_desc *desc); | ||
| 125 | void gpiod_unlock_as_irq(struct gpio_desc *desc); | ||
| 126 | |||
| 127 | #endif | ||
