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