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