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

gpiolib: allow poll() on value

Many gpio chips allow to generate interrupts when the value of a pin
changes. This patch gives usermode application the opportunity to make
use of this feature by calling poll(2) on the /sys/class/gpio/gpioN/value
sysfs file. The edge to trigger can be set in the edge file in the same
directory. Possible values are "none", "rising", "falling", and "both".

Using level triggers is not possible with current sysfs since nothing
changes the GPIO value (and the IRQ keeps triggering). Edge triggering
will "just work". Note that if there was an event between read() and
poll(), the poll() returns immediately.

Also note that this version only supports true GPIO interrupts. Some
later patch might be able to synthesize this behavior by timer-driven
polling; some systems seem to need that.

[dbrownell@users.sourceforge.net: align ids to 16 bit ids; whitespace]
Signed-off-by: Daniel Glöckner <dg@emlix.com>
Signed-off-by: David Brownell <dbrownell@users.sourceforge.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Daniel Glöckner and committed by
Linus Torvalds
ff77c352 d120c17f

+211 -5
+1
Documentation/ABI/testing/sysfs-gpio
··· 19 19 /gpioN ... for each exported GPIO #N 20 20 /value ... always readable, writes fail for input GPIOs 21 21 /direction ... r/w as: in, out (default low); write: high, low 22 + /edge ... r/w as: none, falling, rising, both 22 23 /gpiochipN ... for each gpiochip; #N is its first GPIO 23 24 /base ... (r/o) same as N 24 25 /label ... (r/o) descriptive, not necessarily unique
+7
Documentation/gpio.txt
··· 524 524 is configured as an output, this value may be written; 525 525 any nonzero value is treated as high. 526 526 527 + "edge" ... reads as either "none", "rising", "falling", or 528 + "both". Write these strings to select the signal edge(s) 529 + that will make poll(2) on the "value" file return. 530 + 531 + This file exists only if the pin can be configured as an 532 + interrupt generating input pin. 533 + 527 534 GPIO controllers have paths like /sys/class/gpio/chipchip42/ (for the 528 535 controller implementing GPIOs starting at #42) and have the following 529 536 read-only attributes:
+203 -5
drivers/gpio/gpiolib.c
··· 1 1 #include <linux/kernel.h> 2 2 #include <linux/module.h> 3 + #include <linux/interrupt.h> 3 4 #include <linux/irq.h> 4 5 #include <linux/spinlock.h> 5 6 #include <linux/device.h> ··· 8 7 #include <linux/debugfs.h> 9 8 #include <linux/seq_file.h> 10 9 #include <linux/gpio.h> 10 + #include <linux/idr.h> 11 11 12 12 13 13 /* Optional implementation infrastructure for GPIO interfaces. ··· 51 49 #define FLAG_RESERVED 2 52 50 #define FLAG_EXPORT 3 /* protected by sysfs_lock */ 53 51 #define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */ 52 + #define FLAG_TRIG_FALL 5 /* trigger on falling edge */ 53 + #define FLAG_TRIG_RISE 6 /* trigger on rising edge */ 54 + 55 + #define PDESC_ID_SHIFT 16 /* add new flags before this one */ 56 + 57 + #define GPIO_FLAGS_MASK ((1 << PDESC_ID_SHIFT) - 1) 58 + #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE)) 54 59 55 60 #ifdef CONFIG_DEBUG_FS 56 61 const char *label; 57 62 #endif 58 63 }; 59 64 static struct gpio_desc gpio_desc[ARCH_NR_GPIOS]; 65 + 66 + #ifdef CONFIG_GPIO_SYSFS 67 + struct poll_desc { 68 + struct work_struct work; 69 + struct sysfs_dirent *value_sd; 70 + }; 71 + 72 + static struct idr pdesc_idr; 73 + #endif 60 74 61 75 static inline void desc_set_label(struct gpio_desc *d, const char *label) 62 76 { ··· 206 188 * /value 207 189 * * always readable, subject to hardware behavior 208 190 * * may be writable, as zero/nonzero 209 - * 210 - * REVISIT there will likely be an attribute for configuring async 211 - * notifications, e.g. to specify polling interval or IRQ trigger type 212 - * that would for example trigger a poll() on the "value". 191 + * /edge 192 + * * configures behavior of poll(2) on /value 193 + * * available only if pin can generate IRQs on input 194 + * * is read/write as "none", "falling", "rising", or "both" 213 195 */ 214 196 215 197 static ssize_t gpio_direction_show(struct device *dev, ··· 305 287 306 288 static /*const*/ DEVICE_ATTR(value, 0644, 307 289 gpio_value_show, gpio_value_store); 290 + 291 + static irqreturn_t gpio_sysfs_irq(int irq, void *priv) 292 + { 293 + struct work_struct *work = priv; 294 + 295 + schedule_work(work); 296 + return IRQ_HANDLED; 297 + } 298 + 299 + static void gpio_notify_sysfs(struct work_struct *work) 300 + { 301 + struct poll_desc *pdesc; 302 + 303 + pdesc = container_of(work, struct poll_desc, work); 304 + sysfs_notify_dirent(pdesc->value_sd); 305 + } 306 + 307 + static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev, 308 + unsigned long gpio_flags) 309 + { 310 + struct poll_desc *pdesc; 311 + unsigned long irq_flags; 312 + int ret, irq, id; 313 + 314 + if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags) 315 + return 0; 316 + 317 + irq = gpio_to_irq(desc - gpio_desc); 318 + if (irq < 0) 319 + return -EIO; 320 + 321 + id = desc->flags >> PDESC_ID_SHIFT; 322 + pdesc = idr_find(&pdesc_idr, id); 323 + if (pdesc) { 324 + free_irq(irq, &pdesc->work); 325 + cancel_work_sync(&pdesc->work); 326 + } 327 + 328 + desc->flags &= ~GPIO_TRIGGER_MASK; 329 + 330 + if (!gpio_flags) { 331 + ret = 0; 332 + goto free_sd; 333 + } 334 + 335 + irq_flags = IRQF_SHARED; 336 + if (test_bit(FLAG_TRIG_FALL, &gpio_flags)) 337 + irq_flags |= IRQF_TRIGGER_FALLING; 338 + if (test_bit(FLAG_TRIG_RISE, &gpio_flags)) 339 + irq_flags |= IRQF_TRIGGER_RISING; 340 + 341 + if (!pdesc) { 342 + pdesc = kmalloc(sizeof(*pdesc), GFP_KERNEL); 343 + if (!pdesc) { 344 + ret = -ENOMEM; 345 + goto err_out; 346 + } 347 + 348 + do { 349 + ret = -ENOMEM; 350 + if (idr_pre_get(&pdesc_idr, GFP_KERNEL)) 351 + ret = idr_get_new_above(&pdesc_idr, 352 + pdesc, 1, &id); 353 + } while (ret == -EAGAIN); 354 + 355 + if (ret) 356 + goto free_mem; 357 + 358 + desc->flags &= GPIO_FLAGS_MASK; 359 + desc->flags |= (unsigned long)id << PDESC_ID_SHIFT; 360 + 361 + if (desc->flags >> PDESC_ID_SHIFT != id) { 362 + ret = -ERANGE; 363 + goto free_id; 364 + } 365 + 366 + pdesc->value_sd = sysfs_get_dirent(dev->kobj.sd, "value"); 367 + if (!pdesc->value_sd) { 368 + ret = -ENODEV; 369 + goto free_id; 370 + } 371 + INIT_WORK(&pdesc->work, gpio_notify_sysfs); 372 + } 373 + 374 + ret = request_irq(irq, gpio_sysfs_irq, irq_flags, 375 + "gpiolib", &pdesc->work); 376 + if (ret) 377 + goto free_sd; 378 + 379 + desc->flags |= gpio_flags; 380 + return 0; 381 + 382 + free_sd: 383 + sysfs_put(pdesc->value_sd); 384 + free_id: 385 + idr_remove(&pdesc_idr, id); 386 + desc->flags &= GPIO_FLAGS_MASK; 387 + free_mem: 388 + kfree(pdesc); 389 + err_out: 390 + return ret; 391 + } 392 + 393 + static const struct { 394 + const char *name; 395 + unsigned long flags; 396 + } trigger_types[] = { 397 + { "none", 0 }, 398 + { "falling", BIT(FLAG_TRIG_FALL) }, 399 + { "rising", BIT(FLAG_TRIG_RISE) }, 400 + { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) }, 401 + }; 402 + 403 + static ssize_t gpio_edge_show(struct device *dev, 404 + struct device_attribute *attr, char *buf) 405 + { 406 + const struct gpio_desc *desc = dev_get_drvdata(dev); 407 + ssize_t status; 408 + 409 + mutex_lock(&sysfs_lock); 410 + 411 + if (!test_bit(FLAG_EXPORT, &desc->flags)) 412 + status = -EIO; 413 + else { 414 + int i; 415 + 416 + status = 0; 417 + for (i = 0; i < ARRAY_SIZE(trigger_types); i++) 418 + if ((desc->flags & GPIO_TRIGGER_MASK) 419 + == trigger_types[i].flags) { 420 + status = sprintf(buf, "%s\n", 421 + trigger_types[i].name); 422 + break; 423 + } 424 + } 425 + 426 + mutex_unlock(&sysfs_lock); 427 + return status; 428 + } 429 + 430 + static ssize_t gpio_edge_store(struct device *dev, 431 + struct device_attribute *attr, const char *buf, size_t size) 432 + { 433 + struct gpio_desc *desc = dev_get_drvdata(dev); 434 + ssize_t status; 435 + int i; 436 + 437 + for (i = 0; i < ARRAY_SIZE(trigger_types); i++) 438 + if (sysfs_streq(trigger_types[i].name, buf)) 439 + goto found; 440 + return -EINVAL; 441 + 442 + found: 443 + mutex_lock(&sysfs_lock); 444 + 445 + if (!test_bit(FLAG_EXPORT, &desc->flags)) 446 + status = -EIO; 447 + else { 448 + status = gpio_setup_irq(desc, dev, trigger_types[i].flags); 449 + if (!status) 450 + status = size; 451 + } 452 + 453 + mutex_unlock(&sysfs_lock); 454 + 455 + return status; 456 + } 457 + 458 + static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store); 308 459 309 460 static const struct attribute *gpio_attrs[] = { 310 461 &dev_attr_direction.attr, ··· 660 473 struct device *dev; 661 474 662 475 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0), 663 - desc, ioname ? ioname : "gpio%d", gpio); 476 + desc, ioname ? ioname : "gpio%d", gpio); 664 477 if (dev) { 665 478 if (direction_may_change) 666 479 status = sysfs_create_group(&dev->kobj, ··· 668 481 else 669 482 status = device_create_file(dev, 670 483 &dev_attr_value); 484 + 485 + if (!status && gpio_to_irq(gpio) >= 0 486 + && (direction_may_change 487 + || !test_bit(FLAG_IS_OUT, 488 + &desc->flags))) 489 + status = device_create_file(dev, 490 + &dev_attr_edge); 491 + 671 492 if (status != 0) 672 493 device_unregister(dev); 673 494 } else ··· 767 572 768 573 dev = class_find_device(&gpio_class, NULL, desc, match_export); 769 574 if (dev) { 575 + gpio_setup_irq(desc, dev, 0); 770 576 clear_bit(FLAG_EXPORT, &desc->flags); 771 577 put_device(dev); 772 578 device_unregister(dev); ··· 851 655 int status; 852 656 unsigned long flags; 853 657 unsigned gpio; 658 + 659 + idr_init(&pdesc_idr); 854 660 855 661 status = class_register(&gpio_class); 856 662 if (status < 0)