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