Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

gpiolib: export descriptor-based GPIO interface

This patch exports the gpiod_* family of API functions, a safer
alternative to the legacy GPIO interface. Differences between the gpiod
and legacy gpio APIs are:

- gpio works with integers, whereas gpiod operates on opaque handlers
which cannot be forged or used before proper acquisition
- gpiod get/set functions are aware of the active low state of a GPIO
- gpio consumers should now include <linux/gpio/consumer.h> to access
the new interface, whereas chips drivers will use
<linux/gpio/driver.h>

The legacy gpio API is now built as inline functions on top of gpiod.

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

authored by

Alexandre Courbot and committed by
Linus Walleij
79a9becd b41fb439

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