diff options
Diffstat (limited to 'drivers/gpio/gpiolib.c')
-rw-r--r-- | drivers/gpio/gpiolib.c | 828 |
1 files changed, 594 insertions, 234 deletions
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 4a34ca9c1768..7dd446150294 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c | |||
@@ -10,22 +10,18 @@ | |||
10 | #include <linux/seq_file.h> | 10 | #include <linux/seq_file.h> |
11 | #include <linux/gpio.h> | 11 | #include <linux/gpio.h> |
12 | #include <linux/of_gpio.h> | 12 | #include <linux/of_gpio.h> |
13 | #include <linux/acpi_gpio.h> | ||
13 | #include <linux/idr.h> | 14 | #include <linux/idr.h> |
14 | #include <linux/slab.h> | 15 | #include <linux/slab.h> |
15 | 16 | ||
16 | #define CREATE_TRACE_POINTS | 17 | #define CREATE_TRACE_POINTS |
17 | #include <trace/events/gpio.h> | 18 | #include <trace/events/gpio.h> |
18 | 19 | ||
19 | /* Optional implementation infrastructure for GPIO interfaces. | 20 | /* Implementation infrastructure for GPIO interfaces. |
20 | * | 21 | * |
21 | * Platforms may want to use this if they tend to use very many GPIOs | 22 | * 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. | 23 | * get/set operations for common cases, so that access to SOC-integrated |
23 | * | 24 | * 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 | */ | 25 | */ |
30 | 26 | ||
31 | 27 | ||
@@ -57,9 +53,10 @@ struct gpio_desc { | |||
57 | #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */ | 53 | #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */ |
58 | #define FLAG_TRIG_FALL 4 /* trigger on falling edge */ | 54 | #define FLAG_TRIG_FALL 4 /* trigger on falling edge */ |
59 | #define FLAG_TRIG_RISE 5 /* trigger on rising edge */ | 55 | #define FLAG_TRIG_RISE 5 /* trigger on rising edge */ |
60 | #define FLAG_ACTIVE_LOW 6 /* sysfs value has active low */ | 56 | #define FLAG_ACTIVE_LOW 6 /* value has active low */ |
61 | #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ | 57 | #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ |
62 | #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ | 58 | #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ |
59 | #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ | ||
63 | 60 | ||
64 | #define ID_SHIFT 16 /* add new flags before this one */ | 61 | #define ID_SHIFT 16 /* add new flags before this one */ |
65 | 62 | ||
@@ -74,34 +71,50 @@ static struct gpio_desc gpio_desc[ARCH_NR_GPIOS]; | |||
74 | 71 | ||
75 | #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio) | 72 | #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio) |
76 | 73 | ||
74 | static DEFINE_MUTEX(gpio_lookup_lock); | ||
75 | static LIST_HEAD(gpio_lookup_list); | ||
77 | static LIST_HEAD(gpio_chips); | 76 | static LIST_HEAD(gpio_chips); |
78 | 77 | ||
79 | #ifdef CONFIG_GPIO_SYSFS | 78 | #ifdef CONFIG_GPIO_SYSFS |
80 | static DEFINE_IDR(dirent_idr); | 79 | static DEFINE_IDR(dirent_idr); |
81 | #endif | 80 | #endif |
82 | 81 | ||
83 | /* | ||
84 | * Internal gpiod_* API using descriptors instead of the integer namespace. | ||
85 | * Most of this should eventually go public. | ||
86 | */ | ||
87 | static int gpiod_request(struct gpio_desc *desc, const char *label); | 82 | static int gpiod_request(struct gpio_desc *desc, const char *label); |
88 | static void gpiod_free(struct gpio_desc *desc); | 83 | static void gpiod_free(struct gpio_desc *desc); |
89 | static int gpiod_direction_input(struct gpio_desc *desc); | ||
90 | static int gpiod_direction_output(struct gpio_desc *desc, int value); | ||
91 | static int gpiod_get_direction(const struct gpio_desc *desc); | ||
92 | static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce); | ||
93 | static int gpiod_get_value_cansleep(const struct gpio_desc *desc); | ||
94 | static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value); | ||
95 | static int gpiod_get_value(const struct gpio_desc *desc); | ||
96 | static void gpiod_set_value(struct gpio_desc *desc, int value); | ||
97 | static int gpiod_cansleep(const struct gpio_desc *desc); | ||
98 | static int gpiod_to_irq(const struct gpio_desc *desc); | ||
99 | static int gpiod_export(struct gpio_desc *desc, bool direction_may_change); | ||
100 | static int gpiod_export_link(struct device *dev, const char *name, | ||
101 | struct gpio_desc *desc); | ||
102 | static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value); | ||
103 | static void gpiod_unexport(struct gpio_desc *desc); | ||
104 | 84 | ||
85 | #ifdef CONFIG_DEBUG_FS | ||
86 | #define gpiod_emerg(desc, fmt, ...) \ | ||
87 | pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ | ||
88 | ##__VA_ARGS__) | ||
89 | #define gpiod_crit(desc, fmt, ...) \ | ||
90 | pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ | ||
91 | ##__VA_ARGS__) | ||
92 | #define gpiod_err(desc, fmt, ...) \ | ||
93 | pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ | ||
94 | ##__VA_ARGS__) | ||
95 | #define gpiod_warn(desc, fmt, ...) \ | ||
96 | pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ | ||
97 | ##__VA_ARGS__) | ||
98 | #define gpiod_info(desc, fmt, ...) \ | ||
99 | pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ | ||
100 | ##__VA_ARGS__) | ||
101 | #define gpiod_dbg(desc, fmt, ...) \ | ||
102 | pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ | ||
103 | ##__VA_ARGS__) | ||
104 | #else | ||
105 | #define gpiod_emerg(desc, fmt, ...) \ | ||
106 | pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) | ||
107 | #define gpiod_crit(desc, fmt, ...) \ | ||
108 | pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) | ||
109 | #define gpiod_err(desc, fmt, ...) \ | ||
110 | pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) | ||
111 | #define gpiod_warn(desc, fmt, ...) \ | ||
112 | pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) | ||
113 | #define gpiod_info(desc, fmt, ...) \ | ||
114 | pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) | ||
115 | #define gpiod_dbg(desc, fmt, ...) \ | ||
116 | pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) | ||
117 | #endif | ||
105 | 118 | ||
106 | static inline void desc_set_label(struct gpio_desc *d, const char *label) | 119 | static inline void desc_set_label(struct gpio_desc *d, const char *label) |
107 | { | 120 | { |
@@ -121,23 +134,36 @@ static int gpio_chip_hwgpio(const struct gpio_desc *desc) | |||
121 | /** | 134 | /** |
122 | * Convert a GPIO number to its descriptor | 135 | * Convert a GPIO number to its descriptor |
123 | */ | 136 | */ |
124 | static struct gpio_desc *gpio_to_desc(unsigned gpio) | 137 | struct gpio_desc *gpio_to_desc(unsigned gpio) |
125 | { | 138 | { |
126 | if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio)) | 139 | if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio)) |
127 | return NULL; | 140 | return NULL; |
128 | else | 141 | else |
129 | return &gpio_desc[gpio]; | 142 | return &gpio_desc[gpio]; |
130 | } | 143 | } |
144 | EXPORT_SYMBOL_GPL(gpio_to_desc); | ||
145 | |||
146 | /** | ||
147 | * Convert an offset on a certain chip to a corresponding descriptor | ||
148 | */ | ||
149 | static struct gpio_desc *gpiochip_offset_to_desc(struct gpio_chip *chip, | ||
150 | unsigned int offset) | ||
151 | { | ||
152 | unsigned int gpio = chip->base + offset; | ||
153 | |||
154 | return gpio_to_desc(gpio); | ||
155 | } | ||
131 | 156 | ||
132 | /** | 157 | /** |
133 | * Convert a GPIO descriptor to the integer namespace. | 158 | * Convert a GPIO descriptor to the integer namespace. |
134 | * This should disappear in the future but is needed since we still | 159 | * This should disappear in the future but is needed since we still |
135 | * use GPIO numbers for error messages and sysfs nodes | 160 | * use GPIO numbers for error messages and sysfs nodes |
136 | */ | 161 | */ |
137 | static int desc_to_gpio(const struct gpio_desc *desc) | 162 | int desc_to_gpio(const struct gpio_desc *desc) |
138 | { | 163 | { |
139 | return desc - &gpio_desc[0]; | 164 | return desc - &gpio_desc[0]; |
140 | } | 165 | } |
166 | EXPORT_SYMBOL_GPL(desc_to_gpio); | ||
141 | 167 | ||
142 | 168 | ||
143 | /* Warn when drivers omit gpio_request() calls -- legal but ill-advised | 169 | /* Warn when drivers omit gpio_request() calls -- legal but ill-advised |
@@ -172,16 +198,15 @@ static int gpio_ensure_requested(struct gpio_desc *desc) | |||
172 | return 0; | 198 | return 0; |
173 | } | 199 | } |
174 | 200 | ||
175 | static struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) | 201 | /** |
202 | * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs | ||
203 | * @desc: descriptor to return the chip of | ||
204 | */ | ||
205 | struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) | ||
176 | { | 206 | { |
177 | return desc ? desc->chip : NULL; | 207 | return desc ? desc->chip : NULL; |
178 | } | 208 | } |
179 | 209 | EXPORT_SYMBOL_GPL(gpiod_to_chip); | |
180 | /* caller holds gpio_lock *OR* gpio is marked as requested */ | ||
181 | struct gpio_chip *gpio_to_chip(unsigned gpio) | ||
182 | { | ||
183 | return gpiod_to_chip(gpio_to_desc(gpio)); | ||
184 | } | ||
185 | 210 | ||
186 | /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ | 211 | /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ |
187 | static int gpiochip_find_base(int ngpio) | 212 | static int gpiochip_find_base(int ngpio) |
@@ -207,8 +232,15 @@ static int gpiochip_find_base(int ngpio) | |||
207 | } | 232 | } |
208 | } | 233 | } |
209 | 234 | ||
210 | /* caller ensures gpio is valid and requested, chip->get_direction may sleep */ | 235 | /** |
211 | static int gpiod_get_direction(const struct gpio_desc *desc) | 236 | * gpiod_get_direction - return the current direction of a GPIO |
237 | * @desc: GPIO to get the direction of | ||
238 | * | ||
239 | * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error. | ||
240 | * | ||
241 | * This function may sleep if gpiod_cansleep() is true. | ||
242 | */ | ||
243 | int gpiod_get_direction(const struct gpio_desc *desc) | ||
212 | { | 244 | { |
213 | struct gpio_chip *chip; | 245 | struct gpio_chip *chip; |
214 | unsigned offset; | 246 | unsigned offset; |
@@ -234,6 +266,7 @@ static int gpiod_get_direction(const struct gpio_desc *desc) | |||
234 | } | 266 | } |
235 | return status; | 267 | return status; |
236 | } | 268 | } |
269 | EXPORT_SYMBOL_GPL(gpiod_get_direction); | ||
237 | 270 | ||
238 | #ifdef CONFIG_GPIO_SYSFS | 271 | #ifdef CONFIG_GPIO_SYSFS |
239 | 272 | ||
@@ -318,17 +351,10 @@ static ssize_t gpio_value_show(struct device *dev, | |||
318 | 351 | ||
319 | mutex_lock(&sysfs_lock); | 352 | mutex_lock(&sysfs_lock); |
320 | 353 | ||
321 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { | 354 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
322 | status = -EIO; | 355 | status = -EIO; |
323 | } else { | 356 | else |
324 | int value; | 357 | status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc)); |
325 | |||
326 | value = !!gpiod_get_value_cansleep(desc); | ||
327 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | ||
328 | value = !value; | ||
329 | |||
330 | status = sprintf(buf, "%d\n", value); | ||
331 | } | ||
332 | 358 | ||
333 | mutex_unlock(&sysfs_lock); | 359 | mutex_unlock(&sysfs_lock); |
334 | return status; | 360 | return status; |
@@ -351,9 +377,7 @@ static ssize_t gpio_value_store(struct device *dev, | |||
351 | 377 | ||
352 | status = kstrtol(buf, 0, &value); | 378 | status = kstrtol(buf, 0, &value); |
353 | if (status == 0) { | 379 | if (status == 0) { |
354 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | 380 | gpiod_set_value_cansleep(desc, value); |
355 | value = !value; | ||
356 | gpiod_set_value_cansleep(desc, value != 0); | ||
357 | status = size; | 381 | status = size; |
358 | } | 382 | } |
359 | } | 383 | } |
@@ -395,6 +419,7 @@ static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev, | |||
395 | desc->flags &= ~GPIO_TRIGGER_MASK; | 419 | desc->flags &= ~GPIO_TRIGGER_MASK; |
396 | 420 | ||
397 | if (!gpio_flags) { | 421 | if (!gpio_flags) { |
422 | gpiod_unlock_as_irq(desc); | ||
398 | ret = 0; | 423 | ret = 0; |
399 | goto free_id; | 424 | goto free_id; |
400 | } | 425 | } |
@@ -433,6 +458,12 @@ static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev, | |||
433 | if (ret < 0) | 458 | if (ret < 0) |
434 | goto free_id; | 459 | goto free_id; |
435 | 460 | ||
461 | ret = gpiod_lock_as_irq(desc); | ||
462 | if (ret < 0) { | ||
463 | gpiod_warn(desc, "failed to flag the GPIO for IRQ\n"); | ||
464 | goto free_id; | ||
465 | } | ||
466 | |||
436 | desc->flags |= gpio_flags; | 467 | desc->flags |= gpio_flags; |
437 | return 0; | 468 | return 0; |
438 | 469 | ||
@@ -736,7 +767,7 @@ static struct class gpio_class = { | |||
736 | 767 | ||
737 | 768 | ||
738 | /** | 769 | /** |
739 | * gpio_export - export a GPIO through sysfs | 770 | * gpiod_export - export a GPIO through sysfs |
740 | * @gpio: gpio to make available, already requested | 771 | * @gpio: gpio to make available, already requested |
741 | * @direction_may_change: true if userspace may change gpio direction | 772 | * @direction_may_change: true if userspace may change gpio direction |
742 | * Context: arch_initcall or later | 773 | * Context: arch_initcall or later |
@@ -750,7 +781,7 @@ static struct class gpio_class = { | |||
750 | * | 781 | * |
751 | * Returns zero on success, else an error. | 782 | * Returns zero on success, else an error. |
752 | */ | 783 | */ |
753 | static int gpiod_export(struct gpio_desc *desc, bool direction_may_change) | 784 | int gpiod_export(struct gpio_desc *desc, bool direction_may_change) |
754 | { | 785 | { |
755 | unsigned long flags; | 786 | unsigned long flags; |
756 | int status; | 787 | int status; |
@@ -828,12 +859,7 @@ fail_unlock: | |||
828 | status); | 859 | status); |
829 | return status; | 860 | return status; |
830 | } | 861 | } |
831 | 862 | EXPORT_SYMBOL_GPL(gpiod_export); | |
832 | int gpio_export(unsigned gpio, bool direction_may_change) | ||
833 | { | ||
834 | return gpiod_export(gpio_to_desc(gpio), direction_may_change); | ||
835 | } | ||
836 | EXPORT_SYMBOL_GPL(gpio_export); | ||
837 | 863 | ||
838 | static int match_export(struct device *dev, const void *data) | 864 | static int match_export(struct device *dev, const void *data) |
839 | { | 865 | { |
@@ -841,7 +867,7 @@ static int match_export(struct device *dev, const void *data) | |||
841 | } | 867 | } |
842 | 868 | ||
843 | /** | 869 | /** |
844 | * gpio_export_link - create a sysfs link to an exported GPIO node | 870 | * gpiod_export_link - create a sysfs link to an exported GPIO node |
845 | * @dev: device under which to create symlink | 871 | * @dev: device under which to create symlink |
846 | * @name: name of the symlink | 872 | * @name: name of the symlink |
847 | * @gpio: gpio to create symlink to, already exported | 873 | * @gpio: gpio to create symlink to, already exported |
@@ -851,8 +877,8 @@ static int match_export(struct device *dev, const void *data) | |||
851 | * | 877 | * |
852 | * Returns zero on success, else an error. | 878 | * Returns zero on success, else an error. |
853 | */ | 879 | */ |
854 | static int gpiod_export_link(struct device *dev, const char *name, | 880 | int gpiod_export_link(struct device *dev, const char *name, |
855 | struct gpio_desc *desc) | 881 | struct gpio_desc *desc) |
856 | { | 882 | { |
857 | int status = -EINVAL; | 883 | int status = -EINVAL; |
858 | 884 | ||
@@ -883,15 +909,10 @@ static int gpiod_export_link(struct device *dev, const char *name, | |||
883 | 909 | ||
884 | return status; | 910 | return status; |
885 | } | 911 | } |
886 | 912 | EXPORT_SYMBOL_GPL(gpiod_export_link); | |
887 | int gpio_export_link(struct device *dev, const char *name, unsigned gpio) | ||
888 | { | ||
889 | return gpiod_export_link(dev, name, gpio_to_desc(gpio)); | ||
890 | } | ||
891 | EXPORT_SYMBOL_GPL(gpio_export_link); | ||
892 | 913 | ||
893 | /** | 914 | /** |
894 | * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value | 915 | * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value |
895 | * @gpio: gpio to change | 916 | * @gpio: gpio to change |
896 | * @value: non-zero to use active low, i.e. inverted values | 917 | * @value: non-zero to use active low, i.e. inverted values |
897 | * | 918 | * |
@@ -902,7 +923,7 @@ EXPORT_SYMBOL_GPL(gpio_export_link); | |||
902 | * | 923 | * |
903 | * Returns zero on success, else an error. | 924 | * Returns zero on success, else an error. |
904 | */ | 925 | */ |
905 | static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) | 926 | int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) |
906 | { | 927 | { |
907 | struct device *dev = NULL; | 928 | struct device *dev = NULL; |
908 | int status = -EINVAL; | 929 | int status = -EINVAL; |
@@ -933,20 +954,15 @@ unlock: | |||
933 | 954 | ||
934 | return status; | 955 | return status; |
935 | } | 956 | } |
936 | 957 | EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low); | |
937 | int gpio_sysfs_set_active_low(unsigned gpio, int value) | ||
938 | { | ||
939 | return gpiod_sysfs_set_active_low(gpio_to_desc(gpio), value); | ||
940 | } | ||
941 | EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low); | ||
942 | 958 | ||
943 | /** | 959 | /** |
944 | * gpio_unexport - reverse effect of gpio_export() | 960 | * gpiod_unexport - reverse effect of gpio_export() |
945 | * @gpio: gpio to make unavailable | 961 | * @gpio: gpio to make unavailable |
946 | * | 962 | * |
947 | * This is implicit on gpio_free(). | 963 | * This is implicit on gpio_free(). |
948 | */ | 964 | */ |
949 | static void gpiod_unexport(struct gpio_desc *desc) | 965 | void gpiod_unexport(struct gpio_desc *desc) |
950 | { | 966 | { |
951 | int status = 0; | 967 | int status = 0; |
952 | struct device *dev = NULL; | 968 | struct device *dev = NULL; |
@@ -979,12 +995,7 @@ static void gpiod_unexport(struct gpio_desc *desc) | |||
979 | pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), | 995 | pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), |
980 | status); | 996 | status); |
981 | } | 997 | } |
982 | 998 | EXPORT_SYMBOL_GPL(gpiod_unexport); | |
983 | void gpio_unexport(unsigned gpio) | ||
984 | { | ||
985 | gpiod_unexport(gpio_to_desc(gpio)); | ||
986 | } | ||
987 | EXPORT_SYMBOL_GPL(gpio_unexport); | ||
988 | 999 | ||
989 | static int gpiochip_export(struct gpio_chip *chip) | 1000 | static int gpiochip_export(struct gpio_chip *chip) |
990 | { | 1001 | { |
@@ -1091,27 +1102,6 @@ static inline void gpiochip_unexport(struct gpio_chip *chip) | |||
1091 | { | 1102 | { |
1092 | } | 1103 | } |
1093 | 1104 | ||
1094 | static inline int gpiod_export(struct gpio_desc *desc, | ||
1095 | bool direction_may_change) | ||
1096 | { | ||
1097 | return -ENOSYS; | ||
1098 | } | ||
1099 | |||
1100 | static inline int gpiod_export_link(struct device *dev, const char *name, | ||
1101 | struct gpio_desc *desc) | ||
1102 | { | ||
1103 | return -ENOSYS; | ||
1104 | } | ||
1105 | |||
1106 | static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) | ||
1107 | { | ||
1108 | return -ENOSYS; | ||
1109 | } | ||
1110 | |||
1111 | static inline void gpiod_unexport(struct gpio_desc *desc) | ||
1112 | { | ||
1113 | } | ||
1114 | |||
1115 | #endif /* CONFIG_GPIO_SYSFS */ | 1105 | #endif /* CONFIG_GPIO_SYSFS */ |
1116 | 1106 | ||
1117 | /* | 1107 | /* |
@@ -1670,7 +1660,16 @@ EXPORT_SYMBOL_GPL(gpiochip_is_requested); | |||
1670 | * rely on gpio_request() having been called beforehand. | 1660 | * rely on gpio_request() having been called beforehand. |
1671 | */ | 1661 | */ |
1672 | 1662 | ||
1673 | static int gpiod_direction_input(struct gpio_desc *desc) | 1663 | /** |
1664 | * gpiod_direction_input - set the GPIO direction to input | ||
1665 | * @desc: GPIO to set to input | ||
1666 | * | ||
1667 | * Set the direction of the passed GPIO to input, such as gpiod_get_value() can | ||
1668 | * be called safely on it. | ||
1669 | * | ||
1670 | * Return 0 in case of success, else an error code. | ||
1671 | */ | ||
1672 | int gpiod_direction_input(struct gpio_desc *desc) | ||
1674 | { | 1673 | { |
1675 | unsigned long flags; | 1674 | unsigned long flags; |
1676 | struct gpio_chip *chip; | 1675 | struct gpio_chip *chip; |
@@ -1684,8 +1683,9 @@ static int gpiod_direction_input(struct gpio_desc *desc) | |||
1684 | 1683 | ||
1685 | chip = desc->chip; | 1684 | chip = desc->chip; |
1686 | if (!chip->get || !chip->direction_input) { | 1685 | if (!chip->get || !chip->direction_input) { |
1687 | pr_warn("%s: missing get() or direction_input() operations\n", | 1686 | gpiod_warn(desc, |
1688 | __func__); | 1687 | "%s: missing get() or direction_input() operations\n", |
1688 | __func__); | ||
1689 | return -EIO; | 1689 | return -EIO; |
1690 | } | 1690 | } |
1691 | 1691 | ||
@@ -1705,8 +1705,7 @@ static int gpiod_direction_input(struct gpio_desc *desc) | |||
1705 | if (status) { | 1705 | if (status) { |
1706 | status = chip->request(chip, offset); | 1706 | status = chip->request(chip, offset); |
1707 | if (status < 0) { | 1707 | if (status < 0) { |
1708 | pr_debug("GPIO-%d: chip request fail, %d\n", | 1708 | gpiod_dbg(desc, "chip request fail, %d\n", status); |
1709 | desc_to_gpio(desc), status); | ||
1710 | /* and it's not available to anyone else ... | 1709 | /* and it's not available to anyone else ... |
1711 | * gpio_request() is the fully clean solution. | 1710 | * gpio_request() is the fully clean solution. |
1712 | */ | 1711 | */ |
@@ -1724,18 +1723,22 @@ lose: | |||
1724 | fail: | 1723 | fail: |
1725 | spin_unlock_irqrestore(&gpio_lock, flags); | 1724 | spin_unlock_irqrestore(&gpio_lock, flags); |
1726 | if (status) | 1725 | if (status) |
1727 | pr_debug("%s: gpio-%d status %d\n", __func__, | 1726 | gpiod_dbg(desc, "%s status %d\n", __func__, status); |
1728 | desc_to_gpio(desc), status); | ||
1729 | return status; | 1727 | return status; |
1730 | } | 1728 | } |
1729 | EXPORT_SYMBOL_GPL(gpiod_direction_input); | ||
1731 | 1730 | ||
1732 | int gpio_direction_input(unsigned gpio) | 1731 | /** |
1733 | { | 1732 | * gpiod_direction_output - set the GPIO direction to input |
1734 | return gpiod_direction_input(gpio_to_desc(gpio)); | 1733 | * @desc: GPIO to set to output |
1735 | } | 1734 | * @value: initial output value of the GPIO |
1736 | EXPORT_SYMBOL_GPL(gpio_direction_input); | 1735 | * |
1737 | 1736 | * Set the direction of the passed GPIO to output, such as gpiod_set_value() can | |
1738 | static int gpiod_direction_output(struct gpio_desc *desc, int value) | 1737 | * be called safely on it. The initial value of the output must be specified. |
1738 | * | ||
1739 | * Return 0 in case of success, else an error code. | ||
1740 | */ | ||
1741 | int gpiod_direction_output(struct gpio_desc *desc, int value) | ||
1739 | { | 1742 | { |
1740 | unsigned long flags; | 1743 | unsigned long flags; |
1741 | struct gpio_chip *chip; | 1744 | struct gpio_chip *chip; |
@@ -1747,6 +1750,14 @@ static int gpiod_direction_output(struct gpio_desc *desc, int value) | |||
1747 | return -EINVAL; | 1750 | return -EINVAL; |
1748 | } | 1751 | } |
1749 | 1752 | ||
1753 | /* GPIOs used for IRQs shall not be set as output */ | ||
1754 | if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) { | ||
1755 | gpiod_err(desc, | ||
1756 | "%s: tried to set a GPIO tied to an IRQ as output\n", | ||
1757 | __func__); | ||
1758 | return -EIO; | ||
1759 | } | ||
1760 | |||
1750 | /* Open drain pin should not be driven to 1 */ | 1761 | /* Open drain pin should not be driven to 1 */ |
1751 | if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags)) | 1762 | if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
1752 | return gpiod_direction_input(desc); | 1763 | return gpiod_direction_input(desc); |
@@ -1757,8 +1768,9 @@ static int gpiod_direction_output(struct gpio_desc *desc, int value) | |||
1757 | 1768 | ||
1758 | chip = desc->chip; | 1769 | chip = desc->chip; |
1759 | if (!chip->set || !chip->direction_output) { | 1770 | if (!chip->set || !chip->direction_output) { |
1760 | pr_warn("%s: missing set() or direction_output() operations\n", | 1771 | gpiod_warn(desc, |
1761 | __func__); | 1772 | "%s: missing set() or direction_output() operations\n", |
1773 | __func__); | ||
1762 | return -EIO; | 1774 | return -EIO; |
1763 | } | 1775 | } |
1764 | 1776 | ||
@@ -1778,8 +1790,7 @@ static int gpiod_direction_output(struct gpio_desc *desc, int value) | |||
1778 | if (status) { | 1790 | if (status) { |
1779 | status = chip->request(chip, offset); | 1791 | status = chip->request(chip, offset); |
1780 | if (status < 0) { | 1792 | if (status < 0) { |
1781 | pr_debug("GPIO-%d: chip request fail, %d\n", | 1793 | gpiod_dbg(desc, "chip request fail, %d\n", status); |
1782 | desc_to_gpio(desc), status); | ||
1783 | /* and it's not available to anyone else ... | 1794 | /* and it's not available to anyone else ... |
1784 | * gpio_request() is the fully clean solution. | 1795 | * gpio_request() is the fully clean solution. |
1785 | */ | 1796 | */ |
@@ -1797,26 +1808,20 @@ lose: | |||
1797 | fail: | 1808 | fail: |
1798 | spin_unlock_irqrestore(&gpio_lock, flags); | 1809 | spin_unlock_irqrestore(&gpio_lock, flags); |
1799 | if (status) | 1810 | if (status) |
1800 | pr_debug("%s: gpio-%d status %d\n", __func__, | 1811 | gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status); |
1801 | desc_to_gpio(desc), status); | ||
1802 | return status; | 1812 | return status; |
1803 | } | 1813 | } |
1804 | 1814 | EXPORT_SYMBOL_GPL(gpiod_direction_output); | |
1805 | int gpio_direction_output(unsigned gpio, int value) | ||
1806 | { | ||
1807 | return gpiod_direction_output(gpio_to_desc(gpio), value); | ||
1808 | } | ||
1809 | EXPORT_SYMBOL_GPL(gpio_direction_output); | ||
1810 | 1815 | ||
1811 | /** | 1816 | /** |
1812 | * gpio_set_debounce - sets @debounce time for a @gpio | 1817 | * gpiod_set_debounce - sets @debounce time for a @gpio |
1813 | * @gpio: the gpio to set debounce time | 1818 | * @gpio: the gpio to set debounce time |
1814 | * @debounce: debounce time is microseconds | 1819 | * @debounce: debounce time is microseconds |
1815 | * | 1820 | * |
1816 | * returns -ENOTSUPP if the controller does not support setting | 1821 | * returns -ENOTSUPP if the controller does not support setting |
1817 | * debounce. | 1822 | * debounce. |
1818 | */ | 1823 | */ |
1819 | static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) | 1824 | int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) |
1820 | { | 1825 | { |
1821 | unsigned long flags; | 1826 | unsigned long flags; |
1822 | struct gpio_chip *chip; | 1827 | struct gpio_chip *chip; |
@@ -1830,8 +1835,9 @@ static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) | |||
1830 | 1835 | ||
1831 | chip = desc->chip; | 1836 | chip = desc->chip; |
1832 | if (!chip->set || !chip->set_debounce) { | 1837 | if (!chip->set || !chip->set_debounce) { |
1833 | pr_debug("%s: missing set() or set_debounce() operations\n", | 1838 | gpiod_dbg(desc, |
1834 | __func__); | 1839 | "%s: missing set() or set_debounce() operations\n", |
1840 | __func__); | ||
1835 | return -ENOTSUPP; | 1841 | return -ENOTSUPP; |
1836 | } | 1842 | } |
1837 | 1843 | ||
@@ -1853,17 +1859,23 @@ static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) | |||
1853 | fail: | 1859 | fail: |
1854 | spin_unlock_irqrestore(&gpio_lock, flags); | 1860 | spin_unlock_irqrestore(&gpio_lock, flags); |
1855 | if (status) | 1861 | if (status) |
1856 | pr_debug("%s: gpio-%d status %d\n", __func__, | 1862 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
1857 | desc_to_gpio(desc), status); | ||
1858 | 1863 | ||
1859 | return status; | 1864 | return status; |
1860 | } | 1865 | } |
1866 | EXPORT_SYMBOL_GPL(gpiod_set_debounce); | ||
1861 | 1867 | ||
1862 | int gpio_set_debounce(unsigned gpio, unsigned debounce) | 1868 | /** |
1869 | * gpiod_is_active_low - test whether a GPIO is active-low or not | ||
1870 | * @desc: the gpio descriptor to test | ||
1871 | * | ||
1872 | * Returns 1 if the GPIO is active-low, 0 otherwise. | ||
1873 | */ | ||
1874 | int gpiod_is_active_low(const struct gpio_desc *desc) | ||
1863 | { | 1875 | { |
1864 | return gpiod_set_debounce(gpio_to_desc(gpio), debounce); | 1876 | return test_bit(FLAG_ACTIVE_LOW, &desc->flags); |
1865 | } | 1877 | } |
1866 | EXPORT_SYMBOL_GPL(gpio_set_debounce); | 1878 | EXPORT_SYMBOL_GPL(gpiod_is_active_low); |
1867 | 1879 | ||
1868 | /* I/O calls are only valid after configuration completed; the relevant | 1880 | /* I/O calls are only valid after configuration completed; the relevant |
1869 | * "is this a valid GPIO" error checks should already have been done. | 1881 | * "is this a valid GPIO" error checks should already have been done. |
@@ -1887,42 +1899,68 @@ EXPORT_SYMBOL_GPL(gpio_set_debounce); | |||
1887 | * that the GPIO was actually requested. | 1899 | * that the GPIO was actually requested. |
1888 | */ | 1900 | */ |
1889 | 1901 | ||
1890 | /** | 1902 | static int _gpiod_get_raw_value(const struct gpio_desc *desc) |
1891 | * __gpio_get_value() - return a gpio's value | ||
1892 | * @gpio: gpio whose value will be returned | ||
1893 | * Context: any | ||
1894 | * | ||
1895 | * This is used directly or indirectly to implement gpio_get_value(). | ||
1896 | * It returns the zero or nonzero value provided by the associated | ||
1897 | * gpio_chip.get() method; or zero if no such method is provided. | ||
1898 | */ | ||
1899 | static int gpiod_get_value(const struct gpio_desc *desc) | ||
1900 | { | 1903 | { |
1901 | struct gpio_chip *chip; | 1904 | struct gpio_chip *chip; |
1902 | int value; | 1905 | int value; |
1903 | int offset; | 1906 | int offset; |
1904 | 1907 | ||
1905 | if (!desc) | ||
1906 | return 0; | ||
1907 | chip = desc->chip; | 1908 | chip = desc->chip; |
1908 | offset = gpio_chip_hwgpio(desc); | 1909 | offset = gpio_chip_hwgpio(desc); |
1909 | /* Should be using gpio_get_value_cansleep() */ | ||
1910 | WARN_ON(chip->can_sleep); | ||
1911 | value = chip->get ? chip->get(chip, offset) : 0; | 1910 | value = chip->get ? chip->get(chip, offset) : 0; |
1912 | trace_gpio_value(desc_to_gpio(desc), 1, value); | 1911 | trace_gpio_value(desc_to_gpio(desc), 1, value); |
1913 | return value; | 1912 | return value; |
1914 | } | 1913 | } |
1915 | 1914 | ||
1916 | int __gpio_get_value(unsigned gpio) | 1915 | /** |
1916 | * gpiod_get_raw_value() - return a gpio's raw value | ||
1917 | * @desc: gpio whose value will be returned | ||
1918 | * | ||
1919 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding | ||
1920 | * its ACTIVE_LOW status. | ||
1921 | * | ||
1922 | * This function should be called from contexts where we cannot sleep, and will | ||
1923 | * complain if the GPIO chip functions potentially sleep. | ||
1924 | */ | ||
1925 | int gpiod_get_raw_value(const struct gpio_desc *desc) | ||
1926 | { | ||
1927 | if (!desc) | ||
1928 | return 0; | ||
1929 | /* Should be using gpio_get_value_cansleep() */ | ||
1930 | WARN_ON(desc->chip->can_sleep); | ||
1931 | return _gpiod_get_raw_value(desc); | ||
1932 | } | ||
1933 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value); | ||
1934 | |||
1935 | /** | ||
1936 | * gpiod_get_value() - return a gpio's value | ||
1937 | * @desc: gpio whose value will be returned | ||
1938 | * | ||
1939 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into | ||
1940 | * account. | ||
1941 | * | ||
1942 | * This function should be called from contexts where we cannot sleep, and will | ||
1943 | * complain if the GPIO chip functions potentially sleep. | ||
1944 | */ | ||
1945 | int gpiod_get_value(const struct gpio_desc *desc) | ||
1917 | { | 1946 | { |
1918 | return gpiod_get_value(gpio_to_desc(gpio)); | 1947 | int value; |
1948 | if (!desc) | ||
1949 | return 0; | ||
1950 | /* Should be using gpio_get_value_cansleep() */ | ||
1951 | WARN_ON(desc->chip->can_sleep); | ||
1952 | |||
1953 | value = _gpiod_get_raw_value(desc); | ||
1954 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | ||
1955 | value = !value; | ||
1956 | |||
1957 | return value; | ||
1919 | } | 1958 | } |
1920 | EXPORT_SYMBOL_GPL(__gpio_get_value); | 1959 | EXPORT_SYMBOL_GPL(gpiod_get_value); |
1921 | 1960 | ||
1922 | /* | 1961 | /* |
1923 | * _gpio_set_open_drain_value() - Set the open drain gpio's value. | 1962 | * _gpio_set_open_drain_value() - Set the open drain gpio's value. |
1924 | * @gpio: Gpio whose state need to be set. | 1963 | * @desc: gpio descriptor whose state need to be set. |
1925 | * @chip: Gpio chip. | ||
1926 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. | 1964 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. |
1927 | */ | 1965 | */ |
1928 | static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value) | 1966 | static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value) |
@@ -1942,14 +1980,14 @@ static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value) | |||
1942 | } | 1980 | } |
1943 | trace_gpio_direction(desc_to_gpio(desc), value, err); | 1981 | trace_gpio_direction(desc_to_gpio(desc), value, err); |
1944 | if (err < 0) | 1982 | if (err < 0) |
1945 | pr_err("%s: Error in set_value for open drain gpio%d err %d\n", | 1983 | gpiod_err(desc, |
1946 | __func__, desc_to_gpio(desc), err); | 1984 | "%s: Error in set_value for open drain err %d\n", |
1985 | __func__, err); | ||
1947 | } | 1986 | } |
1948 | 1987 | ||
1949 | /* | 1988 | /* |
1950 | * _gpio_set_open_source() - Set the open source gpio's value. | 1989 | * _gpio_set_open_source_value() - Set the open source gpio's value. |
1951 | * @gpio: Gpio whose state need to be set. | 1990 | * @desc: gpio descriptor whose state need to be set. |
1952 | * @chip: Gpio chip. | ||
1953 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. | 1991 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. |
1954 | */ | 1992 | */ |
1955 | static void _gpio_set_open_source_value(struct gpio_desc *desc, int value) | 1993 | static void _gpio_set_open_source_value(struct gpio_desc *desc, int value) |
@@ -1969,28 +2007,16 @@ static void _gpio_set_open_source_value(struct gpio_desc *desc, int value) | |||
1969 | } | 2007 | } |
1970 | trace_gpio_direction(desc_to_gpio(desc), !value, err); | 2008 | trace_gpio_direction(desc_to_gpio(desc), !value, err); |
1971 | if (err < 0) | 2009 | if (err < 0) |
1972 | pr_err("%s: Error in set_value for open source gpio%d err %d\n", | 2010 | gpiod_err(desc, |
1973 | __func__, desc_to_gpio(desc), err); | 2011 | "%s: Error in set_value for open source err %d\n", |
2012 | __func__, err); | ||
1974 | } | 2013 | } |
1975 | 2014 | ||
1976 | /** | 2015 | static void _gpiod_set_raw_value(struct gpio_desc *desc, int value) |
1977 | * __gpio_set_value() - assign a gpio's value | ||
1978 | * @gpio: gpio whose value will be assigned | ||
1979 | * @value: value to assign | ||
1980 | * Context: any | ||
1981 | * | ||
1982 | * This is used directly or indirectly to implement gpio_set_value(). | ||
1983 | * It invokes the associated gpio_chip.set() method. | ||
1984 | */ | ||
1985 | static void gpiod_set_value(struct gpio_desc *desc, int value) | ||
1986 | { | 2016 | { |
1987 | struct gpio_chip *chip; | 2017 | struct gpio_chip *chip; |
1988 | 2018 | ||
1989 | if (!desc) | ||
1990 | return; | ||
1991 | chip = desc->chip; | 2019 | chip = desc->chip; |
1992 | /* Should be using gpio_set_value_cansleep() */ | ||
1993 | WARN_ON(chip->can_sleep); | ||
1994 | trace_gpio_value(desc_to_gpio(desc), 0, value); | 2020 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
1995 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) | 2021 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
1996 | _gpio_set_open_drain_value(desc, value); | 2022 | _gpio_set_open_drain_value(desc, value); |
@@ -2000,44 +2026,71 @@ static void gpiod_set_value(struct gpio_desc *desc, int value) | |||
2000 | chip->set(chip, gpio_chip_hwgpio(desc), value); | 2026 | chip->set(chip, gpio_chip_hwgpio(desc), value); |
2001 | } | 2027 | } |
2002 | 2028 | ||
2003 | void __gpio_set_value(unsigned gpio, int value) | 2029 | /** |
2030 | * gpiod_set_raw_value() - assign a gpio's raw value | ||
2031 | * @desc: gpio whose value will be assigned | ||
2032 | * @value: value to assign | ||
2033 | * | ||
2034 | * Set the raw value of the GPIO, i.e. the value of its physical line without | ||
2035 | * regard for its ACTIVE_LOW status. | ||
2036 | * | ||
2037 | * This function should be called from contexts where we cannot sleep, and will | ||
2038 | * complain if the GPIO chip functions potentially sleep. | ||
2039 | */ | ||
2040 | void gpiod_set_raw_value(struct gpio_desc *desc, int value) | ||
2004 | { | 2041 | { |
2005 | return gpiod_set_value(gpio_to_desc(gpio), value); | 2042 | if (!desc) |
2043 | return; | ||
2044 | /* Should be using gpio_set_value_cansleep() */ | ||
2045 | WARN_ON(desc->chip->can_sleep); | ||
2046 | _gpiod_set_raw_value(desc, value); | ||
2006 | } | 2047 | } |
2007 | EXPORT_SYMBOL_GPL(__gpio_set_value); | 2048 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value); |
2008 | 2049 | ||
2009 | /** | 2050 | /** |
2010 | * __gpio_cansleep() - report whether gpio value access will sleep | 2051 | * gpiod_set_value() - assign a gpio's value |
2011 | * @gpio: gpio in question | 2052 | * @desc: gpio whose value will be assigned |
2012 | * Context: any | 2053 | * @value: value to assign |
2054 | * | ||
2055 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into | ||
2056 | * account | ||
2013 | * | 2057 | * |
2014 | * This is used directly or indirectly to implement gpio_cansleep(). It | 2058 | * This function should be called from contexts where we cannot sleep, and will |
2015 | * returns nonzero if access reading or writing the GPIO value can sleep. | 2059 | * complain if the GPIO chip functions potentially sleep. |
2016 | */ | 2060 | */ |
2017 | static int gpiod_cansleep(const struct gpio_desc *desc) | 2061 | void gpiod_set_value(struct gpio_desc *desc, int value) |
2018 | { | 2062 | { |
2019 | if (!desc) | 2063 | if (!desc) |
2020 | return 0; | 2064 | return; |
2021 | /* only call this on GPIOs that are valid! */ | 2065 | /* Should be using gpio_set_value_cansleep() */ |
2022 | return desc->chip->can_sleep; | 2066 | WARN_ON(desc->chip->can_sleep); |
2067 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | ||
2068 | value = !value; | ||
2069 | _gpiod_set_raw_value(desc, value); | ||
2023 | } | 2070 | } |
2071 | EXPORT_SYMBOL_GPL(gpiod_set_value); | ||
2024 | 2072 | ||
2025 | int __gpio_cansleep(unsigned gpio) | 2073 | /** |
2074 | * gpiod_cansleep() - report whether gpio value access may sleep | ||
2075 | * @desc: gpio to check | ||
2076 | * | ||
2077 | */ | ||
2078 | int gpiod_cansleep(const struct gpio_desc *desc) | ||
2026 | { | 2079 | { |
2027 | return gpiod_cansleep(gpio_to_desc(gpio)); | 2080 | if (!desc) |
2081 | return 0; | ||
2082 | return desc->chip->can_sleep; | ||
2028 | } | 2083 | } |
2029 | EXPORT_SYMBOL_GPL(__gpio_cansleep); | 2084 | EXPORT_SYMBOL_GPL(gpiod_cansleep); |
2030 | 2085 | ||
2031 | /** | 2086 | /** |
2032 | * __gpio_to_irq() - return the IRQ corresponding to a GPIO | 2087 | * gpiod_to_irq() - return the IRQ corresponding to a GPIO |
2033 | * @gpio: gpio whose IRQ will be returned (already requested) | 2088 | * @desc: gpio whose IRQ will be returned (already requested) |
2034 | * Context: any | ||
2035 | * | 2089 | * |
2036 | * This is used directly or indirectly to implement gpio_to_irq(). | 2090 | * Return the IRQ corresponding to the passed GPIO, or an error code in case of |
2037 | * It returns the number of the IRQ signaled by this (input) GPIO, | 2091 | * error. |
2038 | * or a negative errno. | ||
2039 | */ | 2092 | */ |
2040 | static int gpiod_to_irq(const struct gpio_desc *desc) | 2093 | int gpiod_to_irq(const struct gpio_desc *desc) |
2041 | { | 2094 | { |
2042 | struct gpio_chip *chip; | 2095 | struct gpio_chip *chip; |
2043 | int offset; | 2096 | int offset; |
@@ -2048,62 +2101,366 @@ static int gpiod_to_irq(const struct gpio_desc *desc) | |||
2048 | offset = gpio_chip_hwgpio(desc); | 2101 | offset = gpio_chip_hwgpio(desc); |
2049 | return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO; | 2102 | return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO; |
2050 | } | 2103 | } |
2104 | EXPORT_SYMBOL_GPL(gpiod_to_irq); | ||
2105 | |||
2106 | /** | ||
2107 | * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ | ||
2108 | * @gpio: the GPIO line to lock as used for IRQ | ||
2109 | * | ||
2110 | * This is used directly by GPIO drivers that want to lock down | ||
2111 | * a certain GPIO line to be used as IRQs, for example in the | ||
2112 | * .to_irq() callback of their gpio_chip, or in the .irq_enable() | ||
2113 | * of its irq_chip implementation if the GPIO is known from that | ||
2114 | * code. | ||
2115 | */ | ||
2116 | int gpiod_lock_as_irq(struct gpio_desc *desc) | ||
2117 | { | ||
2118 | if (!desc) | ||
2119 | return -EINVAL; | ||
2120 | |||
2121 | if (test_bit(FLAG_IS_OUT, &desc->flags)) { | ||
2122 | gpiod_err(desc, | ||
2123 | "%s: tried to flag a GPIO set as output for IRQ\n", | ||
2124 | __func__); | ||
2125 | return -EIO; | ||
2126 | } | ||
2127 | |||
2128 | set_bit(FLAG_USED_AS_IRQ, &desc->flags); | ||
2129 | return 0; | ||
2130 | } | ||
2131 | EXPORT_SYMBOL_GPL(gpiod_lock_as_irq); | ||
2132 | |||
2133 | int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset) | ||
2134 | { | ||
2135 | return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip, offset)); | ||
2136 | } | ||
2137 | EXPORT_SYMBOL_GPL(gpio_lock_as_irq); | ||
2051 | 2138 | ||
2052 | int __gpio_to_irq(unsigned gpio) | 2139 | /** |
2140 | * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ | ||
2141 | * @gpio: the GPIO line to unlock from IRQ usage | ||
2142 | * | ||
2143 | * This is used directly by GPIO drivers that want to indicate | ||
2144 | * that a certain GPIO is no longer used exclusively for IRQ. | ||
2145 | */ | ||
2146 | void gpiod_unlock_as_irq(struct gpio_desc *desc) | ||
2053 | { | 2147 | { |
2054 | return gpiod_to_irq(gpio_to_desc(gpio)); | 2148 | if (!desc) |
2149 | return; | ||
2150 | |||
2151 | clear_bit(FLAG_USED_AS_IRQ, &desc->flags); | ||
2055 | } | 2152 | } |
2056 | EXPORT_SYMBOL_GPL(__gpio_to_irq); | 2153 | EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq); |
2057 | 2154 | ||
2155 | void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset) | ||
2156 | { | ||
2157 | return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip, offset)); | ||
2158 | } | ||
2159 | EXPORT_SYMBOL_GPL(gpio_unlock_as_irq); | ||
2058 | 2160 | ||
2059 | /* There's no value in making it easy to inline GPIO calls that may sleep. | 2161 | /** |
2060 | * Common examples include ones connected to I2C or SPI chips. | 2162 | * gpiod_get_raw_value_cansleep() - return a gpio's raw value |
2163 | * @desc: gpio whose value will be returned | ||
2164 | * | ||
2165 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding | ||
2166 | * its ACTIVE_LOW status. | ||
2167 | * | ||
2168 | * This function is to be called from contexts that can sleep. | ||
2061 | */ | 2169 | */ |
2170 | int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) | ||
2171 | { | ||
2172 | might_sleep_if(extra_checks); | ||
2173 | if (!desc) | ||
2174 | return 0; | ||
2175 | return _gpiod_get_raw_value(desc); | ||
2176 | } | ||
2177 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep); | ||
2062 | 2178 | ||
2063 | static int gpiod_get_value_cansleep(const struct gpio_desc *desc) | 2179 | /** |
2180 | * gpiod_get_value_cansleep() - return a gpio's value | ||
2181 | * @desc: gpio whose value will be returned | ||
2182 | * | ||
2183 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into | ||
2184 | * account. | ||
2185 | * | ||
2186 | * This function is to be called from contexts that can sleep. | ||
2187 | */ | ||
2188 | int gpiod_get_value_cansleep(const struct gpio_desc *desc) | ||
2064 | { | 2189 | { |
2065 | struct gpio_chip *chip; | ||
2066 | int value; | 2190 | int value; |
2067 | int offset; | ||
2068 | 2191 | ||
2069 | might_sleep_if(extra_checks); | 2192 | might_sleep_if(extra_checks); |
2070 | if (!desc) | 2193 | if (!desc) |
2071 | return 0; | 2194 | return 0; |
2072 | chip = desc->chip; | 2195 | |
2073 | offset = gpio_chip_hwgpio(desc); | 2196 | value = _gpiod_get_raw_value(desc); |
2074 | value = chip->get ? chip->get(chip, offset) : 0; | 2197 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
2075 | trace_gpio_value(desc_to_gpio(desc), 1, value); | 2198 | value = !value; |
2199 | |||
2076 | return value; | 2200 | return value; |
2077 | } | 2201 | } |
2202 | EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep); | ||
2078 | 2203 | ||
2079 | int gpio_get_value_cansleep(unsigned gpio) | 2204 | /** |
2205 | * gpiod_set_raw_value_cansleep() - assign a gpio's raw value | ||
2206 | * @desc: gpio whose value will be assigned | ||
2207 | * @value: value to assign | ||
2208 | * | ||
2209 | * Set the raw value of the GPIO, i.e. the value of its physical line without | ||
2210 | * regard for its ACTIVE_LOW status. | ||
2211 | * | ||
2212 | * This function is to be called from contexts that can sleep. | ||
2213 | */ | ||
2214 | void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value) | ||
2080 | { | 2215 | { |
2081 | return gpiod_get_value_cansleep(gpio_to_desc(gpio)); | 2216 | might_sleep_if(extra_checks); |
2217 | if (!desc) | ||
2218 | return; | ||
2219 | _gpiod_set_raw_value(desc, value); | ||
2082 | } | 2220 | } |
2083 | EXPORT_SYMBOL_GPL(gpio_get_value_cansleep); | 2221 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep); |
2084 | 2222 | ||
2085 | static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) | 2223 | /** |
2224 | * gpiod_set_value_cansleep() - assign a gpio's value | ||
2225 | * @desc: gpio whose value will be assigned | ||
2226 | * @value: value to assign | ||
2227 | * | ||
2228 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into | ||
2229 | * account | ||
2230 | * | ||
2231 | * This function is to be called from contexts that can sleep. | ||
2232 | */ | ||
2233 | void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) | ||
2086 | { | 2234 | { |
2087 | struct gpio_chip *chip; | ||
2088 | |||
2089 | might_sleep_if(extra_checks); | 2235 | might_sleep_if(extra_checks); |
2090 | if (!desc) | 2236 | if (!desc) |
2091 | return; | 2237 | return; |
2092 | chip = desc->chip; | 2238 | |
2093 | trace_gpio_value(desc_to_gpio(desc), 0, value); | 2239 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
2094 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) | 2240 | value = !value; |
2095 | _gpio_set_open_drain_value(desc, value); | 2241 | _gpiod_set_raw_value(desc, value); |
2096 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) | 2242 | } |
2097 | _gpio_set_open_source_value(desc, value); | 2243 | EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep); |
2244 | |||
2245 | /** | ||
2246 | * gpiod_add_table() - register GPIO device consumers | ||
2247 | * @table: array of consumers to register | ||
2248 | * @num: number of consumers in table | ||
2249 | */ | ||
2250 | void gpiod_add_table(struct gpiod_lookup *table, size_t size) | ||
2251 | { | ||
2252 | mutex_lock(&gpio_lookup_lock); | ||
2253 | |||
2254 | while (size--) { | ||
2255 | list_add_tail(&table->list, &gpio_lookup_list); | ||
2256 | table++; | ||
2257 | } | ||
2258 | |||
2259 | mutex_unlock(&gpio_lookup_lock); | ||
2260 | } | ||
2261 | |||
2262 | /* | ||
2263 | * Caller must have a acquired gpio_lookup_lock | ||
2264 | */ | ||
2265 | static struct gpio_chip *find_chip_by_name(const char *name) | ||
2266 | { | ||
2267 | struct gpio_chip *chip = NULL; | ||
2268 | |||
2269 | list_for_each_entry(chip, &gpio_lookup_list, list) { | ||
2270 | if (chip->label == NULL) | ||
2271 | continue; | ||
2272 | if (!strcmp(chip->label, name)) | ||
2273 | break; | ||
2274 | } | ||
2275 | |||
2276 | return chip; | ||
2277 | } | ||
2278 | |||
2279 | #ifdef CONFIG_OF | ||
2280 | static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, | ||
2281 | unsigned int idx, unsigned long *flags) | ||
2282 | { | ||
2283 | char prop_name[32]; /* 32 is max size of property name */ | ||
2284 | enum of_gpio_flags of_flags; | ||
2285 | struct gpio_desc *desc; | ||
2286 | |||
2287 | if (con_id) | ||
2288 | snprintf(prop_name, 32, "%s-gpios", con_id); | ||
2098 | else | 2289 | else |
2099 | chip->set(chip, gpio_chip_hwgpio(desc), value); | 2290 | snprintf(prop_name, 32, "gpios"); |
2291 | |||
2292 | desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, | ||
2293 | &of_flags); | ||
2294 | |||
2295 | if (IS_ERR(desc)) | ||
2296 | return desc; | ||
2297 | |||
2298 | if (of_flags & OF_GPIO_ACTIVE_LOW) | ||
2299 | *flags |= GPIOF_ACTIVE_LOW; | ||
2300 | |||
2301 | return desc; | ||
2302 | } | ||
2303 | #else | ||
2304 | static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, | ||
2305 | unsigned int idx, unsigned long *flags) | ||
2306 | { | ||
2307 | return ERR_PTR(-ENODEV); | ||
2308 | } | ||
2309 | #endif | ||
2310 | |||
2311 | static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, | ||
2312 | unsigned int idx, unsigned long *flags) | ||
2313 | { | ||
2314 | struct acpi_gpio_info info; | ||
2315 | struct gpio_desc *desc; | ||
2316 | |||
2317 | desc = acpi_get_gpiod_by_index(dev, idx, &info); | ||
2318 | if (IS_ERR(desc)) | ||
2319 | return desc; | ||
2320 | |||
2321 | if (info.gpioint && info.active_low) | ||
2322 | *flags |= GPIOF_ACTIVE_LOW; | ||
2323 | |||
2324 | return desc; | ||
2325 | } | ||
2326 | |||
2327 | static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id, | ||
2328 | unsigned int idx, unsigned long *flags) | ||
2329 | { | ||
2330 | const char *dev_id = dev ? dev_name(dev) : NULL; | ||
2331 | struct gpio_desc *desc = ERR_PTR(-ENODEV); | ||
2332 | unsigned int match, best = 0; | ||
2333 | struct gpiod_lookup *p; | ||
2334 | |||
2335 | mutex_lock(&gpio_lookup_lock); | ||
2336 | |||
2337 | list_for_each_entry(p, &gpio_lookup_list, list) { | ||
2338 | match = 0; | ||
2339 | |||
2340 | if (p->dev_id) { | ||
2341 | if (!dev_id || strcmp(p->dev_id, dev_id)) | ||
2342 | continue; | ||
2343 | |||
2344 | match += 2; | ||
2345 | } | ||
2346 | |||
2347 | if (p->con_id) { | ||
2348 | if (!con_id || strcmp(p->con_id, con_id)) | ||
2349 | continue; | ||
2350 | |||
2351 | match += 1; | ||
2352 | } | ||
2353 | |||
2354 | if (p->idx != idx) | ||
2355 | continue; | ||
2356 | |||
2357 | if (match > best) { | ||
2358 | struct gpio_chip *chip; | ||
2359 | |||
2360 | chip = find_chip_by_name(p->chip_label); | ||
2361 | |||
2362 | if (!chip) { | ||
2363 | dev_warn(dev, "cannot find GPIO chip %s\n", | ||
2364 | p->chip_label); | ||
2365 | continue; | ||
2366 | } | ||
2367 | |||
2368 | if (chip->ngpio >= p->chip_hwnum) { | ||
2369 | dev_warn(dev, "GPIO chip %s has %d GPIOs\n", | ||
2370 | chip->label, chip->ngpio); | ||
2371 | continue; | ||
2372 | } | ||
2373 | |||
2374 | desc = gpio_to_desc(chip->base + p->chip_hwnum); | ||
2375 | *flags = p->flags; | ||
2376 | |||
2377 | if (match != 3) | ||
2378 | best = match; | ||
2379 | else | ||
2380 | break; | ||
2381 | } | ||
2382 | } | ||
2383 | |||
2384 | mutex_unlock(&gpio_lookup_lock); | ||
2385 | |||
2386 | return desc; | ||
2387 | } | ||
2388 | |||
2389 | /** | ||
2390 | * gpio_get - obtain a GPIO for a given GPIO function | ||
2391 | * @dev: GPIO consumer | ||
2392 | * @con_id: function within the GPIO consumer | ||
2393 | * | ||
2394 | * Return the GPIO descriptor corresponding to the function con_id of device | ||
2395 | * dev, or an IS_ERR() condition if an error occured. | ||
2396 | */ | ||
2397 | struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id) | ||
2398 | { | ||
2399 | return gpiod_get_index(dev, con_id, 0); | ||
2400 | } | ||
2401 | EXPORT_SYMBOL_GPL(gpiod_get); | ||
2402 | |||
2403 | /** | ||
2404 | * gpiod_get_index - obtain a GPIO from a multi-index GPIO function | ||
2405 | * @dev: GPIO consumer | ||
2406 | * @con_id: function within the GPIO consumer | ||
2407 | * @idx: index of the GPIO to obtain in the consumer | ||
2408 | * | ||
2409 | * This variant of gpiod_get() allows to access GPIOs other than the first | ||
2410 | * defined one for functions that define several GPIOs. | ||
2411 | * | ||
2412 | * Return a valid GPIO descriptor, or an IS_ERR() condition in case of error. | ||
2413 | */ | ||
2414 | struct gpio_desc *__must_check gpiod_get_index(struct device *dev, | ||
2415 | const char *con_id, | ||
2416 | unsigned int idx) | ||
2417 | { | ||
2418 | struct gpio_desc *desc; | ||
2419 | int status; | ||
2420 | unsigned long flags = 0; | ||
2421 | |||
2422 | dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id); | ||
2423 | |||
2424 | /* Using device tree? */ | ||
2425 | if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) { | ||
2426 | dev_dbg(dev, "using device tree for GPIO lookup\n"); | ||
2427 | desc = of_find_gpio(dev, con_id, idx, &flags); | ||
2428 | } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) { | ||
2429 | dev_dbg(dev, "using ACPI for GPIO lookup\n"); | ||
2430 | desc = acpi_find_gpio(dev, con_id, idx, &flags); | ||
2431 | } else { | ||
2432 | dev_dbg(dev, "using lookup tables for GPIO lookup"); | ||
2433 | desc = gpiod_find(dev, con_id, idx, &flags); | ||
2434 | } | ||
2435 | |||
2436 | if (IS_ERR(desc)) { | ||
2437 | dev_warn(dev, "lookup for GPIO %s failed\n", con_id); | ||
2438 | return desc; | ||
2439 | } | ||
2440 | |||
2441 | status = gpiod_request(desc, con_id); | ||
2442 | |||
2443 | if (status < 0) | ||
2444 | return ERR_PTR(status); | ||
2445 | |||
2446 | if (flags & GPIOF_ACTIVE_LOW) | ||
2447 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); | ||
2448 | |||
2449 | return desc; | ||
2100 | } | 2450 | } |
2451 | EXPORT_SYMBOL_GPL(gpiod_get_index); | ||
2101 | 2452 | ||
2102 | void gpio_set_value_cansleep(unsigned gpio, int value) | 2453 | /** |
2454 | * gpiod_put - dispose of a GPIO descriptor | ||
2455 | * @desc: GPIO descriptor to dispose of | ||
2456 | * | ||
2457 | * No descriptor can be used after gpiod_put() has been called on it. | ||
2458 | */ | ||
2459 | void gpiod_put(struct gpio_desc *desc) | ||
2103 | { | 2460 | { |
2104 | return gpiod_set_value_cansleep(gpio_to_desc(gpio), value); | 2461 | gpiod_free(desc); |
2105 | } | 2462 | } |
2106 | EXPORT_SYMBOL_GPL(gpio_set_value_cansleep); | 2463 | EXPORT_SYMBOL_GPL(gpiod_put); |
2107 | 2464 | ||
2108 | #ifdef CONFIG_DEBUG_FS | 2465 | #ifdef CONFIG_DEBUG_FS |
2109 | 2466 | ||
@@ -2113,6 +2470,7 @@ static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip) | |||
2113 | unsigned gpio = chip->base; | 2470 | unsigned gpio = chip->base; |
2114 | struct gpio_desc *gdesc = &chip->desc[0]; | 2471 | struct gpio_desc *gdesc = &chip->desc[0]; |
2115 | int is_out; | 2472 | int is_out; |
2473 | int is_irq; | ||
2116 | 2474 | ||
2117 | for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) { | 2475 | for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) { |
2118 | if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) | 2476 | if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) |
@@ -2120,12 +2478,14 @@ static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip) | |||
2120 | 2478 | ||
2121 | gpiod_get_direction(gdesc); | 2479 | gpiod_get_direction(gdesc); |
2122 | is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); | 2480 | is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); |
2123 | seq_printf(s, " gpio-%-3d (%-20.20s) %s %s", | 2481 | is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags); |
2482 | seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s", | ||
2124 | gpio, gdesc->label, | 2483 | gpio, gdesc->label, |
2125 | is_out ? "out" : "in ", | 2484 | is_out ? "out" : "in ", |
2126 | chip->get | 2485 | chip->get |
2127 | ? (chip->get(chip, i) ? "hi" : "lo") | 2486 | ? (chip->get(chip, i) ? "hi" : "lo") |
2128 | : "? "); | 2487 | : "? ", |
2488 | is_irq ? "IRQ" : " "); | ||
2129 | seq_printf(s, "\n"); | 2489 | seq_printf(s, "\n"); |
2130 | } | 2490 | } |
2131 | } | 2491 | } |