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