Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 1 | #include <linux/idr.h> |
| 2 | #include <linux/mutex.h> |
| 3 | #include <linux/device.h> |
| 4 | #include <linux/sysfs.h> |
| 5 | #include <linux/gpio/consumer.h> |
| 6 | #include <linux/gpio/driver.h> |
| 7 | #include <linux/interrupt.h> |
| 8 | #include <linux/kdev_t.h> |
| 9 | |
| 10 | #include "gpiolib.h" |
| 11 | |
| 12 | static DEFINE_IDR(dirent_idr); |
| 13 | |
| 14 | |
| 15 | /* lock protects against unexport_gpio() being called while |
| 16 | * sysfs files are active. |
| 17 | */ |
| 18 | static DEFINE_MUTEX(sysfs_lock); |
| 19 | |
| 20 | /* |
| 21 | * /sys/class/gpio/gpioN... only for GPIOs that are exported |
| 22 | * /direction |
| 23 | * * MAY BE OMITTED if kernel won't allow direction changes |
| 24 | * * is read/write as "in" or "out" |
| 25 | * * may also be written as "high" or "low", initializing |
| 26 | * output value as specified ("out" implies "low") |
| 27 | * /value |
| 28 | * * always readable, subject to hardware behavior |
| 29 | * * may be writable, as zero/nonzero |
| 30 | * /edge |
| 31 | * * configures behavior of poll(2) on /value |
| 32 | * * available only if pin can generate IRQs on input |
| 33 | * * is read/write as "none", "falling", "rising", or "both" |
| 34 | * /active_low |
| 35 | * * configures polarity of /value |
| 36 | * * is read/write as zero/nonzero |
| 37 | * * also affects existing and subsequent "falling" and "rising" |
| 38 | * /edge configuration |
| 39 | */ |
| 40 | |
| 41 | static ssize_t gpio_direction_show(struct device *dev, |
| 42 | struct device_attribute *attr, char *buf) |
| 43 | { |
Alexandre Courbot | 8e53b0f | 2014-11-25 17:16:31 +0900 | [diff] [blame] | 44 | struct gpio_desc *desc = dev_get_drvdata(dev); |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 45 | ssize_t status; |
| 46 | |
| 47 | mutex_lock(&sysfs_lock); |
| 48 | |
| 49 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { |
| 50 | status = -EIO; |
| 51 | } else { |
| 52 | gpiod_get_direction(desc); |
| 53 | status = sprintf(buf, "%s\n", |
| 54 | test_bit(FLAG_IS_OUT, &desc->flags) |
| 55 | ? "out" : "in"); |
| 56 | } |
| 57 | |
| 58 | mutex_unlock(&sysfs_lock); |
| 59 | return status; |
| 60 | } |
| 61 | |
| 62 | static ssize_t gpio_direction_store(struct device *dev, |
| 63 | struct device_attribute *attr, const char *buf, size_t size) |
| 64 | { |
| 65 | struct gpio_desc *desc = dev_get_drvdata(dev); |
| 66 | ssize_t status; |
| 67 | |
| 68 | mutex_lock(&sysfs_lock); |
| 69 | |
| 70 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 71 | status = -EIO; |
| 72 | else if (sysfs_streq(buf, "high")) |
| 73 | status = gpiod_direction_output_raw(desc, 1); |
| 74 | else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low")) |
| 75 | status = gpiod_direction_output_raw(desc, 0); |
| 76 | else if (sysfs_streq(buf, "in")) |
| 77 | status = gpiod_direction_input(desc); |
| 78 | else |
| 79 | status = -EINVAL; |
| 80 | |
| 81 | mutex_unlock(&sysfs_lock); |
| 82 | return status ? : size; |
| 83 | } |
| 84 | |
| 85 | static /* const */ DEVICE_ATTR(direction, 0644, |
| 86 | gpio_direction_show, gpio_direction_store); |
| 87 | |
| 88 | static ssize_t gpio_value_show(struct device *dev, |
| 89 | struct device_attribute *attr, char *buf) |
| 90 | { |
| 91 | struct gpio_desc *desc = dev_get_drvdata(dev); |
| 92 | ssize_t status; |
| 93 | |
| 94 | mutex_lock(&sysfs_lock); |
| 95 | |
| 96 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 97 | status = -EIO; |
| 98 | else |
| 99 | status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc)); |
| 100 | |
| 101 | mutex_unlock(&sysfs_lock); |
| 102 | return status; |
| 103 | } |
| 104 | |
| 105 | static ssize_t gpio_value_store(struct device *dev, |
| 106 | struct device_attribute *attr, const char *buf, size_t size) |
| 107 | { |
| 108 | struct gpio_desc *desc = dev_get_drvdata(dev); |
| 109 | ssize_t status; |
| 110 | |
| 111 | mutex_lock(&sysfs_lock); |
| 112 | |
| 113 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 114 | status = -EIO; |
| 115 | else if (!test_bit(FLAG_IS_OUT, &desc->flags)) |
| 116 | status = -EPERM; |
| 117 | else { |
| 118 | long value; |
| 119 | |
| 120 | status = kstrtol(buf, 0, &value); |
| 121 | if (status == 0) { |
| 122 | gpiod_set_value_cansleep(desc, value); |
| 123 | status = size; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | mutex_unlock(&sysfs_lock); |
| 128 | return status; |
| 129 | } |
| 130 | |
Johan Hovold | 0915e6f | 2015-01-13 13:00:05 +0100 | [diff] [blame^] | 131 | static DEVICE_ATTR(value, 0644, |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 132 | gpio_value_show, gpio_value_store); |
| 133 | |
| 134 | static irqreturn_t gpio_sysfs_irq(int irq, void *priv) |
| 135 | { |
| 136 | struct kernfs_node *value_sd = priv; |
| 137 | |
| 138 | sysfs_notify_dirent(value_sd); |
| 139 | return IRQ_HANDLED; |
| 140 | } |
| 141 | |
| 142 | static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev, |
| 143 | unsigned long gpio_flags) |
| 144 | { |
| 145 | struct kernfs_node *value_sd; |
| 146 | unsigned long irq_flags; |
| 147 | int ret, irq, id; |
| 148 | |
| 149 | if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags) |
| 150 | return 0; |
| 151 | |
| 152 | irq = gpiod_to_irq(desc); |
| 153 | if (irq < 0) |
| 154 | return -EIO; |
| 155 | |
| 156 | id = desc->flags >> ID_SHIFT; |
| 157 | value_sd = idr_find(&dirent_idr, id); |
| 158 | if (value_sd) |
| 159 | free_irq(irq, value_sd); |
| 160 | |
| 161 | desc->flags &= ~GPIO_TRIGGER_MASK; |
| 162 | |
| 163 | if (!gpio_flags) { |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 164 | gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc)); |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 165 | ret = 0; |
| 166 | goto free_id; |
| 167 | } |
| 168 | |
| 169 | irq_flags = IRQF_SHARED; |
| 170 | if (test_bit(FLAG_TRIG_FALL, &gpio_flags)) |
| 171 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
| 172 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; |
| 173 | if (test_bit(FLAG_TRIG_RISE, &gpio_flags)) |
| 174 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
| 175 | IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; |
| 176 | |
| 177 | if (!value_sd) { |
| 178 | value_sd = sysfs_get_dirent(dev->kobj.sd, "value"); |
| 179 | if (!value_sd) { |
| 180 | ret = -ENODEV; |
| 181 | goto err_out; |
| 182 | } |
| 183 | |
| 184 | ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL); |
| 185 | if (ret < 0) |
| 186 | goto free_sd; |
| 187 | id = ret; |
| 188 | |
| 189 | desc->flags &= GPIO_FLAGS_MASK; |
| 190 | desc->flags |= (unsigned long)id << ID_SHIFT; |
| 191 | |
| 192 | if (desc->flags >> ID_SHIFT != id) { |
| 193 | ret = -ERANGE; |
| 194 | goto free_id; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags, |
| 199 | "gpiolib", value_sd); |
| 200 | if (ret < 0) |
| 201 | goto free_id; |
| 202 | |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 203 | ret = gpiochip_lock_as_irq(desc->chip, gpio_chip_hwgpio(desc)); |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 204 | if (ret < 0) { |
| 205 | gpiod_warn(desc, "failed to flag the GPIO for IRQ\n"); |
| 206 | goto free_id; |
| 207 | } |
| 208 | |
| 209 | desc->flags |= gpio_flags; |
| 210 | return 0; |
| 211 | |
| 212 | free_id: |
| 213 | idr_remove(&dirent_idr, id); |
| 214 | desc->flags &= GPIO_FLAGS_MASK; |
| 215 | free_sd: |
| 216 | if (value_sd) |
| 217 | sysfs_put(value_sd); |
| 218 | err_out: |
| 219 | return ret; |
| 220 | } |
| 221 | |
| 222 | static const struct { |
| 223 | const char *name; |
| 224 | unsigned long flags; |
| 225 | } trigger_types[] = { |
| 226 | { "none", 0 }, |
| 227 | { "falling", BIT(FLAG_TRIG_FALL) }, |
| 228 | { "rising", BIT(FLAG_TRIG_RISE) }, |
| 229 | { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) }, |
| 230 | }; |
| 231 | |
| 232 | static ssize_t gpio_edge_show(struct device *dev, |
| 233 | struct device_attribute *attr, char *buf) |
| 234 | { |
| 235 | const struct gpio_desc *desc = dev_get_drvdata(dev); |
| 236 | ssize_t status; |
| 237 | |
| 238 | mutex_lock(&sysfs_lock); |
| 239 | |
| 240 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 241 | status = -EIO; |
| 242 | else { |
| 243 | int i; |
| 244 | |
| 245 | status = 0; |
| 246 | for (i = 0; i < ARRAY_SIZE(trigger_types); i++) |
| 247 | if ((desc->flags & GPIO_TRIGGER_MASK) |
| 248 | == trigger_types[i].flags) { |
| 249 | status = sprintf(buf, "%s\n", |
| 250 | trigger_types[i].name); |
| 251 | break; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | mutex_unlock(&sysfs_lock); |
| 256 | return status; |
| 257 | } |
| 258 | |
| 259 | static ssize_t gpio_edge_store(struct device *dev, |
| 260 | struct device_attribute *attr, const char *buf, size_t size) |
| 261 | { |
| 262 | struct gpio_desc *desc = dev_get_drvdata(dev); |
| 263 | ssize_t status; |
| 264 | int i; |
| 265 | |
| 266 | for (i = 0; i < ARRAY_SIZE(trigger_types); i++) |
| 267 | if (sysfs_streq(trigger_types[i].name, buf)) |
| 268 | goto found; |
| 269 | return -EINVAL; |
| 270 | |
| 271 | found: |
| 272 | mutex_lock(&sysfs_lock); |
| 273 | |
| 274 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 275 | status = -EIO; |
| 276 | else { |
| 277 | status = gpio_setup_irq(desc, dev, trigger_types[i].flags); |
| 278 | if (!status) |
| 279 | status = size; |
| 280 | } |
| 281 | |
| 282 | mutex_unlock(&sysfs_lock); |
| 283 | |
| 284 | return status; |
| 285 | } |
| 286 | |
| 287 | static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store); |
| 288 | |
| 289 | static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev, |
| 290 | int value) |
| 291 | { |
| 292 | int status = 0; |
| 293 | |
| 294 | if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value) |
| 295 | return 0; |
| 296 | |
| 297 | if (value) |
| 298 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); |
| 299 | else |
| 300 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); |
| 301 | |
| 302 | /* reconfigure poll(2) support if enabled on one edge only */ |
| 303 | if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^ |
| 304 | !!test_bit(FLAG_TRIG_FALL, &desc->flags))) { |
| 305 | unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK; |
| 306 | |
| 307 | gpio_setup_irq(desc, dev, 0); |
| 308 | status = gpio_setup_irq(desc, dev, trigger_flags); |
| 309 | } |
| 310 | |
| 311 | return status; |
| 312 | } |
| 313 | |
| 314 | static ssize_t gpio_active_low_show(struct device *dev, |
| 315 | struct device_attribute *attr, char *buf) |
| 316 | { |
| 317 | const struct gpio_desc *desc = dev_get_drvdata(dev); |
| 318 | ssize_t status; |
| 319 | |
| 320 | mutex_lock(&sysfs_lock); |
| 321 | |
| 322 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 323 | status = -EIO; |
| 324 | else |
| 325 | status = sprintf(buf, "%d\n", |
| 326 | !!test_bit(FLAG_ACTIVE_LOW, &desc->flags)); |
| 327 | |
| 328 | mutex_unlock(&sysfs_lock); |
| 329 | |
| 330 | return status; |
| 331 | } |
| 332 | |
| 333 | static ssize_t gpio_active_low_store(struct device *dev, |
| 334 | struct device_attribute *attr, const char *buf, size_t size) |
| 335 | { |
| 336 | struct gpio_desc *desc = dev_get_drvdata(dev); |
| 337 | ssize_t status; |
| 338 | |
| 339 | mutex_lock(&sysfs_lock); |
| 340 | |
| 341 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { |
| 342 | status = -EIO; |
| 343 | } else { |
| 344 | long value; |
| 345 | |
| 346 | status = kstrtol(buf, 0, &value); |
| 347 | if (status == 0) |
| 348 | status = sysfs_set_active_low(desc, dev, value != 0); |
| 349 | } |
| 350 | |
| 351 | mutex_unlock(&sysfs_lock); |
| 352 | |
| 353 | return status ? : size; |
| 354 | } |
| 355 | |
Johan Hovold | 0915e6f | 2015-01-13 13:00:05 +0100 | [diff] [blame^] | 356 | static DEVICE_ATTR(active_low, 0644, |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 357 | gpio_active_low_show, gpio_active_low_store); |
| 358 | |
Johan Hovold | 0915e6f | 2015-01-13 13:00:05 +0100 | [diff] [blame^] | 359 | static struct attribute *gpio_attrs[] = { |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 360 | &dev_attr_value.attr, |
| 361 | &dev_attr_active_low.attr, |
| 362 | NULL, |
| 363 | }; |
Johan Hovold | 0915e6f | 2015-01-13 13:00:05 +0100 | [diff] [blame^] | 364 | ATTRIBUTE_GROUPS(gpio); |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 365 | |
| 366 | /* |
| 367 | * /sys/class/gpio/gpiochipN/ |
| 368 | * /base ... matching gpio_chip.base (N) |
| 369 | * /label ... matching gpio_chip.label |
| 370 | * /ngpio ... matching gpio_chip.ngpio |
| 371 | */ |
| 372 | |
| 373 | static ssize_t chip_base_show(struct device *dev, |
| 374 | struct device_attribute *attr, char *buf) |
| 375 | { |
| 376 | const struct gpio_chip *chip = dev_get_drvdata(dev); |
| 377 | |
| 378 | return sprintf(buf, "%d\n", chip->base); |
| 379 | } |
| 380 | static DEVICE_ATTR(base, 0444, chip_base_show, NULL); |
| 381 | |
| 382 | static ssize_t chip_label_show(struct device *dev, |
| 383 | struct device_attribute *attr, char *buf) |
| 384 | { |
| 385 | const struct gpio_chip *chip = dev_get_drvdata(dev); |
| 386 | |
| 387 | return sprintf(buf, "%s\n", chip->label ? : ""); |
| 388 | } |
| 389 | static DEVICE_ATTR(label, 0444, chip_label_show, NULL); |
| 390 | |
| 391 | static ssize_t chip_ngpio_show(struct device *dev, |
| 392 | struct device_attribute *attr, char *buf) |
| 393 | { |
| 394 | const struct gpio_chip *chip = dev_get_drvdata(dev); |
| 395 | |
| 396 | return sprintf(buf, "%u\n", chip->ngpio); |
| 397 | } |
| 398 | static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL); |
| 399 | |
Johan Hovold | 121b6a7 | 2015-01-13 13:00:04 +0100 | [diff] [blame] | 400 | static struct attribute *gpiochip_attrs[] = { |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 401 | &dev_attr_base.attr, |
| 402 | &dev_attr_label.attr, |
| 403 | &dev_attr_ngpio.attr, |
| 404 | NULL, |
| 405 | }; |
Johan Hovold | 121b6a7 | 2015-01-13 13:00:04 +0100 | [diff] [blame] | 406 | ATTRIBUTE_GROUPS(gpiochip); |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 407 | |
| 408 | /* |
| 409 | * /sys/class/gpio/export ... write-only |
| 410 | * integer N ... number of GPIO to export (full access) |
| 411 | * /sys/class/gpio/unexport ... write-only |
| 412 | * integer N ... number of GPIO to unexport |
| 413 | */ |
| 414 | static ssize_t export_store(struct class *class, |
| 415 | struct class_attribute *attr, |
| 416 | const char *buf, size_t len) |
| 417 | { |
| 418 | long gpio; |
| 419 | struct gpio_desc *desc; |
| 420 | int status; |
| 421 | |
| 422 | status = kstrtol(buf, 0, &gpio); |
| 423 | if (status < 0) |
| 424 | goto done; |
| 425 | |
| 426 | desc = gpio_to_desc(gpio); |
| 427 | /* reject invalid GPIOs */ |
| 428 | if (!desc) { |
| 429 | pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); |
| 430 | return -EINVAL; |
| 431 | } |
| 432 | |
| 433 | /* No extra locking here; FLAG_SYSFS just signifies that the |
| 434 | * request and export were done by on behalf of userspace, so |
| 435 | * they may be undone on its behalf too. |
| 436 | */ |
| 437 | |
| 438 | status = gpiod_request(desc, "sysfs"); |
| 439 | if (status < 0) { |
| 440 | if (status == -EPROBE_DEFER) |
| 441 | status = -ENODEV; |
| 442 | goto done; |
| 443 | } |
| 444 | status = gpiod_export(desc, true); |
| 445 | if (status < 0) |
| 446 | gpiod_free(desc); |
| 447 | else |
| 448 | set_bit(FLAG_SYSFS, &desc->flags); |
| 449 | |
| 450 | done: |
| 451 | if (status) |
| 452 | pr_debug("%s: status %d\n", __func__, status); |
| 453 | return status ? : len; |
| 454 | } |
| 455 | |
| 456 | static ssize_t unexport_store(struct class *class, |
| 457 | struct class_attribute *attr, |
| 458 | const char *buf, size_t len) |
| 459 | { |
| 460 | long gpio; |
| 461 | struct gpio_desc *desc; |
| 462 | int status; |
| 463 | |
| 464 | status = kstrtol(buf, 0, &gpio); |
| 465 | if (status < 0) |
| 466 | goto done; |
| 467 | |
| 468 | desc = gpio_to_desc(gpio); |
| 469 | /* reject bogus commands (gpio_unexport ignores them) */ |
| 470 | if (!desc) { |
| 471 | pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); |
| 472 | return -EINVAL; |
| 473 | } |
| 474 | |
| 475 | status = -EINVAL; |
| 476 | |
| 477 | /* No extra locking here; FLAG_SYSFS just signifies that the |
| 478 | * request and export were done by on behalf of userspace, so |
| 479 | * they may be undone on its behalf too. |
| 480 | */ |
| 481 | if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) { |
| 482 | status = 0; |
| 483 | gpiod_free(desc); |
| 484 | } |
| 485 | done: |
| 486 | if (status) |
| 487 | pr_debug("%s: status %d\n", __func__, status); |
| 488 | return status ? : len; |
| 489 | } |
| 490 | |
| 491 | static struct class_attribute gpio_class_attrs[] = { |
| 492 | __ATTR(export, 0200, NULL, export_store), |
| 493 | __ATTR(unexport, 0200, NULL, unexport_store), |
| 494 | __ATTR_NULL, |
| 495 | }; |
| 496 | |
| 497 | static struct class gpio_class = { |
| 498 | .name = "gpio", |
| 499 | .owner = THIS_MODULE, |
| 500 | |
| 501 | .class_attrs = gpio_class_attrs, |
| 502 | }; |
| 503 | |
| 504 | |
| 505 | /** |
| 506 | * gpiod_export - export a GPIO through sysfs |
| 507 | * @gpio: gpio to make available, already requested |
| 508 | * @direction_may_change: true if userspace may change gpio direction |
| 509 | * Context: arch_initcall or later |
| 510 | * |
| 511 | * When drivers want to make a GPIO accessible to userspace after they |
| 512 | * have requested it -- perhaps while debugging, or as part of their |
| 513 | * public interface -- they may use this routine. If the GPIO can |
| 514 | * change direction (some can't) and the caller allows it, userspace |
| 515 | * will see "direction" sysfs attribute which may be used to change |
| 516 | * the gpio's direction. A "value" attribute will always be provided. |
| 517 | * |
| 518 | * Returns zero on success, else an error. |
| 519 | */ |
| 520 | int gpiod_export(struct gpio_desc *desc, bool direction_may_change) |
| 521 | { |
| 522 | unsigned long flags; |
| 523 | int status; |
| 524 | const char *ioname = NULL; |
| 525 | struct device *dev; |
| 526 | int offset; |
| 527 | |
| 528 | /* can't export until sysfs is available ... */ |
| 529 | if (!gpio_class.p) { |
| 530 | pr_debug("%s: called too early!\n", __func__); |
| 531 | return -ENOENT; |
| 532 | } |
| 533 | |
| 534 | if (!desc) { |
| 535 | pr_debug("%s: invalid gpio descriptor\n", __func__); |
| 536 | return -EINVAL; |
| 537 | } |
| 538 | |
| 539 | mutex_lock(&sysfs_lock); |
| 540 | |
| 541 | spin_lock_irqsave(&gpio_lock, flags); |
| 542 | if (!test_bit(FLAG_REQUESTED, &desc->flags) || |
| 543 | test_bit(FLAG_EXPORT, &desc->flags)) { |
| 544 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 545 | gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n", |
| 546 | __func__, |
| 547 | test_bit(FLAG_REQUESTED, &desc->flags), |
| 548 | test_bit(FLAG_EXPORT, &desc->flags)); |
| 549 | status = -EPERM; |
| 550 | goto fail_unlock; |
| 551 | } |
| 552 | |
| 553 | if (!desc->chip->direction_input || !desc->chip->direction_output) |
| 554 | direction_may_change = false; |
| 555 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 556 | |
| 557 | offset = gpio_chip_hwgpio(desc); |
| 558 | if (desc->chip->names && desc->chip->names[offset]) |
| 559 | ioname = desc->chip->names[offset]; |
| 560 | |
Johan Hovold | 0915e6f | 2015-01-13 13:00:05 +0100 | [diff] [blame^] | 561 | dev = device_create_with_groups(&gpio_class, desc->chip->dev, |
| 562 | MKDEV(0, 0), desc, gpio_groups, |
| 563 | ioname ? ioname : "gpio%u", |
| 564 | desc_to_gpio(desc)); |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 565 | if (IS_ERR(dev)) { |
| 566 | status = PTR_ERR(dev); |
| 567 | goto fail_unlock; |
| 568 | } |
| 569 | |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 570 | if (direction_may_change) { |
| 571 | status = device_create_file(dev, &dev_attr_direction); |
| 572 | if (status) |
| 573 | goto fail_unregister_device; |
| 574 | } |
| 575 | |
| 576 | if (gpiod_to_irq(desc) >= 0 && (direction_may_change || |
| 577 | !test_bit(FLAG_IS_OUT, &desc->flags))) { |
| 578 | status = device_create_file(dev, &dev_attr_edge); |
| 579 | if (status) |
Johan Hovold | 0915e6f | 2015-01-13 13:00:05 +0100 | [diff] [blame^] | 580 | goto fail_remove_attr_direction; |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | set_bit(FLAG_EXPORT, &desc->flags); |
| 584 | mutex_unlock(&sysfs_lock); |
| 585 | return 0; |
| 586 | |
Johan Hovold | 0915e6f | 2015-01-13 13:00:05 +0100 | [diff] [blame^] | 587 | fail_remove_attr_direction: |
| 588 | device_remove_file(dev, &dev_attr_direction); |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 589 | fail_unregister_device: |
| 590 | device_unregister(dev); |
| 591 | fail_unlock: |
| 592 | mutex_unlock(&sysfs_lock); |
| 593 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
| 594 | return status; |
| 595 | } |
| 596 | EXPORT_SYMBOL_GPL(gpiod_export); |
| 597 | |
| 598 | static int match_export(struct device *dev, const void *data) |
| 599 | { |
| 600 | return dev_get_drvdata(dev) == data; |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * gpiod_export_link - create a sysfs link to an exported GPIO node |
| 605 | * @dev: device under which to create symlink |
| 606 | * @name: name of the symlink |
| 607 | * @gpio: gpio to create symlink to, already exported |
| 608 | * |
| 609 | * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN |
| 610 | * node. Caller is responsible for unlinking. |
| 611 | * |
| 612 | * Returns zero on success, else an error. |
| 613 | */ |
| 614 | int gpiod_export_link(struct device *dev, const char *name, |
| 615 | struct gpio_desc *desc) |
| 616 | { |
| 617 | int status = -EINVAL; |
| 618 | |
| 619 | if (!desc) { |
| 620 | pr_warn("%s: invalid GPIO\n", __func__); |
| 621 | return -EINVAL; |
| 622 | } |
| 623 | |
| 624 | mutex_lock(&sysfs_lock); |
| 625 | |
| 626 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
| 627 | struct device *tdev; |
| 628 | |
| 629 | tdev = class_find_device(&gpio_class, NULL, desc, match_export); |
| 630 | if (tdev != NULL) { |
| 631 | status = sysfs_create_link(&dev->kobj, &tdev->kobj, |
| 632 | name); |
| 633 | } else { |
| 634 | status = -ENODEV; |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | mutex_unlock(&sysfs_lock); |
| 639 | |
| 640 | if (status) |
| 641 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
| 642 | |
| 643 | return status; |
| 644 | } |
| 645 | EXPORT_SYMBOL_GPL(gpiod_export_link); |
| 646 | |
| 647 | /** |
| 648 | * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value |
| 649 | * @gpio: gpio to change |
| 650 | * @value: non-zero to use active low, i.e. inverted values |
| 651 | * |
| 652 | * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute. |
| 653 | * The GPIO does not have to be exported yet. If poll(2) support has |
| 654 | * been enabled for either rising or falling edge, it will be |
| 655 | * reconfigured to follow the new polarity. |
| 656 | * |
| 657 | * Returns zero on success, else an error. |
| 658 | */ |
| 659 | int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) |
| 660 | { |
| 661 | struct device *dev = NULL; |
| 662 | int status = -EINVAL; |
| 663 | |
| 664 | if (!desc) { |
| 665 | pr_warn("%s: invalid GPIO\n", __func__); |
| 666 | return -EINVAL; |
| 667 | } |
| 668 | |
| 669 | mutex_lock(&sysfs_lock); |
| 670 | |
| 671 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
| 672 | dev = class_find_device(&gpio_class, NULL, desc, match_export); |
| 673 | if (dev == NULL) { |
| 674 | status = -ENODEV; |
| 675 | goto unlock; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | status = sysfs_set_active_low(desc, dev, value); |
| 680 | |
| 681 | unlock: |
| 682 | mutex_unlock(&sysfs_lock); |
| 683 | |
| 684 | if (status) |
| 685 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
| 686 | |
| 687 | return status; |
| 688 | } |
| 689 | EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low); |
| 690 | |
| 691 | /** |
| 692 | * gpiod_unexport - reverse effect of gpio_export() |
| 693 | * @gpio: gpio to make unavailable |
| 694 | * |
| 695 | * This is implicit on gpio_free(). |
| 696 | */ |
| 697 | void gpiod_unexport(struct gpio_desc *desc) |
| 698 | { |
| 699 | int status = 0; |
| 700 | struct device *dev = NULL; |
| 701 | |
| 702 | if (!desc) { |
| 703 | pr_warn("%s: invalid GPIO\n", __func__); |
| 704 | return; |
| 705 | } |
| 706 | |
| 707 | mutex_lock(&sysfs_lock); |
| 708 | |
| 709 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
| 710 | |
| 711 | dev = class_find_device(&gpio_class, NULL, desc, match_export); |
| 712 | if (dev) { |
| 713 | gpio_setup_irq(desc, dev, 0); |
| 714 | clear_bit(FLAG_EXPORT, &desc->flags); |
| 715 | } else |
| 716 | status = -ENODEV; |
| 717 | } |
| 718 | |
| 719 | mutex_unlock(&sysfs_lock); |
| 720 | |
| 721 | if (dev) { |
Johan Hovold | 0915e6f | 2015-01-13 13:00:05 +0100 | [diff] [blame^] | 722 | device_remove_file(dev, &dev_attr_edge); |
| 723 | device_remove_file(dev, &dev_attr_direction); |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 724 | device_unregister(dev); |
| 725 | put_device(dev); |
| 726 | } |
| 727 | |
| 728 | if (status) |
| 729 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
| 730 | } |
| 731 | EXPORT_SYMBOL_GPL(gpiod_unexport); |
| 732 | |
| 733 | int gpiochip_export(struct gpio_chip *chip) |
| 734 | { |
| 735 | int status; |
| 736 | struct device *dev; |
| 737 | |
| 738 | /* Many systems register gpio chips for SOC support very early, |
| 739 | * before driver model support is available. In those cases we |
| 740 | * export this later, in gpiolib_sysfs_init() ... here we just |
| 741 | * verify that _some_ field of gpio_class got initialized. |
| 742 | */ |
| 743 | if (!gpio_class.p) |
| 744 | return 0; |
| 745 | |
| 746 | /* use chip->base for the ID; it's already known to be unique */ |
| 747 | mutex_lock(&sysfs_lock); |
Johan Hovold | 121b6a7 | 2015-01-13 13:00:04 +0100 | [diff] [blame] | 748 | dev = device_create_with_groups(&gpio_class, chip->dev, MKDEV(0, 0), |
| 749 | chip, gpiochip_groups, |
| 750 | "gpiochip%d", chip->base); |
| 751 | if (IS_ERR(dev)) |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 752 | status = PTR_ERR(dev); |
Johan Hovold | 121b6a7 | 2015-01-13 13:00:04 +0100 | [diff] [blame] | 753 | else |
| 754 | status = 0; |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 755 | chip->exported = (status == 0); |
| 756 | mutex_unlock(&sysfs_lock); |
| 757 | |
Alexandre Courbot | 14141a9 | 2014-07-22 16:17:40 +0900 | [diff] [blame] | 758 | if (status) |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 759 | chip_dbg(chip, "%s: status %d\n", __func__, status); |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 760 | |
| 761 | return status; |
| 762 | } |
| 763 | |
| 764 | void gpiochip_unexport(struct gpio_chip *chip) |
| 765 | { |
| 766 | int status; |
| 767 | struct device *dev; |
| 768 | |
| 769 | mutex_lock(&sysfs_lock); |
| 770 | dev = class_find_device(&gpio_class, NULL, chip, match_export); |
| 771 | if (dev) { |
| 772 | put_device(dev); |
| 773 | device_unregister(dev); |
| 774 | chip->exported = false; |
| 775 | status = 0; |
| 776 | } else |
| 777 | status = -ENODEV; |
| 778 | mutex_unlock(&sysfs_lock); |
| 779 | |
| 780 | if (status) |
| 781 | chip_dbg(chip, "%s: status %d\n", __func__, status); |
| 782 | } |
| 783 | |
| 784 | static int __init gpiolib_sysfs_init(void) |
| 785 | { |
| 786 | int status; |
| 787 | unsigned long flags; |
| 788 | struct gpio_chip *chip; |
| 789 | |
| 790 | status = class_register(&gpio_class); |
| 791 | if (status < 0) |
| 792 | return status; |
| 793 | |
| 794 | /* Scan and register the gpio_chips which registered very |
| 795 | * early (e.g. before the class_register above was called). |
| 796 | * |
| 797 | * We run before arch_initcall() so chip->dev nodes can have |
| 798 | * registered, and so arch_initcall() can always gpio_export(). |
| 799 | */ |
| 800 | spin_lock_irqsave(&gpio_lock, flags); |
| 801 | list_for_each_entry(chip, &gpio_chips, list) { |
Alexandre Courbot | 7fd834a | 2014-07-23 23:56:53 +0900 | [diff] [blame] | 802 | if (chip->exported) |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 803 | continue; |
| 804 | |
Alexandre Courbot | 14141a9 | 2014-07-22 16:17:40 +0900 | [diff] [blame] | 805 | /* |
| 806 | * TODO we yield gpio_lock here because gpiochip_export() |
| 807 | * acquires a mutex. This is unsafe and needs to be fixed. |
| 808 | * |
| 809 | * Also it would be nice to use gpiochip_find() here so we |
| 810 | * can keep gpio_chips local to gpiolib.c, but the yield of |
| 811 | * gpio_lock prevents us from doing this. |
| 812 | */ |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 813 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 814 | status = gpiochip_export(chip); |
| 815 | spin_lock_irqsave(&gpio_lock, flags); |
| 816 | } |
| 817 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 818 | |
| 819 | |
| 820 | return status; |
| 821 | } |
| 822 | postcore_initcall(gpiolib_sysfs_init); |