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