aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpio/gpiolib.c
diff options
context:
space:
mode:
authorAlexandre Courbot <acourbot@nvidia.com>2013-10-17 13:21:36 -0400
committerLinus Walleij <linus.walleij@linaro.org>2013-10-19 17:24:45 -0400
commit79a9becda8940deb2274b5aa4577c86d52ee7ecb (patch)
tree2e10b344abcf76446ff97855d280fe9b7fb34ef0 /drivers/gpio/gpiolib.c
parentb41fb43911b4cb864812adec88d028cc6219f23e (diff)
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>
Diffstat (limited to 'drivers/gpio/gpiolib.c')
-rw-r--r--drivers/gpio/gpiolib.c422
1 files changed, 224 insertions, 198 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);
81static DEFINE_IDR(dirent_idr); 76static 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 */
88static int gpiod_request(struct gpio_desc *desc, const char *label); 79static int gpiod_request(struct gpio_desc *desc, const char *label);
89static void gpiod_free(struct gpio_desc *desc); 80static void gpiod_free(struct gpio_desc *desc);
90static int gpiod_direction_input(struct gpio_desc *desc);
91static int gpiod_direction_output(struct gpio_desc *desc, int value);
92static int gpiod_get_direction(const struct gpio_desc *desc);
93static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
94static int gpiod_get_value_cansleep(const struct gpio_desc *desc);
95static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
96static int gpiod_get_value(const struct gpio_desc *desc);
97static void gpiod_set_value(struct gpio_desc *desc, int value);
98static int gpiod_cansleep(const struct gpio_desc *desc);
99static int gpiod_to_irq(const struct gpio_desc *desc);
100static int gpiod_lock_as_irq(struct gpio_desc *desc);
101static void gpiod_unlock_as_irq(struct gpio_desc *desc);
102static int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
103static int gpiod_export_link(struct device *dev, const char *name,
104 struct gpio_desc *desc);
105static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
106static 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 */
160static struct gpio_desc *gpio_to_desc(unsigned gpio) 134struct 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}
141EXPORT_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 */
184static int desc_to_gpio(const struct gpio_desc *desc) 159int desc_to_gpio(const struct gpio_desc *desc)
185{ 160{
186 return desc - &gpio_desc[0]; 161 return desc - &gpio_desc[0];
187} 162}
163EXPORT_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
222static 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 */
202struct 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 206EXPORT_SYMBOL_GPL(gpiod_to_chip);
227/* caller holds gpio_lock *OR* gpio is marked as requested */
228struct 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 */
234static int gpiochip_find_base(int ngpio) 209static 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/**
258static 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 */
240int 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}
266EXPORT_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 */
807static int gpiod_export(struct gpio_desc *desc, bool direction_may_change) 781int 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 859EXPORT_SYMBOL_GPL(gpiod_export);
886int gpio_export(unsigned gpio, bool direction_may_change)
887{
888 return gpiod_export(gpio_to_desc(gpio), direction_may_change);
889}
890EXPORT_SYMBOL_GPL(gpio_export);
891 860
892static int match_export(struct device *dev, const void *data) 861static 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 */
908static int gpiod_export_link(struct device *dev, const char *name, 877int 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 909EXPORT_SYMBOL_GPL(gpiod_export_link);
941int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
942{
943 return gpiod_export_link(dev, name, gpio_to_desc(gpio));
944}
945EXPORT_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 */
959static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) 923int 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 954EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low);
991int gpio_sysfs_set_active_low(unsigned gpio, int value)
992{
993 return gpiod_sysfs_set_active_low(gpio_to_desc(gpio), value);
994}
995EXPORT_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 */
1003static void gpiod_unexport(struct gpio_desc *desc) 962void 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 995EXPORT_SYMBOL_GPL(gpiod_unexport);
1037void gpio_unexport(unsigned gpio)
1038{
1039 gpiod_unexport(gpio_to_desc(gpio));
1040}
1041EXPORT_SYMBOL_GPL(gpio_unexport);
1042 996
1043static int gpiochip_export(struct gpio_chip *chip) 997static 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
1148static inline int gpiod_export(struct gpio_desc *desc,
1149 bool direction_may_change)
1150{
1151 return -ENOSYS;
1152}
1153
1154static inline int gpiod_export_link(struct device *dev, const char *name,
1155 struct gpio_desc *desc)
1156{
1157 return -ENOSYS;
1158}
1159
1160static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
1161{
1162 return -ENOSYS;
1163}
1164
1165static 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
1680static 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 */
1622int 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}
1679EXPORT_SYMBOL_GPL(gpiod_direction_input);
1737 1680
1738int 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
1742EXPORT_SYMBOL_GPL(gpio_direction_input); 1685 *
1743 1686 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1744static 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 */
1691int 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 1764EXPORT_SYMBOL_GPL(gpiod_direction_output);
1818int gpio_direction_output(unsigned gpio, int value)
1819{
1820 return gpiod_direction_output(gpio_to_desc(gpio), value);
1821}
1822EXPORT_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 */
1832static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) 1774int 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}
1816EXPORT_SYMBOL_GPL(gpiod_set_debounce);
1874 1817
1875int 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 */
1824int 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}
1879EXPORT_SYMBOL_GPL(gpio_set_debounce); 1828EXPORT_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
1903static int _gpiod_get_value(const struct gpio_desc *desc) 1852static 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 */
1925static int gpiod_get_value(const struct gpio_desc *desc) 1875int 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}
1883EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
1933 1884
1934int __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 */
1895int 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}
1938EXPORT_SYMBOL_GPL(__gpio_get_value); 1909EXPORT_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 */
1946static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value) 1916static 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 */
1974static void _gpio_set_open_source_value(struct gpio_desc *desc, int value) 1943static 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
1996static void _gpiod_set_value(struct gpio_desc *desc, int value) 1965static 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 */
2019static void gpiod_set_value(struct gpio_desc *desc, int value) 1990void 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}
1998EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
2028 1999
2029void __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 */
2011void 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}
2033EXPORT_SYMBOL_GPL(__gpio_set_value); 2021EXPORT_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 */
2043static int gpiod_cansleep(const struct gpio_desc *desc) 2028int 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 2034EXPORT_SYMBOL_GPL(gpiod_cansleep);
2051int __gpio_cansleep(unsigned gpio)
2052{
2053 return gpiod_cansleep(gpio_to_desc(gpio));
2054}
2055EXPORT_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 */
2066static int gpiod_to_irq(const struct gpio_desc *desc) 2043int 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 2054EXPORT_SYMBOL_GPL(gpiod_to_irq);
2078int __gpio_to_irq(unsigned gpio)
2079{
2080 return gpiod_to_irq(gpio_to_desc(gpio));
2081}
2082EXPORT_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 */
2094static int gpiod_lock_as_irq(struct gpio_desc *desc) 2066int 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}
2081EXPORT_SYMBOL_GPL(gpiod_lock_as_irq);
2109 2082
2110int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset) 2083int 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 */
2123static void gpiod_unlock_as_irq(struct gpio_desc *desc) 2096void 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}
2103EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq);
2130 2104
2131void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset) 2105void 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}
2135EXPORT_SYMBOL_GPL(gpio_unlock_as_irq); 2109EXPORT_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 2120int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
2141static 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}
2127EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
2148 2128
2149int 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 */
2138int 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}
2153EXPORT_SYMBOL_GPL(gpio_get_value_cansleep); 2152EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
2154 2153
2155static 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 */
2164void 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}
2171EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
2162 2172
2163void 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 */
2183void 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}
2167EXPORT_SYMBOL_GPL(gpio_set_value_cansleep); 2193EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
2168 2194
2169#ifdef CONFIG_DEBUG_FS 2195#ifdef CONFIG_DEBUG_FS
2170 2196