David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1 | #include <linux/kernel.h> |
| 2 | #include <linux/module.h> |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 3 | #include <linux/interrupt.h> |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 4 | #include <linux/irq.h> |
| 5 | #include <linux/spinlock.h> |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 6 | #include <linux/list.h> |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 7 | #include <linux/device.h> |
| 8 | #include <linux/err.h> |
| 9 | #include <linux/debugfs.h> |
| 10 | #include <linux/seq_file.h> |
| 11 | #include <linux/gpio.h> |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 12 | #include <linux/of_gpio.h> |
Mika Westerberg | 81f59e9 | 2013-10-10 11:01:09 +0300 | [diff] [blame] | 13 | #include <linux/acpi_gpio.h> |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 14 | #include <linux/idr.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Rafael J. Wysocki | 7b19981 | 2013-11-11 22:41:56 +0100 | [diff] [blame^] | 16 | #include <linux/acpi.h> |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 17 | |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 18 | #define CREATE_TRACE_POINTS |
| 19 | #include <trace/events/gpio.h> |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 20 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 21 | /* Implementation infrastructure for GPIO interfaces. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 22 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 23 | * The GPIO programming interface allows for inlining speed-critical |
| 24 | * get/set operations for common cases, so that access to SOC-integrated |
| 25 | * GPIOs can sometimes cost only an instruction or two per bit. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | |
| 29 | /* When debugging, extend minimal trust to callers and platform code. |
| 30 | * Also emit diagnostic messages that may help initial bringup, when |
| 31 | * board setup or driver bugs are most common. |
| 32 | * |
| 33 | * Otherwise, minimize overhead in what may be bitbanging codepaths. |
| 34 | */ |
| 35 | #ifdef DEBUG |
| 36 | #define extra_checks 1 |
| 37 | #else |
| 38 | #define extra_checks 0 |
| 39 | #endif |
| 40 | |
| 41 | /* gpio_lock prevents conflicts during gpio_desc[] table updates. |
| 42 | * While any GPIO is requested, its gpio_chip is not removable; |
| 43 | * each GPIO's "requested" flag serves as a lock and refcount. |
| 44 | */ |
| 45 | static DEFINE_SPINLOCK(gpio_lock); |
| 46 | |
| 47 | struct gpio_desc { |
| 48 | struct gpio_chip *chip; |
| 49 | unsigned long flags; |
| 50 | /* flag symbols are bit numbers */ |
| 51 | #define FLAG_REQUESTED 0 |
| 52 | #define FLAG_IS_OUT 1 |
Alexandre Courbot | 710b40e | 2013-02-02 23:44:06 +0900 | [diff] [blame] | 53 | #define FLAG_EXPORT 2 /* protected by sysfs_lock */ |
| 54 | #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */ |
| 55 | #define FLAG_TRIG_FALL 4 /* trigger on falling edge */ |
| 56 | #define FLAG_TRIG_RISE 5 /* trigger on rising edge */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 57 | #define FLAG_ACTIVE_LOW 6 /* value has active low */ |
Alexandre Courbot | 710b40e | 2013-02-02 23:44:06 +0900 | [diff] [blame] | 58 | #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ |
| 59 | #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 60 | #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 61 | |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 62 | #define ID_SHIFT 16 /* add new flags before this one */ |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 63 | |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 64 | #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1) |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 65 | #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE)) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 66 | |
| 67 | #ifdef CONFIG_DEBUG_FS |
| 68 | const char *label; |
| 69 | #endif |
| 70 | }; |
| 71 | static struct gpio_desc gpio_desc[ARCH_NR_GPIOS]; |
| 72 | |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 73 | #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio) |
| 74 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 75 | static DEFINE_MUTEX(gpio_lookup_lock); |
| 76 | static LIST_HEAD(gpio_lookup_list); |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 77 | static LIST_HEAD(gpio_chips); |
| 78 | |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 79 | #ifdef CONFIG_GPIO_SYSFS |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 80 | static DEFINE_IDR(dirent_idr); |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 81 | #endif |
| 82 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 83 | static int gpiod_request(struct gpio_desc *desc, const char *label); |
| 84 | static void gpiod_free(struct gpio_desc *desc); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 85 | |
Mark Brown | 7b17b59 | 2013-09-09 10:33:50 +0100 | [diff] [blame] | 86 | #ifdef CONFIG_DEBUG_FS |
| 87 | #define gpiod_emerg(desc, fmt, ...) \ |
| 88 | pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ |
| 89 | ##__VA_ARGS__) |
| 90 | #define gpiod_crit(desc, fmt, ...) \ |
| 91 | pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ |
| 92 | ##__VA_ARGS__) |
| 93 | #define gpiod_err(desc, fmt, ...) \ |
| 94 | pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ |
| 95 | ##__VA_ARGS__) |
| 96 | #define gpiod_warn(desc, fmt, ...) \ |
| 97 | pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ |
| 98 | ##__VA_ARGS__) |
| 99 | #define gpiod_info(desc, fmt, ...) \ |
| 100 | pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ |
| 101 | ##__VA_ARGS__) |
| 102 | #define gpiod_dbg(desc, fmt, ...) \ |
| 103 | pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ |
| 104 | ##__VA_ARGS__) |
| 105 | #else |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 106 | #define gpiod_emerg(desc, fmt, ...) \ |
| 107 | pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) |
| 108 | #define gpiod_crit(desc, fmt, ...) \ |
| 109 | pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) |
| 110 | #define gpiod_err(desc, fmt, ...) \ |
| 111 | pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) |
| 112 | #define gpiod_warn(desc, fmt, ...) \ |
| 113 | pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) |
| 114 | #define gpiod_info(desc, fmt, ...) \ |
| 115 | pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) |
| 116 | #define gpiod_dbg(desc, fmt, ...) \ |
| 117 | pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) |
Mark Brown | 7b17b59 | 2013-09-09 10:33:50 +0100 | [diff] [blame] | 118 | #endif |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 119 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 120 | static inline void desc_set_label(struct gpio_desc *d, const char *label) |
| 121 | { |
| 122 | #ifdef CONFIG_DEBUG_FS |
| 123 | d->label = label; |
| 124 | #endif |
| 125 | } |
| 126 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 127 | /* |
| 128 | * Return the GPIO number of the passed descriptor relative to its chip |
| 129 | */ |
| 130 | static int gpio_chip_hwgpio(const struct gpio_desc *desc) |
| 131 | { |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 132 | return desc - &desc->chip->desc[0]; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Convert a GPIO number to its descriptor |
| 137 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 138 | struct gpio_desc *gpio_to_desc(unsigned gpio) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 139 | { |
| 140 | if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio)) |
| 141 | return NULL; |
| 142 | else |
| 143 | return &gpio_desc[gpio]; |
| 144 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 145 | EXPORT_SYMBOL_GPL(gpio_to_desc); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 146 | |
| 147 | /** |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 148 | * Convert an offset on a certain chip to a corresponding descriptor |
| 149 | */ |
| 150 | static struct gpio_desc *gpiochip_offset_to_desc(struct gpio_chip *chip, |
| 151 | unsigned int offset) |
| 152 | { |
| 153 | unsigned int gpio = chip->base + offset; |
| 154 | |
| 155 | return gpio_to_desc(gpio); |
| 156 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 157 | |
| 158 | /** |
| 159 | * Convert a GPIO descriptor to the integer namespace. |
| 160 | * This should disappear in the future but is needed since we still |
| 161 | * use GPIO numbers for error messages and sysfs nodes |
| 162 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 163 | int desc_to_gpio(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 164 | { |
Alexandre Courbot | 8c0fca8 | 2013-10-04 10:59:57 -0700 | [diff] [blame] | 165 | return desc - &gpio_desc[0]; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 166 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 167 | EXPORT_SYMBOL_GPL(desc_to_gpio); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 168 | |
| 169 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 170 | /* Warn when drivers omit gpio_request() calls -- legal but ill-advised |
| 171 | * when setting direction, and otherwise illegal. Until board setup code |
| 172 | * and drivers use explicit requests everywhere (which won't happen when |
| 173 | * those calls have no teeth) we can't avoid autorequesting. This nag |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 174 | * message should motivate switching to explicit requests... so should |
| 175 | * the weaker cleanup after faults, compared to gpio_request(). |
David Brownell | 8a0cecf | 2009-04-02 16:57:06 -0700 | [diff] [blame] | 176 | * |
| 177 | * NOTE: the autorequest mechanism is going away; at this point it's |
| 178 | * only "legal" in the sense that (old) code using it won't break yet, |
| 179 | * but instead only triggers a WARN() stack dump. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 180 | */ |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 181 | static int gpio_ensure_requested(struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 182 | { |
David Brownell | 8a0cecf | 2009-04-02 16:57:06 -0700 | [diff] [blame] | 183 | const struct gpio_chip *chip = desc->chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 184 | const int gpio = desc_to_gpio(desc); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 185 | |
David Brownell | 8a0cecf | 2009-04-02 16:57:06 -0700 | [diff] [blame] | 186 | if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0, |
| 187 | "autorequest GPIO-%d\n", gpio)) { |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 188 | if (!try_module_get(chip->owner)) { |
| 189 | pr_err("GPIO-%d: module can't be gotten \n", gpio); |
| 190 | clear_bit(FLAG_REQUESTED, &desc->flags); |
| 191 | /* lose */ |
| 192 | return -EIO; |
| 193 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 194 | desc_set_label(desc, "[auto]"); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 195 | /* caller must chip->request() w/o spinlock */ |
| 196 | if (chip->request) |
| 197 | return 1; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 198 | } |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 199 | return 0; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 202 | /** |
| 203 | * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs |
| 204 | * @desc: descriptor to return the chip of |
| 205 | */ |
| 206 | struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 207 | { |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 208 | return desc ? desc->chip : NULL; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 209 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 210 | EXPORT_SYMBOL_GPL(gpiod_to_chip); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 211 | |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 212 | /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ |
| 213 | static int gpiochip_find_base(int ngpio) |
| 214 | { |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 215 | struct gpio_chip *chip; |
| 216 | int base = ARCH_NR_GPIOS - ngpio; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 217 | |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 218 | list_for_each_entry_reverse(chip, &gpio_chips, list) { |
| 219 | /* found a free space? */ |
| 220 | if (chip->base + chip->ngpio <= base) |
| 221 | break; |
| 222 | else |
| 223 | /* nope, check the space right before the chip */ |
| 224 | base = chip->base - ngpio; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 227 | if (gpio_is_valid(base)) { |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 228 | pr_debug("%s: found new base at %d\n", __func__, base); |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 229 | return base; |
| 230 | } else { |
| 231 | pr_err("%s: cannot find free range\n", __func__); |
| 232 | return -ENOSPC; |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 233 | } |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 236 | /** |
| 237 | * gpiod_get_direction - return the current direction of a GPIO |
| 238 | * @desc: GPIO to get the direction of |
| 239 | * |
| 240 | * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error. |
| 241 | * |
| 242 | * This function may sleep if gpiod_cansleep() is true. |
| 243 | */ |
| 244 | int gpiod_get_direction(const struct gpio_desc *desc) |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 245 | { |
| 246 | struct gpio_chip *chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 247 | unsigned offset; |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 248 | int status = -EINVAL; |
| 249 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 250 | chip = gpiod_to_chip(desc); |
| 251 | offset = gpio_chip_hwgpio(desc); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 252 | |
| 253 | if (!chip->get_direction) |
| 254 | return status; |
| 255 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 256 | status = chip->get_direction(chip, offset); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 257 | if (status > 0) { |
| 258 | /* GPIOF_DIR_IN, or other positive */ |
| 259 | status = 1; |
Alexandre Courbot | def6343 | 2013-02-15 14:46:15 +0900 | [diff] [blame] | 260 | /* FLAG_IS_OUT is just a cache of the result of get_direction(), |
| 261 | * so it does not affect constness per se */ |
| 262 | clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 263 | } |
| 264 | if (status == 0) { |
| 265 | /* GPIOF_DIR_OUT */ |
Alexandre Courbot | def6343 | 2013-02-15 14:46:15 +0900 | [diff] [blame] | 266 | set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 267 | } |
| 268 | return status; |
| 269 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 270 | EXPORT_SYMBOL_GPL(gpiod_get_direction); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 271 | |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 272 | #ifdef CONFIG_GPIO_SYSFS |
| 273 | |
| 274 | /* lock protects against unexport_gpio() being called while |
| 275 | * sysfs files are active. |
| 276 | */ |
| 277 | static DEFINE_MUTEX(sysfs_lock); |
| 278 | |
| 279 | /* |
| 280 | * /sys/class/gpio/gpioN... only for GPIOs that are exported |
| 281 | * /direction |
| 282 | * * MAY BE OMITTED if kernel won't allow direction changes |
| 283 | * * is read/write as "in" or "out" |
| 284 | * * may also be written as "high" or "low", initializing |
| 285 | * output value as specified ("out" implies "low") |
| 286 | * /value |
| 287 | * * always readable, subject to hardware behavior |
| 288 | * * may be writable, as zero/nonzero |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 289 | * /edge |
| 290 | * * configures behavior of poll(2) on /value |
| 291 | * * available only if pin can generate IRQs on input |
| 292 | * * is read/write as "none", "falling", "rising", or "both" |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 293 | * /active_low |
| 294 | * * configures polarity of /value |
| 295 | * * is read/write as zero/nonzero |
| 296 | * * also affects existing and subsequent "falling" and "rising" |
| 297 | * /edge configuration |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 298 | */ |
| 299 | |
| 300 | static ssize_t gpio_direction_show(struct device *dev, |
| 301 | struct device_attribute *attr, char *buf) |
| 302 | { |
Alexandre Courbot | def6343 | 2013-02-15 14:46:15 +0900 | [diff] [blame] | 303 | const struct gpio_desc *desc = dev_get_drvdata(dev); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 304 | ssize_t status; |
| 305 | |
| 306 | mutex_lock(&sysfs_lock); |
| 307 | |
Alexandre Courbot | 476171ce | 2013-02-04 17:42:04 +0900 | [diff] [blame] | 308 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 309 | status = -EIO; |
Alexandre Courbot | 476171ce | 2013-02-04 17:42:04 +0900 | [diff] [blame] | 310 | } else { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 311 | gpiod_get_direction(desc); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 312 | status = sprintf(buf, "%s\n", |
| 313 | test_bit(FLAG_IS_OUT, &desc->flags) |
| 314 | ? "out" : "in"); |
Alexandre Courbot | 476171ce | 2013-02-04 17:42:04 +0900 | [diff] [blame] | 315 | } |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 316 | |
| 317 | mutex_unlock(&sysfs_lock); |
| 318 | return status; |
| 319 | } |
| 320 | |
| 321 | static ssize_t gpio_direction_store(struct device *dev, |
| 322 | struct device_attribute *attr, const char *buf, size_t size) |
| 323 | { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 324 | struct gpio_desc *desc = dev_get_drvdata(dev); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 325 | ssize_t status; |
| 326 | |
| 327 | mutex_lock(&sysfs_lock); |
| 328 | |
| 329 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 330 | status = -EIO; |
| 331 | else if (sysfs_streq(buf, "high")) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 332 | status = gpiod_direction_output(desc, 1); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 333 | else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low")) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 334 | status = gpiod_direction_output(desc, 0); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 335 | else if (sysfs_streq(buf, "in")) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 336 | status = gpiod_direction_input(desc); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 337 | else |
| 338 | status = -EINVAL; |
| 339 | |
| 340 | mutex_unlock(&sysfs_lock); |
| 341 | return status ? : size; |
| 342 | } |
| 343 | |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 344 | static /* const */ DEVICE_ATTR(direction, 0644, |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 345 | gpio_direction_show, gpio_direction_store); |
| 346 | |
| 347 | static ssize_t gpio_value_show(struct device *dev, |
| 348 | struct device_attribute *attr, char *buf) |
| 349 | { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 350 | struct gpio_desc *desc = dev_get_drvdata(dev); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 351 | ssize_t status; |
| 352 | |
| 353 | mutex_lock(&sysfs_lock); |
| 354 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 355 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 356 | status = -EIO; |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 357 | else |
| 358 | status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc)); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 359 | |
| 360 | mutex_unlock(&sysfs_lock); |
| 361 | return status; |
| 362 | } |
| 363 | |
| 364 | static ssize_t gpio_value_store(struct device *dev, |
| 365 | struct device_attribute *attr, const char *buf, size_t size) |
| 366 | { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 367 | struct gpio_desc *desc = dev_get_drvdata(dev); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 368 | ssize_t status; |
| 369 | |
| 370 | mutex_lock(&sysfs_lock); |
| 371 | |
| 372 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 373 | status = -EIO; |
| 374 | else if (!test_bit(FLAG_IS_OUT, &desc->flags)) |
| 375 | status = -EPERM; |
| 376 | else { |
| 377 | long value; |
| 378 | |
Jingoo Han | a3d88c9 | 2013-07-19 16:12:50 +0900 | [diff] [blame] | 379 | status = kstrtol(buf, 0, &value); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 380 | if (status == 0) { |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 381 | gpiod_set_value_cansleep(desc, value); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 382 | status = size; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | mutex_unlock(&sysfs_lock); |
| 387 | return status; |
| 388 | } |
| 389 | |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 390 | static const DEVICE_ATTR(value, 0644, |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 391 | gpio_value_show, gpio_value_store); |
| 392 | |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 393 | static irqreturn_t gpio_sysfs_irq(int irq, void *priv) |
| 394 | { |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 395 | struct sysfs_dirent *value_sd = priv; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 396 | |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 397 | sysfs_notify_dirent(value_sd); |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 398 | return IRQ_HANDLED; |
| 399 | } |
| 400 | |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 401 | static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev, |
| 402 | unsigned long gpio_flags) |
| 403 | { |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 404 | struct sysfs_dirent *value_sd; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 405 | unsigned long irq_flags; |
| 406 | int ret, irq, id; |
| 407 | |
| 408 | if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags) |
| 409 | return 0; |
| 410 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 411 | irq = gpiod_to_irq(desc); |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 412 | if (irq < 0) |
| 413 | return -EIO; |
| 414 | |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 415 | id = desc->flags >> ID_SHIFT; |
| 416 | value_sd = idr_find(&dirent_idr, id); |
| 417 | if (value_sd) |
| 418 | free_irq(irq, value_sd); |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 419 | |
| 420 | desc->flags &= ~GPIO_TRIGGER_MASK; |
| 421 | |
| 422 | if (!gpio_flags) { |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 423 | gpiod_unlock_as_irq(desc); |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 424 | ret = 0; |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 425 | goto free_id; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | irq_flags = IRQF_SHARED; |
| 429 | if (test_bit(FLAG_TRIG_FALL, &gpio_flags)) |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 430 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
| 431 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 432 | if (test_bit(FLAG_TRIG_RISE, &gpio_flags)) |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 433 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
| 434 | IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 435 | |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 436 | if (!value_sd) { |
Tejun Heo | 388975c | 2013-09-11 23:19:13 -0400 | [diff] [blame] | 437 | value_sd = sysfs_get_dirent(dev->kobj.sd, "value"); |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 438 | if (!value_sd) { |
| 439 | ret = -ENODEV; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 440 | goto err_out; |
| 441 | } |
| 442 | |
Tejun Heo | 62f516b | 2013-02-27 17:04:06 -0800 | [diff] [blame] | 443 | ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL); |
| 444 | if (ret < 0) |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 445 | goto free_sd; |
Tejun Heo | 62f516b | 2013-02-27 17:04:06 -0800 | [diff] [blame] | 446 | id = ret; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 447 | |
| 448 | desc->flags &= GPIO_FLAGS_MASK; |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 449 | desc->flags |= (unsigned long)id << ID_SHIFT; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 450 | |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 451 | if (desc->flags >> ID_SHIFT != id) { |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 452 | ret = -ERANGE; |
| 453 | goto free_id; |
| 454 | } |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Daniel Gl?ckner | 364fadb3 | 2010-08-10 18:02:26 -0700 | [diff] [blame] | 457 | ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags, |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 458 | "gpiolib", value_sd); |
Daniel Gl?ckner | 364fadb3 | 2010-08-10 18:02:26 -0700 | [diff] [blame] | 459 | if (ret < 0) |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 460 | goto free_id; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 461 | |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 462 | ret = gpiod_lock_as_irq(desc); |
| 463 | if (ret < 0) { |
| 464 | gpiod_warn(desc, "failed to flag the GPIO for IRQ\n"); |
| 465 | goto free_id; |
| 466 | } |
| 467 | |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 468 | desc->flags |= gpio_flags; |
| 469 | return 0; |
| 470 | |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 471 | free_id: |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 472 | idr_remove(&dirent_idr, id); |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 473 | desc->flags &= GPIO_FLAGS_MASK; |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 474 | free_sd: |
| 475 | if (value_sd) |
| 476 | sysfs_put(value_sd); |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 477 | err_out: |
| 478 | return ret; |
| 479 | } |
| 480 | |
| 481 | static const struct { |
| 482 | const char *name; |
| 483 | unsigned long flags; |
| 484 | } trigger_types[] = { |
| 485 | { "none", 0 }, |
| 486 | { "falling", BIT(FLAG_TRIG_FALL) }, |
| 487 | { "rising", BIT(FLAG_TRIG_RISE) }, |
| 488 | { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) }, |
| 489 | }; |
| 490 | |
| 491 | static ssize_t gpio_edge_show(struct device *dev, |
| 492 | struct device_attribute *attr, char *buf) |
| 493 | { |
| 494 | const struct gpio_desc *desc = dev_get_drvdata(dev); |
| 495 | ssize_t status; |
| 496 | |
| 497 | mutex_lock(&sysfs_lock); |
| 498 | |
| 499 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 500 | status = -EIO; |
| 501 | else { |
| 502 | int i; |
| 503 | |
| 504 | status = 0; |
| 505 | for (i = 0; i < ARRAY_SIZE(trigger_types); i++) |
| 506 | if ((desc->flags & GPIO_TRIGGER_MASK) |
| 507 | == trigger_types[i].flags) { |
| 508 | status = sprintf(buf, "%s\n", |
| 509 | trigger_types[i].name); |
| 510 | break; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | mutex_unlock(&sysfs_lock); |
| 515 | return status; |
| 516 | } |
| 517 | |
| 518 | static ssize_t gpio_edge_store(struct device *dev, |
| 519 | struct device_attribute *attr, const char *buf, size_t size) |
| 520 | { |
| 521 | struct gpio_desc *desc = dev_get_drvdata(dev); |
| 522 | ssize_t status; |
| 523 | int i; |
| 524 | |
| 525 | for (i = 0; i < ARRAY_SIZE(trigger_types); i++) |
| 526 | if (sysfs_streq(trigger_types[i].name, buf)) |
| 527 | goto found; |
| 528 | return -EINVAL; |
| 529 | |
| 530 | found: |
| 531 | mutex_lock(&sysfs_lock); |
| 532 | |
| 533 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 534 | status = -EIO; |
| 535 | else { |
| 536 | status = gpio_setup_irq(desc, dev, trigger_types[i].flags); |
| 537 | if (!status) |
| 538 | status = size; |
| 539 | } |
| 540 | |
| 541 | mutex_unlock(&sysfs_lock); |
| 542 | |
| 543 | return status; |
| 544 | } |
| 545 | |
| 546 | static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store); |
| 547 | |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 548 | static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev, |
| 549 | int value) |
| 550 | { |
| 551 | int status = 0; |
| 552 | |
| 553 | if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value) |
| 554 | return 0; |
| 555 | |
| 556 | if (value) |
| 557 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); |
| 558 | else |
| 559 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); |
| 560 | |
| 561 | /* reconfigure poll(2) support if enabled on one edge only */ |
| 562 | if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^ |
| 563 | !!test_bit(FLAG_TRIG_FALL, &desc->flags))) { |
| 564 | unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK; |
| 565 | |
| 566 | gpio_setup_irq(desc, dev, 0); |
| 567 | status = gpio_setup_irq(desc, dev, trigger_flags); |
| 568 | } |
| 569 | |
| 570 | return status; |
| 571 | } |
| 572 | |
| 573 | static ssize_t gpio_active_low_show(struct device *dev, |
| 574 | struct device_attribute *attr, char *buf) |
| 575 | { |
| 576 | const struct gpio_desc *desc = dev_get_drvdata(dev); |
| 577 | ssize_t status; |
| 578 | |
| 579 | mutex_lock(&sysfs_lock); |
| 580 | |
| 581 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 582 | status = -EIO; |
| 583 | else |
| 584 | status = sprintf(buf, "%d\n", |
| 585 | !!test_bit(FLAG_ACTIVE_LOW, &desc->flags)); |
| 586 | |
| 587 | mutex_unlock(&sysfs_lock); |
| 588 | |
| 589 | return status; |
| 590 | } |
| 591 | |
| 592 | static ssize_t gpio_active_low_store(struct device *dev, |
| 593 | struct device_attribute *attr, const char *buf, size_t size) |
| 594 | { |
| 595 | struct gpio_desc *desc = dev_get_drvdata(dev); |
| 596 | ssize_t status; |
| 597 | |
| 598 | mutex_lock(&sysfs_lock); |
| 599 | |
| 600 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { |
| 601 | status = -EIO; |
| 602 | } else { |
| 603 | long value; |
| 604 | |
Jingoo Han | a3d88c9 | 2013-07-19 16:12:50 +0900 | [diff] [blame] | 605 | status = kstrtol(buf, 0, &value); |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 606 | if (status == 0) |
| 607 | status = sysfs_set_active_low(desc, dev, value != 0); |
| 608 | } |
| 609 | |
| 610 | mutex_unlock(&sysfs_lock); |
| 611 | |
| 612 | return status ? : size; |
| 613 | } |
| 614 | |
| 615 | static const DEVICE_ATTR(active_low, 0644, |
| 616 | gpio_active_low_show, gpio_active_low_store); |
| 617 | |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 618 | static const struct attribute *gpio_attrs[] = { |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 619 | &dev_attr_value.attr, |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 620 | &dev_attr_active_low.attr, |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 621 | NULL, |
| 622 | }; |
| 623 | |
| 624 | static const struct attribute_group gpio_attr_group = { |
| 625 | .attrs = (struct attribute **) gpio_attrs, |
| 626 | }; |
| 627 | |
| 628 | /* |
| 629 | * /sys/class/gpio/gpiochipN/ |
| 630 | * /base ... matching gpio_chip.base (N) |
| 631 | * /label ... matching gpio_chip.label |
| 632 | * /ngpio ... matching gpio_chip.ngpio |
| 633 | */ |
| 634 | |
| 635 | static ssize_t chip_base_show(struct device *dev, |
| 636 | struct device_attribute *attr, char *buf) |
| 637 | { |
| 638 | const struct gpio_chip *chip = dev_get_drvdata(dev); |
| 639 | |
| 640 | return sprintf(buf, "%d\n", chip->base); |
| 641 | } |
| 642 | static DEVICE_ATTR(base, 0444, chip_base_show, NULL); |
| 643 | |
| 644 | static ssize_t chip_label_show(struct device *dev, |
| 645 | struct device_attribute *attr, char *buf) |
| 646 | { |
| 647 | const struct gpio_chip *chip = dev_get_drvdata(dev); |
| 648 | |
| 649 | return sprintf(buf, "%s\n", chip->label ? : ""); |
| 650 | } |
| 651 | static DEVICE_ATTR(label, 0444, chip_label_show, NULL); |
| 652 | |
| 653 | static ssize_t chip_ngpio_show(struct device *dev, |
| 654 | struct device_attribute *attr, char *buf) |
| 655 | { |
| 656 | const struct gpio_chip *chip = dev_get_drvdata(dev); |
| 657 | |
| 658 | return sprintf(buf, "%u\n", chip->ngpio); |
| 659 | } |
| 660 | static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL); |
| 661 | |
| 662 | static const struct attribute *gpiochip_attrs[] = { |
| 663 | &dev_attr_base.attr, |
| 664 | &dev_attr_label.attr, |
| 665 | &dev_attr_ngpio.attr, |
| 666 | NULL, |
| 667 | }; |
| 668 | |
| 669 | static const struct attribute_group gpiochip_attr_group = { |
| 670 | .attrs = (struct attribute **) gpiochip_attrs, |
| 671 | }; |
| 672 | |
| 673 | /* |
| 674 | * /sys/class/gpio/export ... write-only |
| 675 | * integer N ... number of GPIO to export (full access) |
| 676 | * /sys/class/gpio/unexport ... write-only |
| 677 | * integer N ... number of GPIO to unexport |
| 678 | */ |
Andi Kleen | 28812fe | 2010-01-05 12:48:07 +0100 | [diff] [blame] | 679 | static ssize_t export_store(struct class *class, |
| 680 | struct class_attribute *attr, |
| 681 | const char *buf, size_t len) |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 682 | { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 683 | long gpio; |
| 684 | struct gpio_desc *desc; |
| 685 | int status; |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 686 | |
Jingoo Han | a3d88c9 | 2013-07-19 16:12:50 +0900 | [diff] [blame] | 687 | status = kstrtol(buf, 0, &gpio); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 688 | if (status < 0) |
| 689 | goto done; |
| 690 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 691 | desc = gpio_to_desc(gpio); |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 692 | /* reject invalid GPIOs */ |
| 693 | if (!desc) { |
| 694 | pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); |
| 695 | return -EINVAL; |
| 696 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 697 | |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 698 | /* No extra locking here; FLAG_SYSFS just signifies that the |
| 699 | * request and export were done by on behalf of userspace, so |
| 700 | * they may be undone on its behalf too. |
| 701 | */ |
| 702 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 703 | status = gpiod_request(desc, "sysfs"); |
Mathias Nyman | ad2fab3 | 2012-10-25 14:03:03 +0300 | [diff] [blame] | 704 | if (status < 0) { |
| 705 | if (status == -EPROBE_DEFER) |
| 706 | status = -ENODEV; |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 707 | goto done; |
Mathias Nyman | ad2fab3 | 2012-10-25 14:03:03 +0300 | [diff] [blame] | 708 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 709 | status = gpiod_export(desc, true); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 710 | if (status < 0) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 711 | gpiod_free(desc); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 712 | else |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 713 | set_bit(FLAG_SYSFS, &desc->flags); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 714 | |
| 715 | done: |
| 716 | if (status) |
| 717 | pr_debug("%s: status %d\n", __func__, status); |
| 718 | return status ? : len; |
| 719 | } |
| 720 | |
Andi Kleen | 28812fe | 2010-01-05 12:48:07 +0100 | [diff] [blame] | 721 | static ssize_t unexport_store(struct class *class, |
| 722 | struct class_attribute *attr, |
| 723 | const char *buf, size_t len) |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 724 | { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 725 | long gpio; |
| 726 | struct gpio_desc *desc; |
| 727 | int status; |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 728 | |
Jingoo Han | a3d88c9 | 2013-07-19 16:12:50 +0900 | [diff] [blame] | 729 | status = kstrtol(buf, 0, &gpio); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 730 | if (status < 0) |
| 731 | goto done; |
| 732 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 733 | desc = gpio_to_desc(gpio); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 734 | /* reject bogus commands (gpio_unexport ignores them) */ |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 735 | if (!desc) { |
| 736 | pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); |
| 737 | return -EINVAL; |
| 738 | } |
| 739 | |
| 740 | status = -EINVAL; |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 741 | |
| 742 | /* No extra locking here; FLAG_SYSFS just signifies that the |
| 743 | * request and export were done by on behalf of userspace, so |
| 744 | * they may be undone on its behalf too. |
| 745 | */ |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 746 | if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) { |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 747 | status = 0; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 748 | gpiod_free(desc); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 749 | } |
| 750 | done: |
| 751 | if (status) |
| 752 | pr_debug("%s: status %d\n", __func__, status); |
| 753 | return status ? : len; |
| 754 | } |
| 755 | |
| 756 | static struct class_attribute gpio_class_attrs[] = { |
| 757 | __ATTR(export, 0200, NULL, export_store), |
| 758 | __ATTR(unexport, 0200, NULL, unexport_store), |
| 759 | __ATTR_NULL, |
| 760 | }; |
| 761 | |
| 762 | static struct class gpio_class = { |
| 763 | .name = "gpio", |
| 764 | .owner = THIS_MODULE, |
| 765 | |
| 766 | .class_attrs = gpio_class_attrs, |
| 767 | }; |
| 768 | |
| 769 | |
| 770 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 771 | * gpiod_export - export a GPIO through sysfs |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 772 | * @gpio: gpio to make available, already requested |
| 773 | * @direction_may_change: true if userspace may change gpio direction |
| 774 | * Context: arch_initcall or later |
| 775 | * |
| 776 | * When drivers want to make a GPIO accessible to userspace after they |
| 777 | * have requested it -- perhaps while debugging, or as part of their |
| 778 | * public interface -- they may use this routine. If the GPIO can |
| 779 | * change direction (some can't) and the caller allows it, userspace |
| 780 | * will see "direction" sysfs attribute which may be used to change |
| 781 | * the gpio's direction. A "value" attribute will always be provided. |
| 782 | * |
| 783 | * Returns zero on success, else an error. |
| 784 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 785 | int gpiod_export(struct gpio_desc *desc, bool direction_may_change) |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 786 | { |
| 787 | unsigned long flags; |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 788 | int status; |
Uwe Kleine-König | 6215499 | 2010-05-26 14:42:17 -0700 | [diff] [blame] | 789 | const char *ioname = NULL; |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 790 | struct device *dev; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 791 | int offset; |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 792 | |
| 793 | /* can't export until sysfs is available ... */ |
| 794 | if (!gpio_class.p) { |
| 795 | pr_debug("%s: called too early!\n", __func__); |
| 796 | return -ENOENT; |
| 797 | } |
| 798 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 799 | if (!desc) { |
| 800 | pr_debug("%s: invalid gpio descriptor\n", __func__); |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 801 | return -EINVAL; |
| 802 | } |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 803 | |
| 804 | mutex_lock(&sysfs_lock); |
| 805 | |
| 806 | spin_lock_irqsave(&gpio_lock, flags); |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 807 | if (!test_bit(FLAG_REQUESTED, &desc->flags) || |
| 808 | test_bit(FLAG_EXPORT, &desc->flags)) { |
| 809 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 810 | pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n", |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 811 | __func__, desc_to_gpio(desc), |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 812 | test_bit(FLAG_REQUESTED, &desc->flags), |
| 813 | test_bit(FLAG_EXPORT, &desc->flags)); |
Dan Carpenter | 529f2ad | 2012-10-26 09:59:43 +0300 | [diff] [blame] | 814 | status = -EPERM; |
| 815 | goto fail_unlock; |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 816 | } |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 817 | |
| 818 | if (!desc->chip->direction_input || !desc->chip->direction_output) |
| 819 | direction_may_change = false; |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 820 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 821 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 822 | offset = gpio_chip_hwgpio(desc); |
| 823 | if (desc->chip->names && desc->chip->names[offset]) |
| 824 | ioname = desc->chip->names[offset]; |
Daniel Silverstone | 926b663 | 2009-04-02 16:57:05 -0700 | [diff] [blame] | 825 | |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 826 | dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0), |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 827 | desc, ioname ? ioname : "gpio%u", |
| 828 | desc_to_gpio(desc)); |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 829 | if (IS_ERR(dev)) { |
| 830 | status = PTR_ERR(dev); |
| 831 | goto fail_unlock; |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 834 | status = sysfs_create_group(&dev->kobj, &gpio_attr_group); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 835 | if (status) |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 836 | goto fail_unregister_device; |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 837 | |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 838 | if (direction_may_change) { |
| 839 | status = device_create_file(dev, &dev_attr_direction); |
| 840 | if (status) |
| 841 | goto fail_unregister_device; |
| 842 | } |
| 843 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 844 | if (gpiod_to_irq(desc) >= 0 && (direction_may_change || |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 845 | !test_bit(FLAG_IS_OUT, &desc->flags))) { |
| 846 | status = device_create_file(dev, &dev_attr_edge); |
| 847 | if (status) |
| 848 | goto fail_unregister_device; |
| 849 | } |
| 850 | |
| 851 | set_bit(FLAG_EXPORT, &desc->flags); |
| 852 | mutex_unlock(&sysfs_lock); |
| 853 | return 0; |
| 854 | |
| 855 | fail_unregister_device: |
| 856 | device_unregister(dev); |
| 857 | fail_unlock: |
| 858 | mutex_unlock(&sysfs_lock); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 859 | pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), |
| 860 | status); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 861 | return status; |
| 862 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 863 | EXPORT_SYMBOL_GPL(gpiod_export); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 864 | |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 865 | static int match_export(struct device *dev, const void *data) |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 866 | { |
| 867 | return dev_get_drvdata(dev) == data; |
| 868 | } |
| 869 | |
| 870 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 871 | * gpiod_export_link - create a sysfs link to an exported GPIO node |
Jani Nikula | a4177ee | 2009-09-22 16:46:33 -0700 | [diff] [blame] | 872 | * @dev: device under which to create symlink |
| 873 | * @name: name of the symlink |
| 874 | * @gpio: gpio to create symlink to, already exported |
| 875 | * |
| 876 | * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN |
| 877 | * node. Caller is responsible for unlinking. |
| 878 | * |
| 879 | * Returns zero on success, else an error. |
| 880 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 881 | int gpiod_export_link(struct device *dev, const char *name, |
| 882 | struct gpio_desc *desc) |
Jani Nikula | a4177ee | 2009-09-22 16:46:33 -0700 | [diff] [blame] | 883 | { |
Jani Nikula | a4177ee | 2009-09-22 16:46:33 -0700 | [diff] [blame] | 884 | int status = -EINVAL; |
| 885 | |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 886 | if (!desc) { |
| 887 | pr_warn("%s: invalid GPIO\n", __func__); |
| 888 | return -EINVAL; |
| 889 | } |
Jani Nikula | a4177ee | 2009-09-22 16:46:33 -0700 | [diff] [blame] | 890 | |
| 891 | mutex_lock(&sysfs_lock); |
| 892 | |
Jani Nikula | a4177ee | 2009-09-22 16:46:33 -0700 | [diff] [blame] | 893 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
| 894 | struct device *tdev; |
| 895 | |
| 896 | tdev = class_find_device(&gpio_class, NULL, desc, match_export); |
| 897 | if (tdev != NULL) { |
| 898 | status = sysfs_create_link(&dev->kobj, &tdev->kobj, |
| 899 | name); |
| 900 | } else { |
| 901 | status = -ENODEV; |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | mutex_unlock(&sysfs_lock); |
| 906 | |
Jani Nikula | a4177ee | 2009-09-22 16:46:33 -0700 | [diff] [blame] | 907 | if (status) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 908 | pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), |
| 909 | status); |
Jani Nikula | a4177ee | 2009-09-22 16:46:33 -0700 | [diff] [blame] | 910 | |
| 911 | return status; |
| 912 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 913 | EXPORT_SYMBOL_GPL(gpiod_export_link); |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 914 | |
| 915 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 916 | * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 917 | * @gpio: gpio to change |
| 918 | * @value: non-zero to use active low, i.e. inverted values |
| 919 | * |
| 920 | * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute. |
| 921 | * The GPIO does not have to be exported yet. If poll(2) support has |
| 922 | * been enabled for either rising or falling edge, it will be |
| 923 | * reconfigured to follow the new polarity. |
| 924 | * |
| 925 | * Returns zero on success, else an error. |
| 926 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 927 | int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 928 | { |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 929 | struct device *dev = NULL; |
| 930 | int status = -EINVAL; |
| 931 | |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 932 | if (!desc) { |
| 933 | pr_warn("%s: invalid GPIO\n", __func__); |
| 934 | return -EINVAL; |
| 935 | } |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 936 | |
| 937 | mutex_lock(&sysfs_lock); |
| 938 | |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 939 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 940 | dev = class_find_device(&gpio_class, NULL, desc, match_export); |
| 941 | if (dev == NULL) { |
| 942 | status = -ENODEV; |
| 943 | goto unlock; |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | status = sysfs_set_active_low(desc, dev, value); |
| 948 | |
| 949 | unlock: |
| 950 | mutex_unlock(&sysfs_lock); |
| 951 | |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 952 | if (status) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 953 | pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), |
| 954 | status); |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 955 | |
| 956 | return status; |
| 957 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 958 | EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low); |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 959 | |
Jani Nikula | a4177ee | 2009-09-22 16:46:33 -0700 | [diff] [blame] | 960 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 961 | * gpiod_unexport - reverse effect of gpio_export() |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 962 | * @gpio: gpio to make unavailable |
| 963 | * |
| 964 | * This is implicit on gpio_free(). |
| 965 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 966 | void gpiod_unexport(struct gpio_desc *desc) |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 967 | { |
Jon Povey | 6a99ad4 | 2010-07-27 13:18:06 -0700 | [diff] [blame] | 968 | int status = 0; |
Ming Lei | 864533c | 2012-02-13 22:53:20 +0800 | [diff] [blame] | 969 | struct device *dev = NULL; |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 970 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 971 | if (!desc) { |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 972 | pr_warn("%s: invalid GPIO\n", __func__); |
| 973 | return; |
Jon Povey | 6a99ad4 | 2010-07-27 13:18:06 -0700 | [diff] [blame] | 974 | } |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 975 | |
| 976 | mutex_lock(&sysfs_lock); |
| 977 | |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 978 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 979 | |
| 980 | dev = class_find_device(&gpio_class, NULL, desc, match_export); |
| 981 | if (dev) { |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 982 | gpio_setup_irq(desc, dev, 0); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 983 | clear_bit(FLAG_EXPORT, &desc->flags); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 984 | } else |
| 985 | status = -ENODEV; |
| 986 | } |
| 987 | |
| 988 | mutex_unlock(&sysfs_lock); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 989 | |
Ming Lei | 864533c | 2012-02-13 22:53:20 +0800 | [diff] [blame] | 990 | if (dev) { |
| 991 | device_unregister(dev); |
| 992 | put_device(dev); |
| 993 | } |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 994 | |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 995 | if (status) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 996 | pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), |
| 997 | status); |
| 998 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 999 | EXPORT_SYMBOL_GPL(gpiod_unexport); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1000 | |
| 1001 | static int gpiochip_export(struct gpio_chip *chip) |
| 1002 | { |
| 1003 | int status; |
| 1004 | struct device *dev; |
| 1005 | |
| 1006 | /* Many systems register gpio chips for SOC support very early, |
| 1007 | * before driver model support is available. In those cases we |
| 1008 | * export this later, in gpiolib_sysfs_init() ... here we just |
| 1009 | * verify that _some_ field of gpio_class got initialized. |
| 1010 | */ |
| 1011 | if (!gpio_class.p) |
| 1012 | return 0; |
| 1013 | |
| 1014 | /* use chip->base for the ID; it's already known to be unique */ |
| 1015 | mutex_lock(&sysfs_lock); |
| 1016 | dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip, |
| 1017 | "gpiochip%d", chip->base); |
Sergei Shtylyov | d62668e | 2009-11-11 14:26:50 -0800 | [diff] [blame] | 1018 | if (!IS_ERR(dev)) { |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1019 | status = sysfs_create_group(&dev->kobj, |
| 1020 | &gpiochip_attr_group); |
| 1021 | } else |
Sergei Shtylyov | d62668e | 2009-11-11 14:26:50 -0800 | [diff] [blame] | 1022 | status = PTR_ERR(dev); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1023 | chip->exported = (status == 0); |
| 1024 | mutex_unlock(&sysfs_lock); |
| 1025 | |
| 1026 | if (status) { |
| 1027 | unsigned long flags; |
| 1028 | unsigned gpio; |
| 1029 | |
| 1030 | spin_lock_irqsave(&gpio_lock, flags); |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 1031 | gpio = 0; |
| 1032 | while (gpio < chip->ngpio) |
| 1033 | chip->desc[gpio++].chip = NULL; |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1034 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1035 | |
| 1036 | pr_debug("%s: chip %s status %d\n", __func__, |
| 1037 | chip->label, status); |
| 1038 | } |
| 1039 | |
| 1040 | return status; |
| 1041 | } |
| 1042 | |
| 1043 | static void gpiochip_unexport(struct gpio_chip *chip) |
| 1044 | { |
| 1045 | int status; |
| 1046 | struct device *dev; |
| 1047 | |
| 1048 | mutex_lock(&sysfs_lock); |
| 1049 | dev = class_find_device(&gpio_class, NULL, chip, match_export); |
| 1050 | if (dev) { |
| 1051 | put_device(dev); |
| 1052 | device_unregister(dev); |
| 1053 | chip->exported = 0; |
| 1054 | status = 0; |
| 1055 | } else |
| 1056 | status = -ENODEV; |
| 1057 | mutex_unlock(&sysfs_lock); |
| 1058 | |
| 1059 | if (status) |
| 1060 | pr_debug("%s: chip %s status %d\n", __func__, |
| 1061 | chip->label, status); |
| 1062 | } |
| 1063 | |
| 1064 | static int __init gpiolib_sysfs_init(void) |
| 1065 | { |
| 1066 | int status; |
| 1067 | unsigned long flags; |
Alexandre Courbot | 65493e3 | 2013-02-03 01:29:25 +0900 | [diff] [blame] | 1068 | struct gpio_chip *chip; |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1069 | |
| 1070 | status = class_register(&gpio_class); |
| 1071 | if (status < 0) |
| 1072 | return status; |
| 1073 | |
| 1074 | /* Scan and register the gpio_chips which registered very |
| 1075 | * early (e.g. before the class_register above was called). |
| 1076 | * |
| 1077 | * We run before arch_initcall() so chip->dev nodes can have |
| 1078 | * registered, and so arch_initcall() can always gpio_export(). |
| 1079 | */ |
| 1080 | spin_lock_irqsave(&gpio_lock, flags); |
Alexandre Courbot | 65493e3 | 2013-02-03 01:29:25 +0900 | [diff] [blame] | 1081 | list_for_each_entry(chip, &gpio_chips, list) { |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1082 | if (!chip || chip->exported) |
| 1083 | continue; |
| 1084 | |
| 1085 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1086 | status = gpiochip_export(chip); |
| 1087 | spin_lock_irqsave(&gpio_lock, flags); |
| 1088 | } |
| 1089 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1090 | |
| 1091 | |
| 1092 | return status; |
| 1093 | } |
| 1094 | postcore_initcall(gpiolib_sysfs_init); |
| 1095 | |
| 1096 | #else |
| 1097 | static inline int gpiochip_export(struct gpio_chip *chip) |
| 1098 | { |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
| 1102 | static inline void gpiochip_unexport(struct gpio_chip *chip) |
| 1103 | { |
| 1104 | } |
| 1105 | |
| 1106 | #endif /* CONFIG_GPIO_SYSFS */ |
| 1107 | |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 1108 | /* |
| 1109 | * Add a new chip to the global chips list, keeping the list of chips sorted |
| 1110 | * by base order. |
| 1111 | * |
| 1112 | * Return -EBUSY if the new chip overlaps with some other chip's integer |
| 1113 | * space. |
| 1114 | */ |
| 1115 | static int gpiochip_add_to_list(struct gpio_chip *chip) |
| 1116 | { |
| 1117 | struct list_head *pos = &gpio_chips; |
| 1118 | struct gpio_chip *_chip; |
| 1119 | int err = 0; |
| 1120 | |
| 1121 | /* find where to insert our chip */ |
| 1122 | list_for_each(pos, &gpio_chips) { |
| 1123 | _chip = list_entry(pos, struct gpio_chip, list); |
| 1124 | /* shall we insert before _chip? */ |
| 1125 | if (_chip->base >= chip->base + chip->ngpio) |
| 1126 | break; |
| 1127 | } |
| 1128 | |
| 1129 | /* are we stepping on the chip right before? */ |
| 1130 | if (pos != &gpio_chips && pos->prev != &gpio_chips) { |
| 1131 | _chip = list_entry(pos->prev, struct gpio_chip, list); |
| 1132 | if (_chip->base + _chip->ngpio > chip->base) { |
| 1133 | dev_err(chip->dev, |
| 1134 | "GPIO integer space overlap, cannot add chip\n"); |
| 1135 | err = -EBUSY; |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | if (!err) |
| 1140 | list_add_tail(&chip->list, pos); |
| 1141 | |
| 1142 | return err; |
| 1143 | } |
| 1144 | |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 1145 | /** |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1146 | * gpiochip_add() - register a gpio_chip |
| 1147 | * @chip: the chip to register, with chip->base initialized |
| 1148 | * Context: potentially before irqs or kmalloc will work |
| 1149 | * |
| 1150 | * Returns a negative errno if the chip can't be registered, such as |
| 1151 | * because the chip->base is invalid or already associated with a |
| 1152 | * different chip. Otherwise it returns zero as a success code. |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1153 | * |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1154 | * When gpiochip_add() is called very early during boot, so that GPIOs |
| 1155 | * can be freely used, the chip->dev device must be registered before |
| 1156 | * the gpio framework's arch_initcall(). Otherwise sysfs initialization |
| 1157 | * for GPIOs will fail rudely. |
| 1158 | * |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1159 | * If chip->base is negative, this requests dynamic assignment of |
| 1160 | * a range of valid GPIOs. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1161 | */ |
| 1162 | int gpiochip_add(struct gpio_chip *chip) |
| 1163 | { |
| 1164 | unsigned long flags; |
| 1165 | int status = 0; |
| 1166 | unsigned id; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1167 | int base = chip->base; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1168 | |
Trent Piepho | bff5fda | 2008-05-23 13:04:44 -0700 | [diff] [blame] | 1169 | if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1)) |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1170 | && base >= 0) { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1171 | status = -EINVAL; |
| 1172 | goto fail; |
| 1173 | } |
| 1174 | |
| 1175 | spin_lock_irqsave(&gpio_lock, flags); |
| 1176 | |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1177 | if (base < 0) { |
| 1178 | base = gpiochip_find_base(chip->ngpio); |
| 1179 | if (base < 0) { |
| 1180 | status = base; |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1181 | goto unlock; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1182 | } |
| 1183 | chip->base = base; |
| 1184 | } |
| 1185 | |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 1186 | status = gpiochip_add_to_list(chip); |
| 1187 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1188 | if (status == 0) { |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 1189 | chip->desc = &gpio_desc[chip->base]; |
| 1190 | |
| 1191 | for (id = 0; id < chip->ngpio; id++) { |
| 1192 | struct gpio_desc *desc = &chip->desc[id]; |
| 1193 | desc->chip = chip; |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1194 | |
| 1195 | /* REVISIT: most hardware initializes GPIOs as |
| 1196 | * inputs (often with pullups enabled) so power |
| 1197 | * usage is minimized. Linux code should set the |
| 1198 | * gpio direction first thing; but until it does, |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 1199 | * and in case chip->get_direction is not set, |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1200 | * we may expose the wrong direction in sysfs. |
| 1201 | */ |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 1202 | desc->flags = !chip->direction_input |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1203 | ? (1 << FLAG_IS_OUT) |
| 1204 | : 0; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1205 | } |
| 1206 | } |
| 1207 | |
Zhangfei Gao | 3bae481 | 2013-06-09 11:08:32 +0800 | [diff] [blame] | 1208 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1209 | |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1210 | #ifdef CONFIG_PINCTRL |
| 1211 | INIT_LIST_HEAD(&chip->pin_ranges); |
| 1212 | #endif |
| 1213 | |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 1214 | of_gpiochip_add(chip); |
| 1215 | |
Anton Vorontsov | cedb188 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 1216 | if (status) |
| 1217 | goto fail; |
| 1218 | |
| 1219 | status = gpiochip_export(chip); |
| 1220 | if (status) |
| 1221 | goto fail; |
| 1222 | |
H Hartley Sweeten | ee1c1e7 | 2012-05-11 16:58:33 -0700 | [diff] [blame] | 1223 | pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n", |
Grant Likely | 64842aa | 2011-11-06 11:36:18 -0700 | [diff] [blame] | 1224 | chip->base, chip->base + chip->ngpio - 1, |
| 1225 | chip->label ? : "generic"); |
| 1226 | |
Anton Vorontsov | cedb188 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 1227 | return 0; |
Zhangfei Gao | 3bae481 | 2013-06-09 11:08:32 +0800 | [diff] [blame] | 1228 | |
| 1229 | unlock: |
| 1230 | spin_unlock_irqrestore(&gpio_lock, flags); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1231 | fail: |
| 1232 | /* failures here can mean systems won't boot... */ |
Anton Vorontsov | cedb188 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 1233 | pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n", |
| 1234 | chip->base, chip->base + chip->ngpio - 1, |
| 1235 | chip->label ? : "generic"); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1236 | return status; |
| 1237 | } |
| 1238 | EXPORT_SYMBOL_GPL(gpiochip_add); |
| 1239 | |
| 1240 | /** |
| 1241 | * gpiochip_remove() - unregister a gpio_chip |
| 1242 | * @chip: the chip to unregister |
| 1243 | * |
| 1244 | * A gpio_chip with any GPIOs still requested may not be removed. |
| 1245 | */ |
| 1246 | int gpiochip_remove(struct gpio_chip *chip) |
| 1247 | { |
| 1248 | unsigned long flags; |
| 1249 | int status = 0; |
| 1250 | unsigned id; |
| 1251 | |
| 1252 | spin_lock_irqsave(&gpio_lock, flags); |
| 1253 | |
Linus Walleij | 9ef0d6f | 2012-11-06 15:15:44 +0100 | [diff] [blame] | 1254 | gpiochip_remove_pin_ranges(chip); |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 1255 | of_gpiochip_remove(chip); |
| 1256 | |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 1257 | for (id = 0; id < chip->ngpio; id++) { |
| 1258 | if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1259 | status = -EBUSY; |
| 1260 | break; |
| 1261 | } |
| 1262 | } |
| 1263 | if (status == 0) { |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 1264 | for (id = 0; id < chip->ngpio; id++) |
| 1265 | chip->desc[id].chip = NULL; |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 1266 | |
| 1267 | list_del(&chip->list); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | spin_unlock_irqrestore(&gpio_lock, flags); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1271 | |
| 1272 | if (status == 0) |
| 1273 | gpiochip_unexport(chip); |
| 1274 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1275 | return status; |
| 1276 | } |
| 1277 | EXPORT_SYMBOL_GPL(gpiochip_remove); |
| 1278 | |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1279 | /** |
| 1280 | * gpiochip_find() - iterator for locating a specific gpio_chip |
| 1281 | * @data: data to pass to match function |
| 1282 | * @callback: Callback function to check gpio_chip |
| 1283 | * |
| 1284 | * Similar to bus_find_device. It returns a reference to a gpio_chip as |
| 1285 | * determined by a user supplied @match callback. The callback should return |
| 1286 | * 0 if the device doesn't match and non-zero if it does. If the callback is |
| 1287 | * non-zero, this function will return to the caller and not iterate over any |
| 1288 | * more gpio_chips. |
| 1289 | */ |
Grant Likely | 07ce8ec | 2012-05-18 23:01:05 -0600 | [diff] [blame] | 1290 | struct gpio_chip *gpiochip_find(void *data, |
Grant Likely | 6e2cf65 | 2012-03-02 15:56:03 -0700 | [diff] [blame] | 1291 | int (*match)(struct gpio_chip *chip, |
Grant Likely | 3d0f7cf | 2012-05-17 13:54:40 -0600 | [diff] [blame] | 1292 | void *data)) |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1293 | { |
Alexandre Courbot | 125eef9 | 2013-02-03 01:29:26 +0900 | [diff] [blame] | 1294 | struct gpio_chip *chip; |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1295 | unsigned long flags; |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1296 | |
| 1297 | spin_lock_irqsave(&gpio_lock, flags); |
Alexandre Courbot | 125eef9 | 2013-02-03 01:29:26 +0900 | [diff] [blame] | 1298 | list_for_each_entry(chip, &gpio_chips, list) |
| 1299 | if (match(chip, data)) |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1300 | break; |
Alexandre Courbot | 125eef9 | 2013-02-03 01:29:26 +0900 | [diff] [blame] | 1301 | |
| 1302 | /* No match? */ |
| 1303 | if (&chip->list == &gpio_chips) |
| 1304 | chip = NULL; |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1305 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1306 | |
| 1307 | return chip; |
| 1308 | } |
Jean Delvare | 8fa0c9b | 2011-05-20 00:40:18 -0600 | [diff] [blame] | 1309 | EXPORT_SYMBOL_GPL(gpiochip_find); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1310 | |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1311 | #ifdef CONFIG_PINCTRL |
Linus Walleij | 165adc9 | 2012-11-06 14:49:39 +0100 | [diff] [blame] | 1312 | |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1313 | /** |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 1314 | * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping |
| 1315 | * @chip: the gpiochip to add the range for |
| 1316 | * @pinctrl: the dev_name() of the pin controller to map to |
| 1317 | * @gpio_offset: the start offset in the current gpio_chip number space |
| 1318 | * @pin_group: name of the pin group inside the pin controller |
| 1319 | */ |
| 1320 | int gpiochip_add_pingroup_range(struct gpio_chip *chip, |
| 1321 | struct pinctrl_dev *pctldev, |
| 1322 | unsigned int gpio_offset, const char *pin_group) |
| 1323 | { |
| 1324 | struct gpio_pin_range *pin_range; |
| 1325 | int ret; |
| 1326 | |
| 1327 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); |
| 1328 | if (!pin_range) { |
| 1329 | pr_err("%s: GPIO chip: failed to allocate pin ranges\n", |
| 1330 | chip->label); |
| 1331 | return -ENOMEM; |
| 1332 | } |
| 1333 | |
| 1334 | /* Use local offset as range ID */ |
| 1335 | pin_range->range.id = gpio_offset; |
| 1336 | pin_range->range.gc = chip; |
| 1337 | pin_range->range.name = chip->label; |
| 1338 | pin_range->range.base = chip->base + gpio_offset; |
| 1339 | pin_range->pctldev = pctldev; |
| 1340 | |
| 1341 | ret = pinctrl_get_group_pins(pctldev, pin_group, |
| 1342 | &pin_range->range.pins, |
| 1343 | &pin_range->range.npins); |
| 1344 | if (ret < 0) |
| 1345 | return ret; |
| 1346 | |
| 1347 | pinctrl_add_gpio_range(pctldev, &pin_range->range); |
| 1348 | |
| 1349 | pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PINGRP %s\n", |
| 1350 | chip->label, gpio_offset, |
| 1351 | gpio_offset + pin_range->range.npins - 1, |
| 1352 | pinctrl_dev_get_devname(pctldev), pin_group); |
| 1353 | |
| 1354 | list_add_tail(&pin_range->node, &chip->pin_ranges); |
| 1355 | |
| 1356 | return 0; |
| 1357 | } |
| 1358 | EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range); |
| 1359 | |
| 1360 | /** |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1361 | * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping |
| 1362 | * @chip: the gpiochip to add the range for |
| 1363 | * @pinctrl_name: the dev_name() of the pin controller to map to |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 1364 | * @gpio_offset: the start offset in the current gpio_chip number space |
| 1365 | * @pin_offset: the start offset in the pin controller number space |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1366 | * @npins: the number of pins from the offset of each pin space (GPIO and |
| 1367 | * pin controller) to accumulate in this range |
| 1368 | */ |
Linus Walleij | 1e63d7b | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 1369 | int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name, |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 1370 | unsigned int gpio_offset, unsigned int pin_offset, |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1371 | unsigned int npins) |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1372 | { |
| 1373 | struct gpio_pin_range *pin_range; |
Axel Lin | b4d4b1f | 2012-11-21 14:33:56 +0800 | [diff] [blame] | 1374 | int ret; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1375 | |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1376 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1377 | if (!pin_range) { |
| 1378 | pr_err("%s: GPIO chip: failed to allocate pin ranges\n", |
| 1379 | chip->label); |
Linus Walleij | 1e63d7b | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 1380 | return -ENOMEM; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1381 | } |
| 1382 | |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1383 | /* Use local offset as range ID */ |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 1384 | pin_range->range.id = gpio_offset; |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1385 | pin_range->range.gc = chip; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1386 | pin_range->range.name = chip->label; |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 1387 | pin_range->range.base = chip->base + gpio_offset; |
| 1388 | pin_range->range.pin_base = pin_offset; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1389 | pin_range->range.npins = npins; |
Linus Walleij | 192c369 | 2012-11-20 14:03:37 +0100 | [diff] [blame] | 1390 | pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name, |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1391 | &pin_range->range); |
Linus Walleij | 8f23ca1 | 2012-11-20 14:56:25 +0100 | [diff] [blame] | 1392 | if (IS_ERR(pin_range->pctldev)) { |
Axel Lin | b4d4b1f | 2012-11-21 14:33:56 +0800 | [diff] [blame] | 1393 | ret = PTR_ERR(pin_range->pctldev); |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1394 | pr_err("%s: GPIO chip: could not create pin range\n", |
| 1395 | chip->label); |
| 1396 | kfree(pin_range); |
Axel Lin | b4d4b1f | 2012-11-21 14:33:56 +0800 | [diff] [blame] | 1397 | return ret; |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1398 | } |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 1399 | pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n", |
| 1400 | chip->label, gpio_offset, gpio_offset + npins - 1, |
| 1401 | pinctl_name, |
| 1402 | pin_offset, pin_offset + npins - 1); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1403 | |
| 1404 | list_add_tail(&pin_range->node, &chip->pin_ranges); |
Linus Walleij | 1e63d7b | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 1405 | |
| 1406 | return 0; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1407 | } |
Linus Walleij | 165adc9 | 2012-11-06 14:49:39 +0100 | [diff] [blame] | 1408 | EXPORT_SYMBOL_GPL(gpiochip_add_pin_range); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1409 | |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1410 | /** |
| 1411 | * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings |
| 1412 | * @chip: the chip to remove all the mappings for |
| 1413 | */ |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1414 | void gpiochip_remove_pin_ranges(struct gpio_chip *chip) |
| 1415 | { |
| 1416 | struct gpio_pin_range *pin_range, *tmp; |
| 1417 | |
| 1418 | list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) { |
| 1419 | list_del(&pin_range->node); |
| 1420 | pinctrl_remove_gpio_range(pin_range->pctldev, |
| 1421 | &pin_range->range); |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1422 | kfree(pin_range); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1423 | } |
| 1424 | } |
Linus Walleij | 165adc9 | 2012-11-06 14:49:39 +0100 | [diff] [blame] | 1425 | EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges); |
| 1426 | |
| 1427 | #endif /* CONFIG_PINCTRL */ |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1428 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1429 | /* These "optional" allocation calls help prevent drivers from stomping |
| 1430 | * on each other, and help provide better diagnostics in debugfs. |
| 1431 | * They're called even less than the "set direction" calls. |
| 1432 | */ |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1433 | static int gpiod_request(struct gpio_desc *desc, const char *label) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1434 | { |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1435 | struct gpio_chip *chip; |
Mark Brown | e935457 | 2012-07-09 12:22:56 +0100 | [diff] [blame] | 1436 | int status = -EPROBE_DEFER; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1437 | unsigned long flags; |
| 1438 | |
Alexandre Courbot | 0204df4 | 2013-10-04 10:59:58 -0700 | [diff] [blame] | 1439 | if (!desc) { |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1440 | pr_warn("%s: invalid GPIO\n", __func__); |
| 1441 | return -EINVAL; |
| 1442 | } |
| 1443 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1444 | spin_lock_irqsave(&gpio_lock, flags); |
| 1445 | |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1446 | chip = desc->chip; |
Alexandre Courbot | 0204df4 | 2013-10-04 10:59:58 -0700 | [diff] [blame] | 1447 | if (chip == NULL) |
| 1448 | goto done; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1449 | |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1450 | if (!try_module_get(chip->owner)) |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 1451 | goto done; |
| 1452 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1453 | /* NOTE: gpio_request() can be called in early boot, |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1454 | * before IRQs are enabled, for non-sleeping (SOC) GPIOs. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1455 | */ |
| 1456 | |
| 1457 | if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { |
| 1458 | desc_set_label(desc, label ? : "?"); |
| 1459 | status = 0; |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 1460 | } else { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1461 | status = -EBUSY; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1462 | module_put(chip->owner); |
Magnus Damm | 7460db5 | 2009-01-29 14:25:12 -0800 | [diff] [blame] | 1463 | goto done; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | if (chip->request) { |
| 1467 | /* chip->request may sleep */ |
| 1468 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1469 | status = chip->request(chip, gpio_chip_hwgpio(desc)); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1470 | spin_lock_irqsave(&gpio_lock, flags); |
| 1471 | |
| 1472 | if (status < 0) { |
| 1473 | desc_set_label(desc, NULL); |
| 1474 | module_put(chip->owner); |
| 1475 | clear_bit(FLAG_REQUESTED, &desc->flags); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 1476 | goto done; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1477 | } |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 1478 | } |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 1479 | if (chip->get_direction) { |
| 1480 | /* chip->get_direction may sleep */ |
| 1481 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1482 | gpiod_get_direction(desc); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 1483 | spin_lock_irqsave(&gpio_lock, flags); |
| 1484 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1485 | done: |
| 1486 | if (status) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1487 | pr_debug("_gpio_request: gpio-%d (%s) status %d\n", |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1488 | desc_to_gpio(desc), label ? : "?", status); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1489 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1490 | return status; |
| 1491 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1492 | |
| 1493 | int gpio_request(unsigned gpio, const char *label) |
| 1494 | { |
| 1495 | return gpiod_request(gpio_to_desc(gpio), label); |
| 1496 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1497 | EXPORT_SYMBOL_GPL(gpio_request); |
| 1498 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1499 | static void gpiod_free(struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1500 | { |
| 1501 | unsigned long flags; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1502 | struct gpio_chip *chip; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1503 | |
Uwe Kleine-König | 3d599d1 | 2008-10-15 22:03:12 -0700 | [diff] [blame] | 1504 | might_sleep(); |
| 1505 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1506 | if (!desc) { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1507 | WARN_ON(extra_checks); |
| 1508 | return; |
| 1509 | } |
| 1510 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1511 | gpiod_unexport(desc); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1512 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1513 | spin_lock_irqsave(&gpio_lock, flags); |
| 1514 | |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1515 | chip = desc->chip; |
| 1516 | if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) { |
| 1517 | if (chip->free) { |
| 1518 | spin_unlock_irqrestore(&gpio_lock, flags); |
David Brownell | 9c4ba94 | 2010-08-10 18:02:24 -0700 | [diff] [blame] | 1519 | might_sleep_if(chip->can_sleep); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1520 | chip->free(chip, gpio_chip_hwgpio(desc)); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1521 | spin_lock_irqsave(&gpio_lock, flags); |
| 1522 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1523 | desc_set_label(desc, NULL); |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 1524 | module_put(desc->chip->owner); |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 1525 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1526 | clear_bit(FLAG_REQUESTED, &desc->flags); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1527 | clear_bit(FLAG_OPEN_DRAIN, &desc->flags); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1528 | clear_bit(FLAG_OPEN_SOURCE, &desc->flags); |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 1529 | } else |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1530 | WARN_ON(extra_checks); |
| 1531 | |
| 1532 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1533 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1534 | |
| 1535 | void gpio_free(unsigned gpio) |
| 1536 | { |
| 1537 | gpiod_free(gpio_to_desc(gpio)); |
| 1538 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1539 | EXPORT_SYMBOL_GPL(gpio_free); |
| 1540 | |
Eric Miao | 3e45f1d | 2010-03-05 13:44:35 -0800 | [diff] [blame] | 1541 | /** |
| 1542 | * gpio_request_one - request a single GPIO with initial configuration |
| 1543 | * @gpio: the GPIO number |
| 1544 | * @flags: GPIO configuration as specified by GPIOF_* |
| 1545 | * @label: a literal description string of this GPIO |
| 1546 | */ |
| 1547 | int gpio_request_one(unsigned gpio, unsigned long flags, const char *label) |
| 1548 | { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1549 | struct gpio_desc *desc; |
Eric Miao | 3e45f1d | 2010-03-05 13:44:35 -0800 | [diff] [blame] | 1550 | int err; |
| 1551 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1552 | desc = gpio_to_desc(gpio); |
| 1553 | |
| 1554 | err = gpiod_request(desc, label); |
Eric Miao | 3e45f1d | 2010-03-05 13:44:35 -0800 | [diff] [blame] | 1555 | if (err) |
| 1556 | return err; |
| 1557 | |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1558 | if (flags & GPIOF_OPEN_DRAIN) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1559 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1560 | |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1561 | if (flags & GPIOF_OPEN_SOURCE) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1562 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1563 | |
Eric Miao | 3e45f1d | 2010-03-05 13:44:35 -0800 | [diff] [blame] | 1564 | if (flags & GPIOF_DIR_IN) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1565 | err = gpiod_direction_input(desc); |
Eric Miao | 3e45f1d | 2010-03-05 13:44:35 -0800 | [diff] [blame] | 1566 | else |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1567 | err = gpiod_direction_output(desc, |
Eric Miao | 3e45f1d | 2010-03-05 13:44:35 -0800 | [diff] [blame] | 1568 | (flags & GPIOF_INIT_HIGH) ? 1 : 0); |
| 1569 | |
Aaro Koskinen | e254811 | 2010-12-21 17:24:22 -0800 | [diff] [blame] | 1570 | if (err) |
Wolfram Sang | fc3a1f0 | 2011-12-13 18:34:01 +0100 | [diff] [blame] | 1571 | goto free_gpio; |
Aaro Koskinen | e254811 | 2010-12-21 17:24:22 -0800 | [diff] [blame] | 1572 | |
Wolfram Sang | fc3a1f0 | 2011-12-13 18:34:01 +0100 | [diff] [blame] | 1573 | if (flags & GPIOF_EXPORT) { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1574 | err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE); |
Wolfram Sang | fc3a1f0 | 2011-12-13 18:34:01 +0100 | [diff] [blame] | 1575 | if (err) |
| 1576 | goto free_gpio; |
| 1577 | } |
| 1578 | |
| 1579 | return 0; |
| 1580 | |
| 1581 | free_gpio: |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1582 | gpiod_free(desc); |
Eric Miao | 3e45f1d | 2010-03-05 13:44:35 -0800 | [diff] [blame] | 1583 | return err; |
| 1584 | } |
| 1585 | EXPORT_SYMBOL_GPL(gpio_request_one); |
| 1586 | |
| 1587 | /** |
| 1588 | * gpio_request_array - request multiple GPIOs in a single call |
| 1589 | * @array: array of the 'struct gpio' |
| 1590 | * @num: how many GPIOs in the array |
| 1591 | */ |
Lars-Peter Clausen | 7c29597 | 2011-05-25 16:20:31 -0700 | [diff] [blame] | 1592 | int gpio_request_array(const struct gpio *array, size_t num) |
Eric Miao | 3e45f1d | 2010-03-05 13:44:35 -0800 | [diff] [blame] | 1593 | { |
| 1594 | int i, err; |
| 1595 | |
| 1596 | for (i = 0; i < num; i++, array++) { |
| 1597 | err = gpio_request_one(array->gpio, array->flags, array->label); |
| 1598 | if (err) |
| 1599 | goto err_free; |
| 1600 | } |
| 1601 | return 0; |
| 1602 | |
| 1603 | err_free: |
| 1604 | while (i--) |
| 1605 | gpio_free((--array)->gpio); |
| 1606 | return err; |
| 1607 | } |
| 1608 | EXPORT_SYMBOL_GPL(gpio_request_array); |
| 1609 | |
| 1610 | /** |
| 1611 | * gpio_free_array - release multiple GPIOs in a single call |
| 1612 | * @array: array of the 'struct gpio' |
| 1613 | * @num: how many GPIOs in the array |
| 1614 | */ |
Lars-Peter Clausen | 7c29597 | 2011-05-25 16:20:31 -0700 | [diff] [blame] | 1615 | void gpio_free_array(const struct gpio *array, size_t num) |
Eric Miao | 3e45f1d | 2010-03-05 13:44:35 -0800 | [diff] [blame] | 1616 | { |
| 1617 | while (num--) |
| 1618 | gpio_free((array++)->gpio); |
| 1619 | } |
| 1620 | EXPORT_SYMBOL_GPL(gpio_free_array); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1621 | |
| 1622 | /** |
| 1623 | * gpiochip_is_requested - return string iff signal was requested |
| 1624 | * @chip: controller managing the signal |
| 1625 | * @offset: of signal within controller's 0..(ngpio - 1) range |
| 1626 | * |
| 1627 | * Returns NULL if the GPIO is not currently requested, else a string. |
| 1628 | * If debugfs support is enabled, the string returned is the label passed |
| 1629 | * to gpio_request(); otherwise it is a meaningless constant. |
| 1630 | * |
| 1631 | * This function is for use by GPIO controller drivers. The label can |
| 1632 | * help with diagnostics, and knowing that the signal is used as a GPIO |
| 1633 | * can help avoid accidentally multiplexing it to another controller. |
| 1634 | */ |
| 1635 | const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset) |
| 1636 | { |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 1637 | struct gpio_desc *desc; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1638 | |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 1639 | if (!GPIO_OFFSET_VALID(chip, offset)) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1640 | return NULL; |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 1641 | |
| 1642 | desc = &chip->desc[offset]; |
| 1643 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1644 | if (test_bit(FLAG_REQUESTED, &desc->flags) == 0) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1645 | return NULL; |
| 1646 | #ifdef CONFIG_DEBUG_FS |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1647 | return desc->label; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1648 | #else |
| 1649 | return "?"; |
| 1650 | #endif |
| 1651 | } |
| 1652 | EXPORT_SYMBOL_GPL(gpiochip_is_requested); |
| 1653 | |
| 1654 | |
| 1655 | /* Drivers MUST set GPIO direction before making get/set calls. In |
| 1656 | * some cases this is done in early boot, before IRQs are enabled. |
| 1657 | * |
| 1658 | * As a rule these aren't called more than once (except for drivers |
| 1659 | * using the open-drain emulation idiom) so these are natural places |
| 1660 | * to accumulate extra debugging checks. Note that we can't (yet) |
| 1661 | * rely on gpio_request() having been called beforehand. |
| 1662 | */ |
| 1663 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1664 | /** |
| 1665 | * gpiod_direction_input - set the GPIO direction to input |
| 1666 | * @desc: GPIO to set to input |
| 1667 | * |
| 1668 | * Set the direction of the passed GPIO to input, such as gpiod_get_value() can |
| 1669 | * be called safely on it. |
| 1670 | * |
| 1671 | * Return 0 in case of success, else an error code. |
| 1672 | */ |
| 1673 | int gpiod_direction_input(struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1674 | { |
| 1675 | unsigned long flags; |
| 1676 | struct gpio_chip *chip; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1677 | int status = -EINVAL; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1678 | int offset; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1679 | |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1680 | if (!desc || !desc->chip) { |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1681 | pr_warn("%s: invalid GPIO\n", __func__); |
| 1682 | return -EINVAL; |
| 1683 | } |
| 1684 | |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1685 | chip = desc->chip; |
| 1686 | if (!chip->get || !chip->direction_input) { |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1687 | gpiod_warn(desc, |
| 1688 | "%s: missing get() or direction_input() operations\n", |
| 1689 | __func__); |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1690 | return -EIO; |
| 1691 | } |
| 1692 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1693 | spin_lock_irqsave(&gpio_lock, flags); |
| 1694 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1695 | status = gpio_ensure_requested(desc); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1696 | if (status < 0) |
| 1697 | goto fail; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1698 | |
| 1699 | /* now we know the gpio is valid and chip won't vanish */ |
| 1700 | |
| 1701 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1702 | |
David Brownell | 9c4ba94 | 2010-08-10 18:02:24 -0700 | [diff] [blame] | 1703 | might_sleep_if(chip->can_sleep); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1704 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1705 | offset = gpio_chip_hwgpio(desc); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1706 | if (status) { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1707 | status = chip->request(chip, offset); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1708 | if (status < 0) { |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1709 | gpiod_dbg(desc, "chip request fail, %d\n", status); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1710 | /* and it's not available to anyone else ... |
| 1711 | * gpio_request() is the fully clean solution. |
| 1712 | */ |
| 1713 | goto lose; |
| 1714 | } |
| 1715 | } |
| 1716 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1717 | status = chip->direction_input(chip, offset); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1718 | if (status == 0) |
| 1719 | clear_bit(FLAG_IS_OUT, &desc->flags); |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 1720 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1721 | trace_gpio_direction(desc_to_gpio(desc), 1, status); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1722 | lose: |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1723 | return status; |
| 1724 | fail: |
| 1725 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1726 | if (status) |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1727 | gpiod_dbg(desc, "%s status %d\n", __func__, status); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1728 | return status; |
| 1729 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1730 | EXPORT_SYMBOL_GPL(gpiod_direction_input); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1731 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1732 | /** |
| 1733 | * gpiod_direction_output - set the GPIO direction to input |
| 1734 | * @desc: GPIO to set to output |
| 1735 | * @value: initial output value of the GPIO |
| 1736 | * |
| 1737 | * Set the direction of the passed GPIO to output, such as gpiod_set_value() can |
| 1738 | * be called safely on it. The initial value of the output must be specified. |
| 1739 | * |
| 1740 | * Return 0 in case of success, else an error code. |
| 1741 | */ |
| 1742 | int gpiod_direction_output(struct gpio_desc *desc, int value) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1743 | { |
| 1744 | unsigned long flags; |
| 1745 | struct gpio_chip *chip; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1746 | int status = -EINVAL; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1747 | int offset; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1748 | |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1749 | if (!desc || !desc->chip) { |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1750 | pr_warn("%s: invalid GPIO\n", __func__); |
| 1751 | return -EINVAL; |
| 1752 | } |
| 1753 | |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1754 | /* GPIOs used for IRQs shall not be set as output */ |
| 1755 | if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) { |
| 1756 | gpiod_err(desc, |
| 1757 | "%s: tried to set a GPIO tied to an IRQ as output\n", |
| 1758 | __func__); |
| 1759 | return -EIO; |
| 1760 | } |
| 1761 | |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1762 | /* Open drain pin should not be driven to 1 */ |
| 1763 | if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1764 | return gpiod_direction_input(desc); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1765 | |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1766 | /* Open source pin should not be driven to 0 */ |
| 1767 | if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags)) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1768 | return gpiod_direction_input(desc); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1769 | |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1770 | chip = desc->chip; |
| 1771 | if (!chip->set || !chip->direction_output) { |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1772 | gpiod_warn(desc, |
| 1773 | "%s: missing set() or direction_output() operations\n", |
| 1774 | __func__); |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1775 | return -EIO; |
| 1776 | } |
| 1777 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1778 | spin_lock_irqsave(&gpio_lock, flags); |
| 1779 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1780 | status = gpio_ensure_requested(desc); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1781 | if (status < 0) |
| 1782 | goto fail; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1783 | |
| 1784 | /* now we know the gpio is valid and chip won't vanish */ |
| 1785 | |
| 1786 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1787 | |
David Brownell | 9c4ba94 | 2010-08-10 18:02:24 -0700 | [diff] [blame] | 1788 | might_sleep_if(chip->can_sleep); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1789 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1790 | offset = gpio_chip_hwgpio(desc); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1791 | if (status) { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1792 | status = chip->request(chip, offset); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1793 | if (status < 0) { |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1794 | gpiod_dbg(desc, "chip request fail, %d\n", status); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1795 | /* and it's not available to anyone else ... |
| 1796 | * gpio_request() is the fully clean solution. |
| 1797 | */ |
| 1798 | goto lose; |
| 1799 | } |
| 1800 | } |
| 1801 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1802 | status = chip->direction_output(chip, offset, value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1803 | if (status == 0) |
| 1804 | set_bit(FLAG_IS_OUT, &desc->flags); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1805 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
| 1806 | trace_gpio_direction(desc_to_gpio(desc), 0, status); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1807 | lose: |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1808 | return status; |
| 1809 | fail: |
| 1810 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1811 | if (status) |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1812 | gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1813 | return status; |
| 1814 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1815 | EXPORT_SYMBOL_GPL(gpiod_direction_output); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1816 | |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1817 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1818 | * gpiod_set_debounce - sets @debounce time for a @gpio |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1819 | * @gpio: the gpio to set debounce time |
| 1820 | * @debounce: debounce time is microseconds |
Linus Walleij | 65d8765 | 2013-09-04 14:17:08 +0200 | [diff] [blame] | 1821 | * |
| 1822 | * returns -ENOTSUPP if the controller does not support setting |
| 1823 | * debounce. |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1824 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1825 | int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1826 | { |
| 1827 | unsigned long flags; |
| 1828 | struct gpio_chip *chip; |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1829 | int status = -EINVAL; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1830 | int offset; |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1831 | |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1832 | if (!desc || !desc->chip) { |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1833 | pr_warn("%s: invalid GPIO\n", __func__); |
| 1834 | return -EINVAL; |
| 1835 | } |
| 1836 | |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1837 | chip = desc->chip; |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1838 | if (!chip->set || !chip->set_debounce) { |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1839 | gpiod_dbg(desc, |
| 1840 | "%s: missing set() or set_debounce() operations\n", |
| 1841 | __func__); |
Linus Walleij | 65d8765 | 2013-09-04 14:17:08 +0200 | [diff] [blame] | 1842 | return -ENOTSUPP; |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | spin_lock_irqsave(&gpio_lock, flags); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1846 | |
| 1847 | status = gpio_ensure_requested(desc); |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1848 | if (status < 0) |
| 1849 | goto fail; |
| 1850 | |
| 1851 | /* now we know the gpio is valid and chip won't vanish */ |
| 1852 | |
| 1853 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1854 | |
David Brownell | 9c4ba94 | 2010-08-10 18:02:24 -0700 | [diff] [blame] | 1855 | might_sleep_if(chip->can_sleep); |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1856 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1857 | offset = gpio_chip_hwgpio(desc); |
| 1858 | return chip->set_debounce(chip, offset, debounce); |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1859 | |
| 1860 | fail: |
| 1861 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1862 | if (status) |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1863 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1864 | |
| 1865 | return status; |
| 1866 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1867 | EXPORT_SYMBOL_GPL(gpiod_set_debounce); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1868 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1869 | /** |
| 1870 | * gpiod_is_active_low - test whether a GPIO is active-low or not |
| 1871 | * @desc: the gpio descriptor to test |
| 1872 | * |
| 1873 | * Returns 1 if the GPIO is active-low, 0 otherwise. |
| 1874 | */ |
| 1875 | int gpiod_is_active_low(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1876 | { |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1877 | return test_bit(FLAG_ACTIVE_LOW, &desc->flags); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1878 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1879 | EXPORT_SYMBOL_GPL(gpiod_is_active_low); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1880 | |
| 1881 | /* I/O calls are only valid after configuration completed; the relevant |
| 1882 | * "is this a valid GPIO" error checks should already have been done. |
| 1883 | * |
| 1884 | * "Get" operations are often inlinable as reading a pin value register, |
| 1885 | * and masking the relevant bit in that register. |
| 1886 | * |
| 1887 | * When "set" operations are inlinable, they involve writing that mask to |
| 1888 | * one register to set a low value, or a different register to set it high. |
| 1889 | * Otherwise locking is needed, so there may be little value to inlining. |
| 1890 | * |
| 1891 | *------------------------------------------------------------------------ |
| 1892 | * |
| 1893 | * IMPORTANT!!! The hot paths -- get/set value -- assume that callers |
| 1894 | * have requested the GPIO. That can include implicit requesting by |
| 1895 | * a direction setting call. Marking a gpio as requested locks its chip |
| 1896 | * in memory, guaranteeing that these table lookups need no more locking |
| 1897 | * and that gpiochip_remove() will fail. |
| 1898 | * |
| 1899 | * REVISIT when debugging, consider adding some instrumentation to ensure |
| 1900 | * that the GPIO was actually requested. |
| 1901 | */ |
| 1902 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1903 | static int _gpiod_get_raw_value(const struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1904 | { |
| 1905 | struct gpio_chip *chip; |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 1906 | int value; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1907 | int offset; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1908 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1909 | chip = desc->chip; |
| 1910 | offset = gpio_chip_hwgpio(desc); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1911 | value = chip->get ? chip->get(chip, offset) : 0; |
| 1912 | trace_gpio_value(desc_to_gpio(desc), 1, value); |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 1913 | return value; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1914 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1915 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1916 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1917 | * gpiod_get_raw_value() - return a gpio's raw value |
| 1918 | * @desc: gpio whose value will be returned |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1919 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 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. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1925 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1926 | int gpiod_get_raw_value(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1927 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1928 | if (!desc) |
| 1929 | return 0; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1930 | /* Should be using gpio_get_value_cansleep() */ |
Alexandre Courbot | d8e0ac0 | 2013-09-04 20:29:25 +0900 | [diff] [blame] | 1931 | WARN_ON(desc->chip->can_sleep); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1932 | return _gpiod_get_raw_value(desc); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1933 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1934 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1935 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 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) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1947 | { |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 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; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1959 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1960 | EXPORT_SYMBOL_GPL(gpiod_get_value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1961 | |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1962 | /* |
| 1963 | * _gpio_set_open_drain_value() - Set the open drain gpio's value. |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1964 | * @desc: gpio descriptor whose state need to be set. |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1965 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. |
| 1966 | */ |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1967 | static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value) |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1968 | { |
| 1969 | int err = 0; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1970 | struct gpio_chip *chip = desc->chip; |
| 1971 | int offset = gpio_chip_hwgpio(desc); |
| 1972 | |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1973 | if (value) { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1974 | err = chip->direction_input(chip, offset); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1975 | if (!err) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1976 | clear_bit(FLAG_IS_OUT, &desc->flags); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1977 | } else { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1978 | err = chip->direction_output(chip, offset, 0); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1979 | if (!err) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1980 | set_bit(FLAG_IS_OUT, &desc->flags); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1981 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1982 | trace_gpio_direction(desc_to_gpio(desc), value, err); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1983 | if (err < 0) |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1984 | gpiod_err(desc, |
| 1985 | "%s: Error in set_value for open drain err %d\n", |
| 1986 | __func__, err); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1987 | } |
| 1988 | |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1989 | /* |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1990 | * _gpio_set_open_source_value() - Set the open source gpio's value. |
| 1991 | * @desc: gpio descriptor whose state need to be set. |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1992 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. |
| 1993 | */ |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1994 | static void _gpio_set_open_source_value(struct gpio_desc *desc, int value) |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1995 | { |
| 1996 | int err = 0; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1997 | struct gpio_chip *chip = desc->chip; |
| 1998 | int offset = gpio_chip_hwgpio(desc); |
| 1999 | |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2000 | if (value) { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2001 | err = chip->direction_output(chip, offset, 1); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2002 | if (!err) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2003 | set_bit(FLAG_IS_OUT, &desc->flags); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2004 | } else { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2005 | err = chip->direction_input(chip, offset); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2006 | if (!err) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2007 | clear_bit(FLAG_IS_OUT, &desc->flags); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2008 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2009 | trace_gpio_direction(desc_to_gpio(desc), !value, err); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2010 | if (err < 0) |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 2011 | gpiod_err(desc, |
| 2012 | "%s: Error in set_value for open source err %d\n", |
| 2013 | __func__, err); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2014 | } |
| 2015 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2016 | static void _gpiod_set_raw_value(struct gpio_desc *desc, int value) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2017 | { |
| 2018 | struct gpio_chip *chip; |
| 2019 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2020 | chip = desc->chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2021 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
| 2022 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
| 2023 | _gpio_set_open_drain_value(desc, value); |
| 2024 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) |
| 2025 | _gpio_set_open_source_value(desc, value); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 2026 | else |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2027 | chip->set(chip, gpio_chip_hwgpio(desc), value); |
| 2028 | } |
| 2029 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2030 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2031 | * gpiod_set_raw_value() - assign a gpio's raw value |
| 2032 | * @desc: gpio whose value will be assigned |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2033 | * @value: value to assign |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2034 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2035 | * Set the raw value of the GPIO, i.e. the value of its physical line without |
| 2036 | * regard for its ACTIVE_LOW status. |
| 2037 | * |
| 2038 | * This function should be called from contexts where we cannot sleep, and will |
| 2039 | * complain if the GPIO chip functions potentially sleep. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2040 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2041 | void gpiod_set_raw_value(struct gpio_desc *desc, int value) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2042 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2043 | if (!desc) |
| 2044 | return; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2045 | /* Should be using gpio_set_value_cansleep() */ |
Alexandre Courbot | d8e0ac0 | 2013-09-04 20:29:25 +0900 | [diff] [blame] | 2046 | WARN_ON(desc->chip->can_sleep); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2047 | _gpiod_set_raw_value(desc, value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2048 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2049 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2050 | |
| 2051 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2052 | * gpiod_set_value() - assign a gpio's value |
| 2053 | * @desc: gpio whose value will be assigned |
| 2054 | * @value: value to assign |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2055 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2056 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into |
| 2057 | * account |
| 2058 | * |
| 2059 | * This function should be called from contexts where we cannot sleep, and will |
| 2060 | * complain if the GPIO chip functions potentially sleep. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2061 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2062 | void gpiod_set_value(struct gpio_desc *desc, int value) |
| 2063 | { |
| 2064 | if (!desc) |
| 2065 | return; |
| 2066 | /* Should be using gpio_set_value_cansleep() */ |
| 2067 | WARN_ON(desc->chip->can_sleep); |
| 2068 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 2069 | value = !value; |
| 2070 | _gpiod_set_raw_value(desc, value); |
| 2071 | } |
| 2072 | EXPORT_SYMBOL_GPL(gpiod_set_value); |
| 2073 | |
| 2074 | /** |
| 2075 | * gpiod_cansleep() - report whether gpio value access may sleep |
| 2076 | * @desc: gpio to check |
| 2077 | * |
| 2078 | */ |
| 2079 | int gpiod_cansleep(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2080 | { |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 2081 | if (!desc) |
| 2082 | return 0; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2083 | return desc->chip->can_sleep; |
| 2084 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2085 | EXPORT_SYMBOL_GPL(gpiod_cansleep); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2086 | |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 2087 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2088 | * gpiod_to_irq() - return the IRQ corresponding to a GPIO |
| 2089 | * @desc: gpio whose IRQ will be returned (already requested) |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 2090 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2091 | * Return the IRQ corresponding to the passed GPIO, or an error code in case of |
| 2092 | * error. |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 2093 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2094 | int gpiod_to_irq(const struct gpio_desc *desc) |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 2095 | { |
| 2096 | struct gpio_chip *chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2097 | int offset; |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 2098 | |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 2099 | if (!desc) |
| 2100 | return -EINVAL; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2101 | chip = desc->chip; |
| 2102 | offset = gpio_chip_hwgpio(desc); |
| 2103 | return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO; |
| 2104 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2105 | EXPORT_SYMBOL_GPL(gpiod_to_irq); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2106 | |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2107 | /** |
| 2108 | * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ |
| 2109 | * @gpio: the GPIO line to lock as used for IRQ |
| 2110 | * |
| 2111 | * This is used directly by GPIO drivers that want to lock down |
| 2112 | * a certain GPIO line to be used as IRQs, for example in the |
| 2113 | * .to_irq() callback of their gpio_chip, or in the .irq_enable() |
| 2114 | * of its irq_chip implementation if the GPIO is known from that |
| 2115 | * code. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2116 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2117 | int gpiod_lock_as_irq(struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2118 | { |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2119 | if (!desc) |
| 2120 | return -EINVAL; |
| 2121 | |
| 2122 | if (test_bit(FLAG_IS_OUT, &desc->flags)) { |
| 2123 | gpiod_err(desc, |
| 2124 | "%s: tried to flag a GPIO set as output for IRQ\n", |
| 2125 | __func__); |
| 2126 | return -EIO; |
| 2127 | } |
| 2128 | |
| 2129 | set_bit(FLAG_USED_AS_IRQ, &desc->flags); |
| 2130 | return 0; |
| 2131 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2132 | EXPORT_SYMBOL_GPL(gpiod_lock_as_irq); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2133 | |
| 2134 | int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset) |
| 2135 | { |
| 2136 | return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip, offset)); |
| 2137 | } |
| 2138 | EXPORT_SYMBOL_GPL(gpio_lock_as_irq); |
| 2139 | |
| 2140 | /** |
| 2141 | * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ |
| 2142 | * @gpio: the GPIO line to unlock from IRQ usage |
| 2143 | * |
| 2144 | * This is used directly by GPIO drivers that want to indicate |
| 2145 | * that a certain GPIO is no longer used exclusively for IRQ. |
| 2146 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2147 | void gpiod_unlock_as_irq(struct gpio_desc *desc) |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2148 | { |
| 2149 | if (!desc) |
| 2150 | return; |
| 2151 | |
| 2152 | clear_bit(FLAG_USED_AS_IRQ, &desc->flags); |
| 2153 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2154 | EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2155 | |
| 2156 | void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset) |
| 2157 | { |
| 2158 | return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip, offset)); |
| 2159 | } |
| 2160 | EXPORT_SYMBOL_GPL(gpio_unlock_as_irq); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2161 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2162 | /** |
| 2163 | * gpiod_get_raw_value_cansleep() - return a gpio's raw value |
| 2164 | * @desc: gpio whose value will be returned |
| 2165 | * |
| 2166 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding |
| 2167 | * its ACTIVE_LOW status. |
| 2168 | * |
| 2169 | * This function is to be called from contexts that can sleep. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2170 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2171 | int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2172 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2173 | might_sleep_if(extra_checks); |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 2174 | if (!desc) |
| 2175 | return 0; |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2176 | return _gpiod_get_raw_value(desc); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2177 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2178 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2179 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2180 | /** |
| 2181 | * gpiod_get_value_cansleep() - return a gpio's value |
| 2182 | * @desc: gpio whose value will be returned |
| 2183 | * |
| 2184 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into |
| 2185 | * account. |
| 2186 | * |
| 2187 | * This function is to be called from contexts that can sleep. |
| 2188 | */ |
| 2189 | int gpiod_get_value_cansleep(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2190 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2191 | int value; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2192 | |
| 2193 | might_sleep_if(extra_checks); |
| 2194 | if (!desc) |
| 2195 | return 0; |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2196 | |
| 2197 | value = _gpiod_get_raw_value(desc); |
| 2198 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 2199 | value = !value; |
| 2200 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2201 | return value; |
| 2202 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2203 | EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2204 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2205 | /** |
| 2206 | * gpiod_set_raw_value_cansleep() - assign a gpio's raw value |
| 2207 | * @desc: gpio whose value will be assigned |
| 2208 | * @value: value to assign |
| 2209 | * |
| 2210 | * Set the raw value of the GPIO, i.e. the value of its physical line without |
| 2211 | * regard for its ACTIVE_LOW status. |
| 2212 | * |
| 2213 | * This function is to be called from contexts that can sleep. |
| 2214 | */ |
| 2215 | void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2216 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2217 | might_sleep_if(extra_checks); |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 2218 | if (!desc) |
| 2219 | return; |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2220 | _gpiod_set_raw_value(desc, value); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2221 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2222 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2223 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2224 | /** |
| 2225 | * gpiod_set_value_cansleep() - assign a gpio's value |
| 2226 | * @desc: gpio whose value will be assigned |
| 2227 | * @value: value to assign |
| 2228 | * |
| 2229 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into |
| 2230 | * account |
| 2231 | * |
| 2232 | * This function is to be called from contexts that can sleep. |
| 2233 | */ |
| 2234 | void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2235 | { |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2236 | might_sleep_if(extra_checks); |
| 2237 | if (!desc) |
| 2238 | return; |
| 2239 | |
| 2240 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 2241 | value = !value; |
| 2242 | _gpiod_set_raw_value(desc, value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2243 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2244 | EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2245 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2246 | /** |
| 2247 | * gpiod_add_table() - register GPIO device consumers |
| 2248 | * @table: array of consumers to register |
| 2249 | * @num: number of consumers in table |
| 2250 | */ |
| 2251 | void gpiod_add_table(struct gpiod_lookup *table, size_t size) |
| 2252 | { |
| 2253 | mutex_lock(&gpio_lookup_lock); |
| 2254 | |
| 2255 | while (size--) { |
| 2256 | list_add_tail(&table->list, &gpio_lookup_list); |
| 2257 | table++; |
| 2258 | } |
| 2259 | |
| 2260 | mutex_unlock(&gpio_lookup_lock); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2261 | } |
| 2262 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2263 | /* |
| 2264 | * Caller must have a acquired gpio_lookup_lock |
| 2265 | */ |
| 2266 | static struct gpio_chip *find_chip_by_name(const char *name) |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 2267 | { |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2268 | struct gpio_chip *chip = NULL; |
| 2269 | |
| 2270 | list_for_each_entry(chip, &gpio_lookup_list, list) { |
| 2271 | if (chip->label == NULL) |
| 2272 | continue; |
| 2273 | if (!strcmp(chip->label, name)) |
| 2274 | break; |
| 2275 | } |
| 2276 | |
| 2277 | return chip; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2278 | } |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2279 | |
| 2280 | #ifdef CONFIG_OF |
| 2281 | static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, |
| 2282 | unsigned int idx, unsigned long *flags) |
| 2283 | { |
| 2284 | char prop_name[32]; /* 32 is max size of property name */ |
| 2285 | enum of_gpio_flags of_flags; |
| 2286 | struct gpio_desc *desc; |
| 2287 | |
| 2288 | if (con_id) |
| 2289 | snprintf(prop_name, 32, "%s-gpios", con_id); |
| 2290 | else |
| 2291 | snprintf(prop_name, 32, "gpios"); |
| 2292 | |
| 2293 | desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, |
| 2294 | &of_flags); |
| 2295 | |
| 2296 | if (IS_ERR(desc)) |
| 2297 | return desc; |
| 2298 | |
| 2299 | if (of_flags & OF_GPIO_ACTIVE_LOW) |
| 2300 | *flags |= GPIOF_ACTIVE_LOW; |
| 2301 | |
| 2302 | return desc; |
| 2303 | } |
| 2304 | #else |
| 2305 | static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, |
| 2306 | unsigned int idx, unsigned long *flags) |
| 2307 | { |
| 2308 | return ERR_PTR(-ENODEV); |
| 2309 | } |
| 2310 | #endif |
| 2311 | |
Mika Westerberg | 81f59e9 | 2013-10-10 11:01:09 +0300 | [diff] [blame] | 2312 | static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, |
| 2313 | unsigned int idx, unsigned long *flags) |
| 2314 | { |
Mika Westerberg | e01f440 | 2013-10-10 11:01:10 +0300 | [diff] [blame] | 2315 | struct acpi_gpio_info info; |
| 2316 | struct gpio_desc *desc; |
| 2317 | |
| 2318 | desc = acpi_get_gpiod_by_index(dev, idx, &info); |
| 2319 | if (IS_ERR(desc)) |
| 2320 | return desc; |
| 2321 | |
| 2322 | if (info.gpioint && info.active_low) |
| 2323 | *flags |= GPIOF_ACTIVE_LOW; |
| 2324 | |
| 2325 | return desc; |
Mika Westerberg | 81f59e9 | 2013-10-10 11:01:09 +0300 | [diff] [blame] | 2326 | } |
| 2327 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2328 | static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id, |
| 2329 | unsigned int idx, unsigned long *flags) |
| 2330 | { |
| 2331 | const char *dev_id = dev ? dev_name(dev) : NULL; |
| 2332 | struct gpio_desc *desc = ERR_PTR(-ENODEV); |
| 2333 | unsigned int match, best = 0; |
| 2334 | struct gpiod_lookup *p; |
| 2335 | |
| 2336 | mutex_lock(&gpio_lookup_lock); |
| 2337 | |
| 2338 | list_for_each_entry(p, &gpio_lookup_list, list) { |
| 2339 | match = 0; |
| 2340 | |
| 2341 | if (p->dev_id) { |
| 2342 | if (!dev_id || strcmp(p->dev_id, dev_id)) |
| 2343 | continue; |
| 2344 | |
| 2345 | match += 2; |
| 2346 | } |
| 2347 | |
| 2348 | if (p->con_id) { |
| 2349 | if (!con_id || strcmp(p->con_id, con_id)) |
| 2350 | continue; |
| 2351 | |
| 2352 | match += 1; |
| 2353 | } |
| 2354 | |
| 2355 | if (p->idx != idx) |
| 2356 | continue; |
| 2357 | |
| 2358 | if (match > best) { |
| 2359 | struct gpio_chip *chip; |
| 2360 | |
| 2361 | chip = find_chip_by_name(p->chip_label); |
| 2362 | |
| 2363 | if (!chip) { |
| 2364 | dev_warn(dev, "cannot find GPIO chip %s\n", |
| 2365 | p->chip_label); |
| 2366 | continue; |
| 2367 | } |
| 2368 | |
| 2369 | if (chip->ngpio >= p->chip_hwnum) { |
| 2370 | dev_warn(dev, "GPIO chip %s has %d GPIOs\n", |
| 2371 | chip->label, chip->ngpio); |
| 2372 | continue; |
| 2373 | } |
| 2374 | |
| 2375 | desc = gpio_to_desc(chip->base + p->chip_hwnum); |
| 2376 | *flags = p->flags; |
| 2377 | |
| 2378 | if (match != 3) |
| 2379 | best = match; |
| 2380 | else |
| 2381 | break; |
| 2382 | } |
| 2383 | } |
| 2384 | |
| 2385 | mutex_unlock(&gpio_lookup_lock); |
| 2386 | |
| 2387 | return desc; |
| 2388 | } |
| 2389 | |
| 2390 | /** |
| 2391 | * gpio_get - obtain a GPIO for a given GPIO function |
| 2392 | * @dev: GPIO consumer |
| 2393 | * @con_id: function within the GPIO consumer |
| 2394 | * |
| 2395 | * Return the GPIO descriptor corresponding to the function con_id of device |
| 2396 | * dev, or an IS_ERR() condition if an error occured. |
| 2397 | */ |
| 2398 | struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id) |
| 2399 | { |
| 2400 | return gpiod_get_index(dev, con_id, 0); |
| 2401 | } |
| 2402 | EXPORT_SYMBOL_GPL(gpiod_get); |
| 2403 | |
| 2404 | /** |
| 2405 | * gpiod_get_index - obtain a GPIO from a multi-index GPIO function |
| 2406 | * @dev: GPIO consumer |
| 2407 | * @con_id: function within the GPIO consumer |
| 2408 | * @idx: index of the GPIO to obtain in the consumer |
| 2409 | * |
| 2410 | * This variant of gpiod_get() allows to access GPIOs other than the first |
| 2411 | * defined one for functions that define several GPIOs. |
| 2412 | * |
| 2413 | * Return a valid GPIO descriptor, or an IS_ERR() condition in case of error. |
| 2414 | */ |
| 2415 | struct gpio_desc *__must_check gpiod_get_index(struct device *dev, |
| 2416 | const char *con_id, |
| 2417 | unsigned int idx) |
| 2418 | { |
| 2419 | struct gpio_desc *desc; |
| 2420 | int status; |
| 2421 | unsigned long flags = 0; |
| 2422 | |
| 2423 | dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id); |
| 2424 | |
| 2425 | /* Using device tree? */ |
| 2426 | if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) { |
| 2427 | dev_dbg(dev, "using device tree for GPIO lookup\n"); |
| 2428 | desc = of_find_gpio(dev, con_id, idx, &flags); |
Mika Westerberg | 81f59e9 | 2013-10-10 11:01:09 +0300 | [diff] [blame] | 2429 | } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) { |
| 2430 | dev_dbg(dev, "using ACPI for GPIO lookup\n"); |
| 2431 | desc = acpi_find_gpio(dev, con_id, idx, &flags); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2432 | } else { |
| 2433 | dev_dbg(dev, "using lookup tables for GPIO lookup"); |
| 2434 | desc = gpiod_find(dev, con_id, idx, &flags); |
| 2435 | } |
| 2436 | |
| 2437 | if (IS_ERR(desc)) { |
| 2438 | dev_warn(dev, "lookup for GPIO %s failed\n", con_id); |
| 2439 | return desc; |
| 2440 | } |
| 2441 | |
| 2442 | status = gpiod_request(desc, con_id); |
| 2443 | |
| 2444 | if (status < 0) |
| 2445 | return ERR_PTR(status); |
| 2446 | |
| 2447 | if (flags & GPIOF_ACTIVE_LOW) |
| 2448 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); |
| 2449 | |
| 2450 | return desc; |
| 2451 | } |
| 2452 | EXPORT_SYMBOL_GPL(gpiod_get_index); |
| 2453 | |
| 2454 | /** |
| 2455 | * gpiod_put - dispose of a GPIO descriptor |
| 2456 | * @desc: GPIO descriptor to dispose of |
| 2457 | * |
| 2458 | * No descriptor can be used after gpiod_put() has been called on it. |
| 2459 | */ |
| 2460 | void gpiod_put(struct gpio_desc *desc) |
| 2461 | { |
| 2462 | gpiod_free(desc); |
| 2463 | } |
| 2464 | EXPORT_SYMBOL_GPL(gpiod_put); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2465 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2466 | #ifdef CONFIG_DEBUG_FS |
| 2467 | |
| 2468 | static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 2469 | { |
| 2470 | unsigned i; |
| 2471 | unsigned gpio = chip->base; |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 2472 | struct gpio_desc *gdesc = &chip->desc[0]; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2473 | int is_out; |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2474 | int is_irq; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2475 | |
| 2476 | for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) { |
| 2477 | if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) |
| 2478 | continue; |
| 2479 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2480 | gpiod_get_direction(gdesc); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2481 | is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2482 | is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags); |
| 2483 | seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s", |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2484 | gpio, gdesc->label, |
| 2485 | is_out ? "out" : "in ", |
| 2486 | chip->get |
| 2487 | ? (chip->get(chip, i) ? "hi" : "lo") |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2488 | : "? ", |
| 2489 | is_irq ? "IRQ" : " "); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2490 | seq_printf(s, "\n"); |
| 2491 | } |
| 2492 | } |
| 2493 | |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2494 | static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2495 | { |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 2496 | unsigned long flags; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2497 | struct gpio_chip *chip = NULL; |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 2498 | loff_t index = *pos; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2499 | |
| 2500 | s->private = ""; |
| 2501 | |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 2502 | spin_lock_irqsave(&gpio_lock, flags); |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 2503 | list_for_each_entry(chip, &gpio_chips, list) |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 2504 | if (index-- == 0) { |
| 2505 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 2506 | return chip; |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 2507 | } |
| 2508 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 2509 | |
| 2510 | return NULL; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2511 | } |
| 2512 | |
| 2513 | static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 2514 | { |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 2515 | unsigned long flags; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2516 | struct gpio_chip *chip = v; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2517 | void *ret = NULL; |
| 2518 | |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 2519 | spin_lock_irqsave(&gpio_lock, flags); |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 2520 | if (list_is_last(&chip->list, &gpio_chips)) |
| 2521 | ret = NULL; |
| 2522 | else |
| 2523 | ret = list_entry(chip->list.next, struct gpio_chip, list); |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 2524 | spin_unlock_irqrestore(&gpio_lock, flags); |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2525 | |
| 2526 | s->private = "\n"; |
| 2527 | ++*pos; |
| 2528 | |
| 2529 | return ret; |
| 2530 | } |
| 2531 | |
| 2532 | static void gpiolib_seq_stop(struct seq_file *s, void *v) |
| 2533 | { |
| 2534 | } |
| 2535 | |
| 2536 | static int gpiolib_seq_show(struct seq_file *s, void *v) |
| 2537 | { |
| 2538 | struct gpio_chip *chip = v; |
| 2539 | struct device *dev; |
| 2540 | |
| 2541 | seq_printf(s, "%sGPIOs %d-%d", (char *)s->private, |
| 2542 | chip->base, chip->base + chip->ngpio - 1); |
| 2543 | dev = chip->dev; |
| 2544 | if (dev) |
| 2545 | seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus", |
| 2546 | dev_name(dev)); |
| 2547 | if (chip->label) |
| 2548 | seq_printf(s, ", %s", chip->label); |
| 2549 | if (chip->can_sleep) |
| 2550 | seq_printf(s, ", can sleep"); |
| 2551 | seq_printf(s, ":\n"); |
| 2552 | |
| 2553 | if (chip->dbg_show) |
| 2554 | chip->dbg_show(s, chip); |
| 2555 | else |
| 2556 | gpiolib_dbg_show(s, chip); |
| 2557 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2558 | return 0; |
| 2559 | } |
| 2560 | |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2561 | static const struct seq_operations gpiolib_seq_ops = { |
| 2562 | .start = gpiolib_seq_start, |
| 2563 | .next = gpiolib_seq_next, |
| 2564 | .stop = gpiolib_seq_stop, |
| 2565 | .show = gpiolib_seq_show, |
| 2566 | }; |
| 2567 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2568 | static int gpiolib_open(struct inode *inode, struct file *file) |
| 2569 | { |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2570 | return seq_open(file, &gpiolib_seq_ops); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2571 | } |
| 2572 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2573 | static const struct file_operations gpiolib_operations = { |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2574 | .owner = THIS_MODULE, |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2575 | .open = gpiolib_open, |
| 2576 | .read = seq_read, |
| 2577 | .llseek = seq_lseek, |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2578 | .release = seq_release, |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2579 | }; |
| 2580 | |
| 2581 | static int __init gpiolib_debugfs_init(void) |
| 2582 | { |
| 2583 | /* /sys/kernel/debug/gpio */ |
| 2584 | (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO, |
| 2585 | NULL, NULL, &gpiolib_operations); |
| 2586 | return 0; |
| 2587 | } |
| 2588 | subsys_initcall(gpiolib_debugfs_init); |
| 2589 | |
| 2590 | #endif /* DEBUG_FS */ |