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