Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

gpio: move sysfs support to its own file

sysfs support is currently entangled within the core GPIO support, while
it should relly just be a (privileged) user of the integer GPIO API.
This patch is a first step towards making the gpiolib code more readable
by splitting it into logical parts.

Move all sysfs support to their own source file, and share static
members of gpiolib that need to be in the private gpiolib.h file. In
the future we will want to put some of them back into gpiolib.c, but this
first patch let us at least identify them.

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

authored by

Alexandre Courbot and committed by
Linus Walleij
0eb4c6c2 9c8318ff

+925 -907
+1
drivers/gpio/Makefile
··· 5 5 obj-$(CONFIG_GPIO_DEVRES) += devres.o 6 6 obj-$(CONFIG_GPIOLIB) += gpiolib.o 7 7 obj-$(CONFIG_OF_GPIO) += gpiolib-of.o 8 + obj-$(CONFIG_GPIO_SYSFS) += gpiolib-sysfs.o 8 9 obj-$(CONFIG_GPIO_ACPI) += gpiolib-acpi.o 9 10 10 11 # Device drivers. Generally keep list sorted alphabetically
+829
drivers/gpio/gpiolib-sysfs.c
··· 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 + { 44 + const struct gpio_desc *desc = dev_get_drvdata(dev); 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 + 131 + static const DEVICE_ATTR(value, 0644, 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) { 164 + gpiod_unlock_as_irq(desc); 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 + 203 + ret = gpiod_lock_as_irq(desc); 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 + 356 + static const DEVICE_ATTR(active_low, 0644, 357 + gpio_active_low_show, gpio_active_low_store); 358 + 359 + static const struct attribute *gpio_attrs[] = { 360 + &dev_attr_value.attr, 361 + &dev_attr_active_low.attr, 362 + NULL, 363 + }; 364 + 365 + static const struct attribute_group gpio_attr_group = { 366 + .attrs = (struct attribute **) gpio_attrs, 367 + }; 368 + 369 + /* 370 + * /sys/class/gpio/gpiochipN/ 371 + * /base ... matching gpio_chip.base (N) 372 + * /label ... matching gpio_chip.label 373 + * /ngpio ... matching gpio_chip.ngpio 374 + */ 375 + 376 + static ssize_t chip_base_show(struct device *dev, 377 + struct device_attribute *attr, char *buf) 378 + { 379 + const struct gpio_chip *chip = dev_get_drvdata(dev); 380 + 381 + return sprintf(buf, "%d\n", chip->base); 382 + } 383 + static DEVICE_ATTR(base, 0444, chip_base_show, NULL); 384 + 385 + static ssize_t chip_label_show(struct device *dev, 386 + struct device_attribute *attr, char *buf) 387 + { 388 + const struct gpio_chip *chip = dev_get_drvdata(dev); 389 + 390 + return sprintf(buf, "%s\n", chip->label ? : ""); 391 + } 392 + static DEVICE_ATTR(label, 0444, chip_label_show, NULL); 393 + 394 + static ssize_t chip_ngpio_show(struct device *dev, 395 + struct device_attribute *attr, char *buf) 396 + { 397 + const struct gpio_chip *chip = dev_get_drvdata(dev); 398 + 399 + return sprintf(buf, "%u\n", chip->ngpio); 400 + } 401 + static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL); 402 + 403 + static const struct attribute *gpiochip_attrs[] = { 404 + &dev_attr_base.attr, 405 + &dev_attr_label.attr, 406 + &dev_attr_ngpio.attr, 407 + NULL, 408 + }; 409 + 410 + static const struct attribute_group gpiochip_attr_group = { 411 + .attrs = (struct attribute **) gpiochip_attrs, 412 + }; 413 + 414 + /* 415 + * /sys/class/gpio/export ... write-only 416 + * integer N ... number of GPIO to export (full access) 417 + * /sys/class/gpio/unexport ... write-only 418 + * integer N ... number of GPIO to unexport 419 + */ 420 + static ssize_t export_store(struct class *class, 421 + struct class_attribute *attr, 422 + const char *buf, size_t len) 423 + { 424 + long gpio; 425 + struct gpio_desc *desc; 426 + int status; 427 + 428 + status = kstrtol(buf, 0, &gpio); 429 + if (status < 0) 430 + goto done; 431 + 432 + desc = gpio_to_desc(gpio); 433 + /* reject invalid GPIOs */ 434 + if (!desc) { 435 + pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); 436 + return -EINVAL; 437 + } 438 + 439 + /* No extra locking here; FLAG_SYSFS just signifies that the 440 + * request and export were done by on behalf of userspace, so 441 + * they may be undone on its behalf too. 442 + */ 443 + 444 + status = gpiod_request(desc, "sysfs"); 445 + if (status < 0) { 446 + if (status == -EPROBE_DEFER) 447 + status = -ENODEV; 448 + goto done; 449 + } 450 + status = gpiod_export(desc, true); 451 + if (status < 0) 452 + gpiod_free(desc); 453 + else 454 + set_bit(FLAG_SYSFS, &desc->flags); 455 + 456 + done: 457 + if (status) 458 + pr_debug("%s: status %d\n", __func__, status); 459 + return status ? : len; 460 + } 461 + 462 + static ssize_t unexport_store(struct class *class, 463 + struct class_attribute *attr, 464 + const char *buf, size_t len) 465 + { 466 + long gpio; 467 + struct gpio_desc *desc; 468 + int status; 469 + 470 + status = kstrtol(buf, 0, &gpio); 471 + if (status < 0) 472 + goto done; 473 + 474 + desc = gpio_to_desc(gpio); 475 + /* reject bogus commands (gpio_unexport ignores them) */ 476 + if (!desc) { 477 + pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); 478 + return -EINVAL; 479 + } 480 + 481 + status = -EINVAL; 482 + 483 + /* No extra locking here; FLAG_SYSFS just signifies that the 484 + * request and export were done by on behalf of userspace, so 485 + * they may be undone on its behalf too. 486 + */ 487 + if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) { 488 + status = 0; 489 + gpiod_free(desc); 490 + } 491 + done: 492 + if (status) 493 + pr_debug("%s: status %d\n", __func__, status); 494 + return status ? : len; 495 + } 496 + 497 + static struct class_attribute gpio_class_attrs[] = { 498 + __ATTR(export, 0200, NULL, export_store), 499 + __ATTR(unexport, 0200, NULL, unexport_store), 500 + __ATTR_NULL, 501 + }; 502 + 503 + static struct class gpio_class = { 504 + .name = "gpio", 505 + .owner = THIS_MODULE, 506 + 507 + .class_attrs = gpio_class_attrs, 508 + }; 509 + 510 + 511 + /** 512 + * gpiod_export - export a GPIO through sysfs 513 + * @gpio: gpio to make available, already requested 514 + * @direction_may_change: true if userspace may change gpio direction 515 + * Context: arch_initcall or later 516 + * 517 + * When drivers want to make a GPIO accessible to userspace after they 518 + * have requested it -- perhaps while debugging, or as part of their 519 + * public interface -- they may use this routine. If the GPIO can 520 + * change direction (some can't) and the caller allows it, userspace 521 + * will see "direction" sysfs attribute which may be used to change 522 + * the gpio's direction. A "value" attribute will always be provided. 523 + * 524 + * Returns zero on success, else an error. 525 + */ 526 + int gpiod_export(struct gpio_desc *desc, bool direction_may_change) 527 + { 528 + unsigned long flags; 529 + int status; 530 + const char *ioname = NULL; 531 + struct device *dev; 532 + int offset; 533 + 534 + /* can't export until sysfs is available ... */ 535 + if (!gpio_class.p) { 536 + pr_debug("%s: called too early!\n", __func__); 537 + return -ENOENT; 538 + } 539 + 540 + if (!desc) { 541 + pr_debug("%s: invalid gpio descriptor\n", __func__); 542 + return -EINVAL; 543 + } 544 + 545 + mutex_lock(&sysfs_lock); 546 + 547 + spin_lock_irqsave(&gpio_lock, flags); 548 + if (!test_bit(FLAG_REQUESTED, &desc->flags) || 549 + test_bit(FLAG_EXPORT, &desc->flags)) { 550 + spin_unlock_irqrestore(&gpio_lock, flags); 551 + gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n", 552 + __func__, 553 + test_bit(FLAG_REQUESTED, &desc->flags), 554 + test_bit(FLAG_EXPORT, &desc->flags)); 555 + status = -EPERM; 556 + goto fail_unlock; 557 + } 558 + 559 + if (!desc->chip->direction_input || !desc->chip->direction_output) 560 + direction_may_change = false; 561 + spin_unlock_irqrestore(&gpio_lock, flags); 562 + 563 + offset = gpio_chip_hwgpio(desc); 564 + if (desc->chip->names && desc->chip->names[offset]) 565 + ioname = desc->chip->names[offset]; 566 + 567 + dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0), 568 + desc, ioname ? ioname : "gpio%u", 569 + desc_to_gpio(desc)); 570 + if (IS_ERR(dev)) { 571 + status = PTR_ERR(dev); 572 + goto fail_unlock; 573 + } 574 + 575 + status = sysfs_create_group(&dev->kobj, &gpio_attr_group); 576 + if (status) 577 + goto fail_unregister_device; 578 + 579 + if (direction_may_change) { 580 + status = device_create_file(dev, &dev_attr_direction); 581 + if (status) 582 + goto fail_unregister_device; 583 + } 584 + 585 + if (gpiod_to_irq(desc) >= 0 && (direction_may_change || 586 + !test_bit(FLAG_IS_OUT, &desc->flags))) { 587 + status = device_create_file(dev, &dev_attr_edge); 588 + if (status) 589 + goto fail_unregister_device; 590 + } 591 + 592 + set_bit(FLAG_EXPORT, &desc->flags); 593 + mutex_unlock(&sysfs_lock); 594 + return 0; 595 + 596 + fail_unregister_device: 597 + device_unregister(dev); 598 + fail_unlock: 599 + mutex_unlock(&sysfs_lock); 600 + gpiod_dbg(desc, "%s: status %d\n", __func__, status); 601 + return status; 602 + } 603 + EXPORT_SYMBOL_GPL(gpiod_export); 604 + 605 + static int match_export(struct device *dev, const void *data) 606 + { 607 + return dev_get_drvdata(dev) == data; 608 + } 609 + 610 + /** 611 + * gpiod_export_link - create a sysfs link to an exported GPIO node 612 + * @dev: device under which to create symlink 613 + * @name: name of the symlink 614 + * @gpio: gpio to create symlink to, already exported 615 + * 616 + * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN 617 + * node. Caller is responsible for unlinking. 618 + * 619 + * Returns zero on success, else an error. 620 + */ 621 + int gpiod_export_link(struct device *dev, const char *name, 622 + struct gpio_desc *desc) 623 + { 624 + int status = -EINVAL; 625 + 626 + if (!desc) { 627 + pr_warn("%s: invalid GPIO\n", __func__); 628 + return -EINVAL; 629 + } 630 + 631 + mutex_lock(&sysfs_lock); 632 + 633 + if (test_bit(FLAG_EXPORT, &desc->flags)) { 634 + struct device *tdev; 635 + 636 + tdev = class_find_device(&gpio_class, NULL, desc, match_export); 637 + if (tdev != NULL) { 638 + status = sysfs_create_link(&dev->kobj, &tdev->kobj, 639 + name); 640 + } else { 641 + status = -ENODEV; 642 + } 643 + } 644 + 645 + mutex_unlock(&sysfs_lock); 646 + 647 + if (status) 648 + gpiod_dbg(desc, "%s: status %d\n", __func__, status); 649 + 650 + return status; 651 + } 652 + EXPORT_SYMBOL_GPL(gpiod_export_link); 653 + 654 + /** 655 + * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value 656 + * @gpio: gpio to change 657 + * @value: non-zero to use active low, i.e. inverted values 658 + * 659 + * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute. 660 + * The GPIO does not have to be exported yet. If poll(2) support has 661 + * been enabled for either rising or falling edge, it will be 662 + * reconfigured to follow the new polarity. 663 + * 664 + * Returns zero on success, else an error. 665 + */ 666 + int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) 667 + { 668 + struct device *dev = NULL; 669 + int status = -EINVAL; 670 + 671 + if (!desc) { 672 + pr_warn("%s: invalid GPIO\n", __func__); 673 + return -EINVAL; 674 + } 675 + 676 + mutex_lock(&sysfs_lock); 677 + 678 + if (test_bit(FLAG_EXPORT, &desc->flags)) { 679 + dev = class_find_device(&gpio_class, NULL, desc, match_export); 680 + if (dev == NULL) { 681 + status = -ENODEV; 682 + goto unlock; 683 + } 684 + } 685 + 686 + status = sysfs_set_active_low(desc, dev, value); 687 + 688 + unlock: 689 + mutex_unlock(&sysfs_lock); 690 + 691 + if (status) 692 + gpiod_dbg(desc, "%s: status %d\n", __func__, status); 693 + 694 + return status; 695 + } 696 + EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low); 697 + 698 + /** 699 + * gpiod_unexport - reverse effect of gpio_export() 700 + * @gpio: gpio to make unavailable 701 + * 702 + * This is implicit on gpio_free(). 703 + */ 704 + void gpiod_unexport(struct gpio_desc *desc) 705 + { 706 + int status = 0; 707 + struct device *dev = NULL; 708 + 709 + if (!desc) { 710 + pr_warn("%s: invalid GPIO\n", __func__); 711 + return; 712 + } 713 + 714 + mutex_lock(&sysfs_lock); 715 + 716 + if (test_bit(FLAG_EXPORT, &desc->flags)) { 717 + 718 + dev = class_find_device(&gpio_class, NULL, desc, match_export); 719 + if (dev) { 720 + gpio_setup_irq(desc, dev, 0); 721 + clear_bit(FLAG_EXPORT, &desc->flags); 722 + } else 723 + status = -ENODEV; 724 + } 725 + 726 + mutex_unlock(&sysfs_lock); 727 + 728 + if (dev) { 729 + device_unregister(dev); 730 + put_device(dev); 731 + } 732 + 733 + if (status) 734 + gpiod_dbg(desc, "%s: status %d\n", __func__, status); 735 + } 736 + EXPORT_SYMBOL_GPL(gpiod_unexport); 737 + 738 + int gpiochip_export(struct gpio_chip *chip) 739 + { 740 + int status; 741 + struct device *dev; 742 + 743 + /* Many systems register gpio chips for SOC support very early, 744 + * before driver model support is available. In those cases we 745 + * export this later, in gpiolib_sysfs_init() ... here we just 746 + * verify that _some_ field of gpio_class got initialized. 747 + */ 748 + if (!gpio_class.p) 749 + return 0; 750 + 751 + /* use chip->base for the ID; it's already known to be unique */ 752 + mutex_lock(&sysfs_lock); 753 + dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip, 754 + "gpiochip%d", chip->base); 755 + if (!IS_ERR(dev)) { 756 + status = sysfs_create_group(&dev->kobj, 757 + &gpiochip_attr_group); 758 + } else 759 + status = PTR_ERR(dev); 760 + chip->exported = (status == 0); 761 + mutex_unlock(&sysfs_lock); 762 + 763 + if (status) { 764 + unsigned long flags; 765 + unsigned gpio; 766 + 767 + spin_lock_irqsave(&gpio_lock, flags); 768 + gpio = 0; 769 + while (gpio < chip->ngpio) 770 + chip->desc[gpio++].chip = NULL; 771 + spin_unlock_irqrestore(&gpio_lock, flags); 772 + 773 + chip_dbg(chip, "%s: status %d\n", __func__, status); 774 + } 775 + 776 + return status; 777 + } 778 + 779 + void gpiochip_unexport(struct gpio_chip *chip) 780 + { 781 + int status; 782 + struct device *dev; 783 + 784 + mutex_lock(&sysfs_lock); 785 + dev = class_find_device(&gpio_class, NULL, chip, match_export); 786 + if (dev) { 787 + put_device(dev); 788 + device_unregister(dev); 789 + chip->exported = false; 790 + status = 0; 791 + } else 792 + status = -ENODEV; 793 + mutex_unlock(&sysfs_lock); 794 + 795 + if (status) 796 + chip_dbg(chip, "%s: status %d\n", __func__, status); 797 + } 798 + 799 + static int __init gpiolib_sysfs_init(void) 800 + { 801 + int status; 802 + unsigned long flags; 803 + struct gpio_chip *chip; 804 + 805 + status = class_register(&gpio_class); 806 + if (status < 0) 807 + return status; 808 + 809 + /* Scan and register the gpio_chips which registered very 810 + * early (e.g. before the class_register above was called). 811 + * 812 + * We run before arch_initcall() so chip->dev nodes can have 813 + * registered, and so arch_initcall() can always gpio_export(). 814 + */ 815 + spin_lock_irqsave(&gpio_lock, flags); 816 + list_for_each_entry(chip, &gpio_chips, list) { 817 + if (!chip || chip->exported) 818 + continue; 819 + 820 + spin_unlock_irqrestore(&gpio_lock, flags); 821 + status = gpiochip_export(chip); 822 + spin_lock_irqsave(&gpio_lock, flags); 823 + } 824 + spin_unlock_irqrestore(&gpio_lock, flags); 825 + 826 + 827 + return status; 828 + } 829 + postcore_initcall(gpiolib_sysfs_init);
+4 -907
drivers/gpio/gpiolib.c
··· 44 44 * While any GPIO is requested, its gpio_chip is not removable; 45 45 * each GPIO's "requested" flag serves as a lock and refcount. 46 46 */ 47 - static DEFINE_SPINLOCK(gpio_lock); 47 + DEFINE_SPINLOCK(gpio_lock); 48 48 49 - struct gpio_desc { 50 - struct gpio_chip *chip; 51 - unsigned long flags; 52 - /* flag symbols are bit numbers */ 53 - #define FLAG_REQUESTED 0 54 - #define FLAG_IS_OUT 1 55 - #define FLAG_EXPORT 2 /* protected by sysfs_lock */ 56 - #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */ 57 - #define FLAG_TRIG_FALL 4 /* trigger on falling edge */ 58 - #define FLAG_TRIG_RISE 5 /* trigger on rising edge */ 59 - #define FLAG_ACTIVE_LOW 6 /* value has active low */ 60 - #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ 61 - #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ 62 - #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ 63 - 64 - #define ID_SHIFT 16 /* add new flags before this one */ 65 - 66 - #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1) 67 - #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE)) 68 - 69 - const char *label; 70 - }; 71 49 static struct gpio_desc gpio_desc[ARCH_NR_GPIOS]; 72 50 73 51 #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio) 74 52 75 53 static DEFINE_MUTEX(gpio_lookup_lock); 76 54 static LIST_HEAD(gpio_lookup_list); 77 - static LIST_HEAD(gpio_chips); 78 - 79 - #ifdef CONFIG_GPIO_SYSFS 80 - static DEFINE_IDR(dirent_idr); 81 - #endif 82 - 83 - static int gpiod_request(struct gpio_desc *desc, const char *label); 84 - static void gpiod_free(struct gpio_desc *desc); 85 - 86 - /* With descriptor prefix */ 87 - 88 - #define gpiod_emerg(desc, fmt, ...) \ 89 - pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ 90 - ##__VA_ARGS__) 91 - #define gpiod_crit(desc, fmt, ...) \ 92 - pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 93 - ##__VA_ARGS__) 94 - #define gpiod_err(desc, fmt, ...) \ 95 - pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 96 - ##__VA_ARGS__) 97 - #define gpiod_warn(desc, fmt, ...) \ 98 - pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 99 - ##__VA_ARGS__) 100 - #define gpiod_info(desc, fmt, ...) \ 101 - pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 102 - ##__VA_ARGS__) 103 - #define gpiod_dbg(desc, fmt, ...) \ 104 - pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ 105 - ##__VA_ARGS__) 106 - 107 - /* With chip prefix */ 108 - 109 - #define chip_emerg(chip, fmt, ...) \ 110 - pr_emerg("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) 111 - #define chip_crit(chip, fmt, ...) \ 112 - pr_crit("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) 113 - #define chip_err(chip, fmt, ...) \ 114 - pr_err("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) 115 - #define chip_warn(chip, fmt, ...) \ 116 - pr_warn("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) 117 - #define chip_info(chip, fmt, ...) \ 118 - pr_info("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) 119 - #define chip_dbg(chip, fmt, ...) \ 120 - pr_debug("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) 55 + LIST_HEAD(gpio_chips); 121 56 122 57 static inline void desc_set_label(struct gpio_desc *d, const char *label) 123 58 { 124 59 d->label = label; 125 - } 126 - 127 - /* 128 - * Return the GPIO number of the passed descriptor relative to its chip 129 - */ 130 - static int gpio_chip_hwgpio(const struct gpio_desc *desc) 131 - { 132 - return desc - &desc->chip->desc[0]; 133 60 } 134 61 135 62 /** ··· 198 271 return status; 199 272 } 200 273 EXPORT_SYMBOL_GPL(gpiod_get_direction); 201 - 202 - #ifdef CONFIG_GPIO_SYSFS 203 - 204 - /* lock protects against unexport_gpio() being called while 205 - * sysfs files are active. 206 - */ 207 - static DEFINE_MUTEX(sysfs_lock); 208 - 209 - /* 210 - * /sys/class/gpio/gpioN... only for GPIOs that are exported 211 - * /direction 212 - * * MAY BE OMITTED if kernel won't allow direction changes 213 - * * is read/write as "in" or "out" 214 - * * may also be written as "high" or "low", initializing 215 - * output value as specified ("out" implies "low") 216 - * /value 217 - * * always readable, subject to hardware behavior 218 - * * may be writable, as zero/nonzero 219 - * /edge 220 - * * configures behavior of poll(2) on /value 221 - * * available only if pin can generate IRQs on input 222 - * * is read/write as "none", "falling", "rising", or "both" 223 - * /active_low 224 - * * configures polarity of /value 225 - * * is read/write as zero/nonzero 226 - * * also affects existing and subsequent "falling" and "rising" 227 - * /edge configuration 228 - */ 229 - 230 - static ssize_t gpio_direction_show(struct device *dev, 231 - struct device_attribute *attr, char *buf) 232 - { 233 - const struct gpio_desc *desc = dev_get_drvdata(dev); 234 - ssize_t status; 235 - 236 - mutex_lock(&sysfs_lock); 237 - 238 - if (!test_bit(FLAG_EXPORT, &desc->flags)) { 239 - status = -EIO; 240 - } else { 241 - gpiod_get_direction(desc); 242 - status = sprintf(buf, "%s\n", 243 - test_bit(FLAG_IS_OUT, &desc->flags) 244 - ? "out" : "in"); 245 - } 246 - 247 - mutex_unlock(&sysfs_lock); 248 - return status; 249 - } 250 - 251 - static ssize_t gpio_direction_store(struct device *dev, 252 - struct device_attribute *attr, const char *buf, size_t size) 253 - { 254 - struct gpio_desc *desc = dev_get_drvdata(dev); 255 - ssize_t status; 256 - 257 - mutex_lock(&sysfs_lock); 258 - 259 - if (!test_bit(FLAG_EXPORT, &desc->flags)) 260 - status = -EIO; 261 - else if (sysfs_streq(buf, "high")) 262 - status = gpiod_direction_output_raw(desc, 1); 263 - else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low")) 264 - status = gpiod_direction_output_raw(desc, 0); 265 - else if (sysfs_streq(buf, "in")) 266 - status = gpiod_direction_input(desc); 267 - else 268 - status = -EINVAL; 269 - 270 - mutex_unlock(&sysfs_lock); 271 - return status ? : size; 272 - } 273 - 274 - static /* const */ DEVICE_ATTR(direction, 0644, 275 - gpio_direction_show, gpio_direction_store); 276 - 277 - static ssize_t gpio_value_show(struct device *dev, 278 - struct device_attribute *attr, char *buf) 279 - { 280 - struct gpio_desc *desc = dev_get_drvdata(dev); 281 - ssize_t status; 282 - 283 - mutex_lock(&sysfs_lock); 284 - 285 - if (!test_bit(FLAG_EXPORT, &desc->flags)) 286 - status = -EIO; 287 - else 288 - status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc)); 289 - 290 - mutex_unlock(&sysfs_lock); 291 - return status; 292 - } 293 - 294 - static ssize_t gpio_value_store(struct device *dev, 295 - struct device_attribute *attr, const char *buf, size_t size) 296 - { 297 - struct gpio_desc *desc = dev_get_drvdata(dev); 298 - ssize_t status; 299 - 300 - mutex_lock(&sysfs_lock); 301 - 302 - if (!test_bit(FLAG_EXPORT, &desc->flags)) 303 - status = -EIO; 304 - else if (!test_bit(FLAG_IS_OUT, &desc->flags)) 305 - status = -EPERM; 306 - else { 307 - long value; 308 - 309 - status = kstrtol(buf, 0, &value); 310 - if (status == 0) { 311 - gpiod_set_value_cansleep(desc, value); 312 - status = size; 313 - } 314 - } 315 - 316 - mutex_unlock(&sysfs_lock); 317 - return status; 318 - } 319 - 320 - static const DEVICE_ATTR(value, 0644, 321 - gpio_value_show, gpio_value_store); 322 - 323 - static irqreturn_t gpio_sysfs_irq(int irq, void *priv) 324 - { 325 - struct kernfs_node *value_sd = priv; 326 - 327 - sysfs_notify_dirent(value_sd); 328 - return IRQ_HANDLED; 329 - } 330 - 331 - static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev, 332 - unsigned long gpio_flags) 333 - { 334 - struct kernfs_node *value_sd; 335 - unsigned long irq_flags; 336 - int ret, irq, id; 337 - 338 - if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags) 339 - return 0; 340 - 341 - irq = gpiod_to_irq(desc); 342 - if (irq < 0) 343 - return -EIO; 344 - 345 - id = desc->flags >> ID_SHIFT; 346 - value_sd = idr_find(&dirent_idr, id); 347 - if (value_sd) 348 - free_irq(irq, value_sd); 349 - 350 - desc->flags &= ~GPIO_TRIGGER_MASK; 351 - 352 - if (!gpio_flags) { 353 - gpiod_unlock_as_irq(desc); 354 - ret = 0; 355 - goto free_id; 356 - } 357 - 358 - irq_flags = IRQF_SHARED; 359 - if (test_bit(FLAG_TRIG_FALL, &gpio_flags)) 360 - irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? 361 - IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; 362 - if (test_bit(FLAG_TRIG_RISE, &gpio_flags)) 363 - irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? 364 - IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; 365 - 366 - if (!value_sd) { 367 - value_sd = sysfs_get_dirent(dev->kobj.sd, "value"); 368 - if (!value_sd) { 369 - ret = -ENODEV; 370 - goto err_out; 371 - } 372 - 373 - ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL); 374 - if (ret < 0) 375 - goto free_sd; 376 - id = ret; 377 - 378 - desc->flags &= GPIO_FLAGS_MASK; 379 - desc->flags |= (unsigned long)id << ID_SHIFT; 380 - 381 - if (desc->flags >> ID_SHIFT != id) { 382 - ret = -ERANGE; 383 - goto free_id; 384 - } 385 - } 386 - 387 - ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags, 388 - "gpiolib", value_sd); 389 - if (ret < 0) 390 - goto free_id; 391 - 392 - ret = gpiod_lock_as_irq(desc); 393 - if (ret < 0) { 394 - gpiod_warn(desc, "failed to flag the GPIO for IRQ\n"); 395 - goto free_id; 396 - } 397 - 398 - desc->flags |= gpio_flags; 399 - return 0; 400 - 401 - free_id: 402 - idr_remove(&dirent_idr, id); 403 - desc->flags &= GPIO_FLAGS_MASK; 404 - free_sd: 405 - if (value_sd) 406 - sysfs_put(value_sd); 407 - err_out: 408 - return ret; 409 - } 410 - 411 - static const struct { 412 - const char *name; 413 - unsigned long flags; 414 - } trigger_types[] = { 415 - { "none", 0 }, 416 - { "falling", BIT(FLAG_TRIG_FALL) }, 417 - { "rising", BIT(FLAG_TRIG_RISE) }, 418 - { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) }, 419 - }; 420 - 421 - static ssize_t gpio_edge_show(struct device *dev, 422 - struct device_attribute *attr, char *buf) 423 - { 424 - const struct gpio_desc *desc = dev_get_drvdata(dev); 425 - ssize_t status; 426 - 427 - mutex_lock(&sysfs_lock); 428 - 429 - if (!test_bit(FLAG_EXPORT, &desc->flags)) 430 - status = -EIO; 431 - else { 432 - int i; 433 - 434 - status = 0; 435 - for (i = 0; i < ARRAY_SIZE(trigger_types); i++) 436 - if ((desc->flags & GPIO_TRIGGER_MASK) 437 - == trigger_types[i].flags) { 438 - status = sprintf(buf, "%s\n", 439 - trigger_types[i].name); 440 - break; 441 - } 442 - } 443 - 444 - mutex_unlock(&sysfs_lock); 445 - return status; 446 - } 447 - 448 - static ssize_t gpio_edge_store(struct device *dev, 449 - struct device_attribute *attr, const char *buf, size_t size) 450 - { 451 - struct gpio_desc *desc = dev_get_drvdata(dev); 452 - ssize_t status; 453 - int i; 454 - 455 - for (i = 0; i < ARRAY_SIZE(trigger_types); i++) 456 - if (sysfs_streq(trigger_types[i].name, buf)) 457 - goto found; 458 - return -EINVAL; 459 - 460 - found: 461 - mutex_lock(&sysfs_lock); 462 - 463 - if (!test_bit(FLAG_EXPORT, &desc->flags)) 464 - status = -EIO; 465 - else { 466 - status = gpio_setup_irq(desc, dev, trigger_types[i].flags); 467 - if (!status) 468 - status = size; 469 - } 470 - 471 - mutex_unlock(&sysfs_lock); 472 - 473 - return status; 474 - } 475 - 476 - static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store); 477 - 478 - static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev, 479 - int value) 480 - { 481 - int status = 0; 482 - 483 - if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value) 484 - return 0; 485 - 486 - if (value) 487 - set_bit(FLAG_ACTIVE_LOW, &desc->flags); 488 - else 489 - clear_bit(FLAG_ACTIVE_LOW, &desc->flags); 490 - 491 - /* reconfigure poll(2) support if enabled on one edge only */ 492 - if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^ 493 - !!test_bit(FLAG_TRIG_FALL, &desc->flags))) { 494 - unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK; 495 - 496 - gpio_setup_irq(desc, dev, 0); 497 - status = gpio_setup_irq(desc, dev, trigger_flags); 498 - } 499 - 500 - return status; 501 - } 502 - 503 - static ssize_t gpio_active_low_show(struct device *dev, 504 - struct device_attribute *attr, char *buf) 505 - { 506 - const struct gpio_desc *desc = dev_get_drvdata(dev); 507 - ssize_t status; 508 - 509 - mutex_lock(&sysfs_lock); 510 - 511 - if (!test_bit(FLAG_EXPORT, &desc->flags)) 512 - status = -EIO; 513 - else 514 - status = sprintf(buf, "%d\n", 515 - !!test_bit(FLAG_ACTIVE_LOW, &desc->flags)); 516 - 517 - mutex_unlock(&sysfs_lock); 518 - 519 - return status; 520 - } 521 - 522 - static ssize_t gpio_active_low_store(struct device *dev, 523 - struct device_attribute *attr, const char *buf, size_t size) 524 - { 525 - struct gpio_desc *desc = dev_get_drvdata(dev); 526 - ssize_t status; 527 - 528 - mutex_lock(&sysfs_lock); 529 - 530 - if (!test_bit(FLAG_EXPORT, &desc->flags)) { 531 - status = -EIO; 532 - } else { 533 - long value; 534 - 535 - status = kstrtol(buf, 0, &value); 536 - if (status == 0) 537 - status = sysfs_set_active_low(desc, dev, value != 0); 538 - } 539 - 540 - mutex_unlock(&sysfs_lock); 541 - 542 - return status ? : size; 543 - } 544 - 545 - static const DEVICE_ATTR(active_low, 0644, 546 - gpio_active_low_show, gpio_active_low_store); 547 - 548 - static const struct attribute *gpio_attrs[] = { 549 - &dev_attr_value.attr, 550 - &dev_attr_active_low.attr, 551 - NULL, 552 - }; 553 - 554 - static const struct attribute_group gpio_attr_group = { 555 - .attrs = (struct attribute **) gpio_attrs, 556 - }; 557 - 558 - /* 559 - * /sys/class/gpio/gpiochipN/ 560 - * /base ... matching gpio_chip.base (N) 561 - * /label ... matching gpio_chip.label 562 - * /ngpio ... matching gpio_chip.ngpio 563 - */ 564 - 565 - static ssize_t chip_base_show(struct device *dev, 566 - struct device_attribute *attr, char *buf) 567 - { 568 - const struct gpio_chip *chip = dev_get_drvdata(dev); 569 - 570 - return sprintf(buf, "%d\n", chip->base); 571 - } 572 - static DEVICE_ATTR(base, 0444, chip_base_show, NULL); 573 - 574 - static ssize_t chip_label_show(struct device *dev, 575 - struct device_attribute *attr, char *buf) 576 - { 577 - const struct gpio_chip *chip = dev_get_drvdata(dev); 578 - 579 - return sprintf(buf, "%s\n", chip->label ? : ""); 580 - } 581 - static DEVICE_ATTR(label, 0444, chip_label_show, NULL); 582 - 583 - static ssize_t chip_ngpio_show(struct device *dev, 584 - struct device_attribute *attr, char *buf) 585 - { 586 - const struct gpio_chip *chip = dev_get_drvdata(dev); 587 - 588 - return sprintf(buf, "%u\n", chip->ngpio); 589 - } 590 - static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL); 591 - 592 - static const struct attribute *gpiochip_attrs[] = { 593 - &dev_attr_base.attr, 594 - &dev_attr_label.attr, 595 - &dev_attr_ngpio.attr, 596 - NULL, 597 - }; 598 - 599 - static const struct attribute_group gpiochip_attr_group = { 600 - .attrs = (struct attribute **) gpiochip_attrs, 601 - }; 602 - 603 - /* 604 - * /sys/class/gpio/export ... write-only 605 - * integer N ... number of GPIO to export (full access) 606 - * /sys/class/gpio/unexport ... write-only 607 - * integer N ... number of GPIO to unexport 608 - */ 609 - static ssize_t export_store(struct class *class, 610 - struct class_attribute *attr, 611 - const char *buf, size_t len) 612 - { 613 - long gpio; 614 - struct gpio_desc *desc; 615 - int status; 616 - 617 - status = kstrtol(buf, 0, &gpio); 618 - if (status < 0) 619 - goto done; 620 - 621 - desc = gpio_to_desc(gpio); 622 - /* reject invalid GPIOs */ 623 - if (!desc) { 624 - pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); 625 - return -EINVAL; 626 - } 627 - 628 - /* No extra locking here; FLAG_SYSFS just signifies that the 629 - * request and export were done by on behalf of userspace, so 630 - * they may be undone on its behalf too. 631 - */ 632 - 633 - status = gpiod_request(desc, "sysfs"); 634 - if (status < 0) { 635 - if (status == -EPROBE_DEFER) 636 - status = -ENODEV; 637 - goto done; 638 - } 639 - status = gpiod_export(desc, true); 640 - if (status < 0) 641 - gpiod_free(desc); 642 - else 643 - set_bit(FLAG_SYSFS, &desc->flags); 644 - 645 - done: 646 - if (status) 647 - pr_debug("%s: status %d\n", __func__, status); 648 - return status ? : len; 649 - } 650 - 651 - static ssize_t unexport_store(struct class *class, 652 - struct class_attribute *attr, 653 - const char *buf, size_t len) 654 - { 655 - long gpio; 656 - struct gpio_desc *desc; 657 - int status; 658 - 659 - status = kstrtol(buf, 0, &gpio); 660 - if (status < 0) 661 - goto done; 662 - 663 - desc = gpio_to_desc(gpio); 664 - /* reject bogus commands (gpio_unexport ignores them) */ 665 - if (!desc) { 666 - pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); 667 - return -EINVAL; 668 - } 669 - 670 - status = -EINVAL; 671 - 672 - /* No extra locking here; FLAG_SYSFS just signifies that the 673 - * request and export were done by on behalf of userspace, so 674 - * they may be undone on its behalf too. 675 - */ 676 - if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) { 677 - status = 0; 678 - gpiod_free(desc); 679 - } 680 - done: 681 - if (status) 682 - pr_debug("%s: status %d\n", __func__, status); 683 - return status ? : len; 684 - } 685 - 686 - static struct class_attribute gpio_class_attrs[] = { 687 - __ATTR(export, 0200, NULL, export_store), 688 - __ATTR(unexport, 0200, NULL, unexport_store), 689 - __ATTR_NULL, 690 - }; 691 - 692 - static struct class gpio_class = { 693 - .name = "gpio", 694 - .owner = THIS_MODULE, 695 - 696 - .class_attrs = gpio_class_attrs, 697 - }; 698 - 699 - 700 - /** 701 - * gpiod_export - export a GPIO through sysfs 702 - * @gpio: gpio to make available, already requested 703 - * @direction_may_change: true if userspace may change gpio direction 704 - * Context: arch_initcall or later 705 - * 706 - * When drivers want to make a GPIO accessible to userspace after they 707 - * have requested it -- perhaps while debugging, or as part of their 708 - * public interface -- they may use this routine. If the GPIO can 709 - * change direction (some can't) and the caller allows it, userspace 710 - * will see "direction" sysfs attribute which may be used to change 711 - * the gpio's direction. A "value" attribute will always be provided. 712 - * 713 - * Returns zero on success, else an error. 714 - */ 715 - int gpiod_export(struct gpio_desc *desc, bool direction_may_change) 716 - { 717 - unsigned long flags; 718 - int status; 719 - const char *ioname = NULL; 720 - struct device *dev; 721 - int offset; 722 - 723 - /* can't export until sysfs is available ... */ 724 - if (!gpio_class.p) { 725 - pr_debug("%s: called too early!\n", __func__); 726 - return -ENOENT; 727 - } 728 - 729 - if (!desc) { 730 - pr_debug("%s: invalid gpio descriptor\n", __func__); 731 - return -EINVAL; 732 - } 733 - 734 - mutex_lock(&sysfs_lock); 735 - 736 - spin_lock_irqsave(&gpio_lock, flags); 737 - if (!test_bit(FLAG_REQUESTED, &desc->flags) || 738 - test_bit(FLAG_EXPORT, &desc->flags)) { 739 - spin_unlock_irqrestore(&gpio_lock, flags); 740 - gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n", 741 - __func__, 742 - test_bit(FLAG_REQUESTED, &desc->flags), 743 - test_bit(FLAG_EXPORT, &desc->flags)); 744 - status = -EPERM; 745 - goto fail_unlock; 746 - } 747 - 748 - if (!desc->chip->direction_input || !desc->chip->direction_output) 749 - direction_may_change = false; 750 - spin_unlock_irqrestore(&gpio_lock, flags); 751 - 752 - offset = gpio_chip_hwgpio(desc); 753 - if (desc->chip->names && desc->chip->names[offset]) 754 - ioname = desc->chip->names[offset]; 755 - 756 - dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0), 757 - desc, ioname ? ioname : "gpio%u", 758 - desc_to_gpio(desc)); 759 - if (IS_ERR(dev)) { 760 - status = PTR_ERR(dev); 761 - goto fail_unlock; 762 - } 763 - 764 - status = sysfs_create_group(&dev->kobj, &gpio_attr_group); 765 - if (status) 766 - goto fail_unregister_device; 767 - 768 - if (direction_may_change) { 769 - status = device_create_file(dev, &dev_attr_direction); 770 - if (status) 771 - goto fail_unregister_device; 772 - } 773 - 774 - if (gpiod_to_irq(desc) >= 0 && (direction_may_change || 775 - !test_bit(FLAG_IS_OUT, &desc->flags))) { 776 - status = device_create_file(dev, &dev_attr_edge); 777 - if (status) 778 - goto fail_unregister_device; 779 - } 780 - 781 - set_bit(FLAG_EXPORT, &desc->flags); 782 - mutex_unlock(&sysfs_lock); 783 - return 0; 784 - 785 - fail_unregister_device: 786 - device_unregister(dev); 787 - fail_unlock: 788 - mutex_unlock(&sysfs_lock); 789 - gpiod_dbg(desc, "%s: status %d\n", __func__, status); 790 - return status; 791 - } 792 - EXPORT_SYMBOL_GPL(gpiod_export); 793 - 794 - static int match_export(struct device *dev, const void *data) 795 - { 796 - return dev_get_drvdata(dev) == data; 797 - } 798 - 799 - /** 800 - * gpiod_export_link - create a sysfs link to an exported GPIO node 801 - * @dev: device under which to create symlink 802 - * @name: name of the symlink 803 - * @gpio: gpio to create symlink to, already exported 804 - * 805 - * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN 806 - * node. Caller is responsible for unlinking. 807 - * 808 - * Returns zero on success, else an error. 809 - */ 810 - int gpiod_export_link(struct device *dev, const char *name, 811 - struct gpio_desc *desc) 812 - { 813 - int status = -EINVAL; 814 - 815 - if (!desc) { 816 - pr_warn("%s: invalid GPIO\n", __func__); 817 - return -EINVAL; 818 - } 819 - 820 - mutex_lock(&sysfs_lock); 821 - 822 - if (test_bit(FLAG_EXPORT, &desc->flags)) { 823 - struct device *tdev; 824 - 825 - tdev = class_find_device(&gpio_class, NULL, desc, match_export); 826 - if (tdev != NULL) { 827 - status = sysfs_create_link(&dev->kobj, &tdev->kobj, 828 - name); 829 - } else { 830 - status = -ENODEV; 831 - } 832 - } 833 - 834 - mutex_unlock(&sysfs_lock); 835 - 836 - if (status) 837 - gpiod_dbg(desc, "%s: status %d\n", __func__, status); 838 - 839 - return status; 840 - } 841 - EXPORT_SYMBOL_GPL(gpiod_export_link); 842 - 843 - /** 844 - * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value 845 - * @gpio: gpio to change 846 - * @value: non-zero to use active low, i.e. inverted values 847 - * 848 - * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute. 849 - * The GPIO does not have to be exported yet. If poll(2) support has 850 - * been enabled for either rising or falling edge, it will be 851 - * reconfigured to follow the new polarity. 852 - * 853 - * Returns zero on success, else an error. 854 - */ 855 - int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) 856 - { 857 - struct device *dev = NULL; 858 - int status = -EINVAL; 859 - 860 - if (!desc) { 861 - pr_warn("%s: invalid GPIO\n", __func__); 862 - return -EINVAL; 863 - } 864 - 865 - mutex_lock(&sysfs_lock); 866 - 867 - if (test_bit(FLAG_EXPORT, &desc->flags)) { 868 - dev = class_find_device(&gpio_class, NULL, desc, match_export); 869 - if (dev == NULL) { 870 - status = -ENODEV; 871 - goto unlock; 872 - } 873 - } 874 - 875 - status = sysfs_set_active_low(desc, dev, value); 876 - 877 - unlock: 878 - mutex_unlock(&sysfs_lock); 879 - 880 - if (status) 881 - gpiod_dbg(desc, "%s: status %d\n", __func__, status); 882 - 883 - return status; 884 - } 885 - EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low); 886 - 887 - /** 888 - * gpiod_unexport - reverse effect of gpio_export() 889 - * @gpio: gpio to make unavailable 890 - * 891 - * This is implicit on gpio_free(). 892 - */ 893 - void gpiod_unexport(struct gpio_desc *desc) 894 - { 895 - int status = 0; 896 - struct device *dev = NULL; 897 - 898 - if (!desc) { 899 - pr_warn("%s: invalid GPIO\n", __func__); 900 - return; 901 - } 902 - 903 - mutex_lock(&sysfs_lock); 904 - 905 - if (test_bit(FLAG_EXPORT, &desc->flags)) { 906 - 907 - dev = class_find_device(&gpio_class, NULL, desc, match_export); 908 - if (dev) { 909 - gpio_setup_irq(desc, dev, 0); 910 - clear_bit(FLAG_EXPORT, &desc->flags); 911 - } else 912 - status = -ENODEV; 913 - } 914 - 915 - mutex_unlock(&sysfs_lock); 916 - 917 - if (dev) { 918 - device_unregister(dev); 919 - put_device(dev); 920 - } 921 - 922 - if (status) 923 - gpiod_dbg(desc, "%s: status %d\n", __func__, status); 924 - } 925 - EXPORT_SYMBOL_GPL(gpiod_unexport); 926 - 927 - static int gpiochip_export(struct gpio_chip *chip) 928 - { 929 - int status; 930 - struct device *dev; 931 - 932 - /* Many systems register gpio chips for SOC support very early, 933 - * before driver model support is available. In those cases we 934 - * export this later, in gpiolib_sysfs_init() ... here we just 935 - * verify that _some_ field of gpio_class got initialized. 936 - */ 937 - if (!gpio_class.p) 938 - return 0; 939 - 940 - /* use chip->base for the ID; it's already known to be unique */ 941 - mutex_lock(&sysfs_lock); 942 - dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip, 943 - "gpiochip%d", chip->base); 944 - if (!IS_ERR(dev)) { 945 - status = sysfs_create_group(&dev->kobj, 946 - &gpiochip_attr_group); 947 - } else 948 - status = PTR_ERR(dev); 949 - chip->exported = (status == 0); 950 - mutex_unlock(&sysfs_lock); 951 - 952 - if (status) { 953 - unsigned long flags; 954 - unsigned gpio; 955 - 956 - spin_lock_irqsave(&gpio_lock, flags); 957 - gpio = 0; 958 - while (gpio < chip->ngpio) 959 - chip->desc[gpio++].chip = NULL; 960 - spin_unlock_irqrestore(&gpio_lock, flags); 961 - 962 - chip_dbg(chip, "%s: status %d\n", __func__, status); 963 - } 964 - 965 - return status; 966 - } 967 - 968 - static void gpiochip_unexport(struct gpio_chip *chip) 969 - { 970 - int status; 971 - struct device *dev; 972 - 973 - mutex_lock(&sysfs_lock); 974 - dev = class_find_device(&gpio_class, NULL, chip, match_export); 975 - if (dev) { 976 - put_device(dev); 977 - device_unregister(dev); 978 - chip->exported = false; 979 - status = 0; 980 - } else 981 - status = -ENODEV; 982 - mutex_unlock(&sysfs_lock); 983 - 984 - if (status) 985 - chip_dbg(chip, "%s: status %d\n", __func__, status); 986 - } 987 - 988 - static int __init gpiolib_sysfs_init(void) 989 - { 990 - int status; 991 - unsigned long flags; 992 - struct gpio_chip *chip; 993 - 994 - status = class_register(&gpio_class); 995 - if (status < 0) 996 - return status; 997 - 998 - /* Scan and register the gpio_chips which registered very 999 - * early (e.g. before the class_register above was called). 1000 - * 1001 - * We run before arch_initcall() so chip->dev nodes can have 1002 - * registered, and so arch_initcall() can always gpio_export(). 1003 - */ 1004 - spin_lock_irqsave(&gpio_lock, flags); 1005 - list_for_each_entry(chip, &gpio_chips, list) { 1006 - if (!chip || chip->exported) 1007 - continue; 1008 - 1009 - spin_unlock_irqrestore(&gpio_lock, flags); 1010 - status = gpiochip_export(chip); 1011 - spin_lock_irqsave(&gpio_lock, flags); 1012 - } 1013 - spin_unlock_irqrestore(&gpio_lock, flags); 1014 - 1015 - 1016 - return status; 1017 - } 1018 - postcore_initcall(gpiolib_sysfs_init); 1019 - 1020 - #else 1021 - static inline int gpiochip_export(struct gpio_chip *chip) 1022 - { 1023 - return 0; 1024 - } 1025 - 1026 - static inline void gpiochip_unexport(struct gpio_chip *chip) 1027 - { 1028 - } 1029 - 1030 - #endif /* CONFIG_GPIO_SYSFS */ 1031 274 1032 275 /* 1033 276 * Add a new chip to the global chips list, keeping the list of chips sorted ··· 818 1721 return status; 819 1722 } 820 1723 821 - static int gpiod_request(struct gpio_desc *desc, const char *label) 1724 + int gpiod_request(struct gpio_desc *desc, const char *label) 822 1725 { 823 1726 int status = -EPROBE_DEFER; 824 1727 struct gpio_chip *chip; ··· 883 1786 return ret; 884 1787 } 885 1788 886 - static void gpiod_free(struct gpio_desc *desc) 1789 + void gpiod_free(struct gpio_desc *desc) 887 1790 { 888 1791 if (desc && __gpiod_free(desc)) 889 1792 module_put(desc->chip->owner);
+91
drivers/gpio/gpiolib.h
··· 51 51 struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, 52 52 const char *list_name, int index, enum of_gpio_flags *flags); 53 53 54 + extern struct spinlock gpio_lock; 55 + extern struct list_head gpio_chips; 56 + 57 + struct gpio_desc { 58 + struct gpio_chip *chip; 59 + unsigned long flags; 60 + /* flag symbols are bit numbers */ 61 + #define FLAG_REQUESTED 0 62 + #define FLAG_IS_OUT 1 63 + #define FLAG_EXPORT 2 /* protected by sysfs_lock */ 64 + #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */ 65 + #define FLAG_TRIG_FALL 4 /* trigger on falling edge */ 66 + #define FLAG_TRIG_RISE 5 /* trigger on rising edge */ 67 + #define FLAG_ACTIVE_LOW 6 /* value has active low */ 68 + #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ 69 + #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ 70 + #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ 71 + 72 + #define ID_SHIFT 16 /* add new flags before this one */ 73 + 74 + #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1) 75 + #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE)) 76 + 77 + const char *label; 78 + }; 79 + 80 + int gpiod_request(struct gpio_desc *desc, const char *label); 81 + void gpiod_free(struct gpio_desc *desc); 82 + 83 + /* 84 + * Return the GPIO number of the passed descriptor relative to its chip 85 + */ 86 + static int __maybe_unused gpio_chip_hwgpio(const struct gpio_desc *desc) 87 + { 88 + return desc - &desc->chip->desc[0]; 89 + } 90 + 91 + /* With descriptor prefix */ 92 + 93 + #define gpiod_emerg(desc, fmt, ...) \ 94 + pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ 95 + ##__VA_ARGS__) 96 + #define gpiod_crit(desc, fmt, ...) \ 97 + pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 98 + ##__VA_ARGS__) 99 + #define gpiod_err(desc, fmt, ...) \ 100 + pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 101 + ##__VA_ARGS__) 102 + #define gpiod_warn(desc, fmt, ...) \ 103 + pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 104 + ##__VA_ARGS__) 105 + #define gpiod_info(desc, fmt, ...) \ 106 + pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 107 + ##__VA_ARGS__) 108 + #define gpiod_dbg(desc, fmt, ...) \ 109 + pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ 110 + ##__VA_ARGS__) 111 + 112 + /* With chip prefix */ 113 + 114 + #define chip_emerg(chip, fmt, ...) \ 115 + pr_emerg("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) 116 + #define chip_crit(chip, fmt, ...) \ 117 + pr_crit("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) 118 + #define chip_err(chip, fmt, ...) \ 119 + pr_err("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) 120 + #define chip_warn(chip, fmt, ...) \ 121 + pr_warn("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) 122 + #define chip_info(chip, fmt, ...) \ 123 + pr_info("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) 124 + #define chip_dbg(chip, fmt, ...) \ 125 + pr_debug("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) 126 + 127 + #ifdef CONFIG_GPIO_SYSFS 128 + 129 + int gpiochip_export(struct gpio_chip *chip); 130 + void gpiochip_unexport(struct gpio_chip *chip); 131 + 132 + #else 133 + 134 + static inline int gpiochip_export(struct gpio_chip *chip) 135 + { 136 + return 0; 137 + } 138 + 139 + static inline void gpiochip_unexport(struct gpio_chip *chip) 140 + { 141 + } 142 + 143 + #endif /* CONFIG_GPIO_SYSFS */ 144 + 54 145 #endif /* GPIOLIB_H */