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