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