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