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