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