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