1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * GPIO Testing Device Driver 4 * 5 * Copyright (C) 2014 Kamlakant Patel <kamlakant.patel@broadcom.com> 6 * Copyright (C) 2015-2016 Bamvor Jian Zhang <bamv2005@gmail.com> 7 * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl> 8 */ 9 10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 11 12 #include <linux/cleanup.h> 13 #include <linux/debugfs.h> 14 #include <linux/device.h> 15 #include <linux/gpio/driver.h> 16 #include <linux/interrupt.h> 17 #include <linux/irq.h> 18 #include <linux/irq_sim.h> 19 #include <linux/irqdomain.h> 20 #include <linux/limits.h> 21 #include <linux/mod_devicetable.h> 22 #include <linux/module.h> 23 #include <linux/platform_device.h> 24 #include <linux/property.h> 25 #include <linux/seq_file.h> 26 #include <linux/slab.h> 27 #include <linux/string_helpers.h> 28 #include <linux/uaccess.h> 29 30 #define GPIO_MOCKUP_MAX_GC 10 31 /* 32 * We're storing two values per chip: the GPIO base and the number 33 * of GPIO lines. 34 */ 35 #define GPIO_MOCKUP_MAX_RANGES (GPIO_MOCKUP_MAX_GC * 2) 36 /* Maximum of four properties + the sentinel. */ 37 #define GPIO_MOCKUP_MAX_PROP 5 38 39 /* 40 * struct gpio_pin_status - structure describing a GPIO status 41 * @dir: Configures direction of gpio as "in" or "out" 42 * @value: Configures status of the gpio as 0(low) or 1(high) 43 * @pull: Configures the current pull of the GPIO as 0 (pull-down) or 44 * 1 (pull-up) 45 * @requested: Request status of this GPIO 46 */ 47 struct gpio_mockup_line_status { 48 int dir; 49 int value; 50 int pull; 51 bool requested; 52 }; 53 54 struct gpio_mockup_chip { 55 struct gpio_chip gc; 56 struct gpio_mockup_line_status *lines; 57 struct irq_domain *irq_sim_domain; 58 struct dentry *dbg_dir; 59 struct mutex lock; 60 }; 61 62 struct gpio_mockup_dbgfs_private { 63 struct gpio_mockup_chip *chip; 64 unsigned int offset; 65 }; 66 67 static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES]; 68 static int gpio_mockup_num_ranges; 69 module_param_array(gpio_mockup_ranges, int, &gpio_mockup_num_ranges, 0400); 70 71 static bool gpio_mockup_named_lines; 72 module_param_named(gpio_mockup_named_lines, 73 gpio_mockup_named_lines, bool, 0400); 74 75 static struct dentry *gpio_mockup_dbg_dir; 76 77 static int gpio_mockup_range_base(unsigned int index) 78 { 79 return gpio_mockup_ranges[index * 2]; 80 } 81 82 static int gpio_mockup_range_ngpio(unsigned int index) 83 { 84 return gpio_mockup_ranges[index * 2 + 1]; 85 } 86 87 static int __gpio_mockup_get(struct gpio_mockup_chip *chip, 88 unsigned int offset) 89 { 90 return chip->lines[offset].value; 91 } 92 93 static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset) 94 { 95 struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 96 int val; 97 98 scoped_guard(mutex, &chip->lock) 99 val = __gpio_mockup_get(chip, offset); 100 101 return val; 102 } 103 104 static int gpio_mockup_get_multiple(struct gpio_chip *gc, 105 unsigned long *mask, unsigned long *bits) 106 { 107 struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 108 unsigned int bit, val; 109 110 scoped_guard(mutex, &chip->lock) { 111 for_each_set_bit(bit, mask, gc->ngpio) { 112 val = __gpio_mockup_get(chip, bit); 113 __assign_bit(bit, bits, val); 114 } 115 } 116 117 return 0; 118 } 119 120 static void __gpio_mockup_set(struct gpio_mockup_chip *chip, 121 unsigned int offset, int value) 122 { 123 chip->lines[offset].value = !!value; 124 } 125 126 static int gpio_mockup_set(struct gpio_chip *gc, 127 unsigned int offset, int value) 128 { 129 struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 130 131 guard(mutex)(&chip->lock); 132 133 __gpio_mockup_set(chip, offset, value); 134 135 return 0; 136 } 137 138 static int gpio_mockup_set_multiple(struct gpio_chip *gc, 139 unsigned long *mask, unsigned long *bits) 140 { 141 struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 142 unsigned int bit; 143 144 guard(mutex)(&chip->lock); 145 146 for_each_set_bit(bit, mask, gc->ngpio) 147 __gpio_mockup_set(chip, bit, test_bit(bit, bits)); 148 149 return 0; 150 } 151 152 static int gpio_mockup_apply_pull(struct gpio_mockup_chip *chip, 153 unsigned int offset, int value) 154 { 155 struct gpio_mockup_line_status *line = &chip->lines[offset]; 156 int curr, irq, irq_type, ret = 0; 157 158 guard(mutex)(&chip->lock); 159 160 if (line->requested && line->dir == GPIO_LINE_DIRECTION_IN) { 161 curr = __gpio_mockup_get(chip, offset); 162 if (curr == value) 163 goto out; 164 165 irq = irq_find_mapping(chip->irq_sim_domain, offset); 166 if (!irq) 167 /* 168 * This is fine - it just means, nobody is listening 169 * for interrupts on this line, otherwise 170 * irq_create_mapping() would have been called from 171 * the to_irq() callback. 172 */ 173 goto set_value; 174 175 irq_type = irq_get_trigger_type(irq); 176 177 if ((value == 1 && (irq_type & IRQ_TYPE_EDGE_RISING)) || 178 (value == 0 && (irq_type & IRQ_TYPE_EDGE_FALLING))) { 179 ret = irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING, 180 true); 181 if (ret) 182 goto out; 183 } 184 } 185 186 set_value: 187 /* Change the value unless we're actively driving the line. */ 188 if (!line->requested || line->dir == GPIO_LINE_DIRECTION_IN) 189 __gpio_mockup_set(chip, offset, value); 190 191 out: 192 chip->lines[offset].pull = value; 193 return ret; 194 } 195 196 static int gpio_mockup_set_config(struct gpio_chip *gc, 197 unsigned int offset, unsigned long config) 198 { 199 struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 200 201 switch (pinconf_to_config_param(config)) { 202 case PIN_CONFIG_BIAS_PULL_UP: 203 return gpio_mockup_apply_pull(chip, offset, 1); 204 case PIN_CONFIG_BIAS_PULL_DOWN: 205 return gpio_mockup_apply_pull(chip, offset, 0); 206 default: 207 break; 208 } 209 return -ENOTSUPP; 210 } 211 212 static int gpio_mockup_dirout(struct gpio_chip *gc, 213 unsigned int offset, int value) 214 { 215 struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 216 217 scoped_guard(mutex, &chip->lock) { 218 chip->lines[offset].dir = GPIO_LINE_DIRECTION_OUT; 219 __gpio_mockup_set(chip, offset, value); 220 } 221 222 return 0; 223 } 224 225 static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset) 226 { 227 struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 228 229 scoped_guard(mutex, &chip->lock) 230 chip->lines[offset].dir = GPIO_LINE_DIRECTION_IN; 231 232 return 0; 233 } 234 235 static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset) 236 { 237 struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 238 int direction; 239 240 scoped_guard(mutex, &chip->lock) 241 direction = chip->lines[offset].dir; 242 243 return direction; 244 } 245 246 static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset) 247 { 248 struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 249 250 return irq_create_mapping(chip->irq_sim_domain, offset); 251 } 252 253 static int gpio_mockup_request(struct gpio_chip *gc, unsigned int offset) 254 { 255 struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 256 257 scoped_guard(mutex, &chip->lock) 258 chip->lines[offset].requested = true; 259 260 return 0; 261 } 262 263 static void gpio_mockup_free(struct gpio_chip *gc, unsigned int offset) 264 { 265 struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 266 267 guard(mutex)(&chip->lock); 268 269 chip->lines[offset].requested = false; 270 __gpio_mockup_set(chip, offset, chip->lines[offset].pull); 271 } 272 273 static ssize_t gpio_mockup_debugfs_read(struct file *file, 274 char __user *usr_buf, 275 size_t size, loff_t *ppos) 276 { 277 struct gpio_mockup_dbgfs_private *priv; 278 struct gpio_mockup_chip *chip; 279 struct seq_file *sfile; 280 struct gpio_chip *gc; 281 int val, cnt; 282 char buf[3]; 283 284 if (*ppos != 0) 285 return 0; 286 287 sfile = file->private_data; 288 priv = sfile->private; 289 chip = priv->chip; 290 gc = &chip->gc; 291 292 val = gpio_mockup_get(gc, priv->offset); 293 cnt = snprintf(buf, sizeof(buf), "%d\n", val); 294 295 return simple_read_from_buffer(usr_buf, size, ppos, buf, cnt); 296 } 297 298 static ssize_t gpio_mockup_debugfs_write(struct file *file, 299 const char __user *usr_buf, 300 size_t size, loff_t *ppos) 301 { 302 struct gpio_mockup_dbgfs_private *priv; 303 int rv, val; 304 struct seq_file *sfile; 305 306 if (*ppos != 0) 307 return -EINVAL; 308 309 rv = kstrtoint_from_user(usr_buf, size, 0, &val); 310 if (rv) 311 return rv; 312 if (val != 0 && val != 1) 313 return -EINVAL; 314 315 sfile = file->private_data; 316 priv = sfile->private; 317 rv = gpio_mockup_apply_pull(priv->chip, priv->offset, val); 318 if (rv) 319 return rv; 320 321 return size; 322 } 323 324 static int gpio_mockup_debugfs_open(struct inode *inode, struct file *file) 325 { 326 return single_open(file, NULL, inode->i_private); 327 } 328 329 /* 330 * Each mockup chip is represented by a directory named after the chip's device 331 * name under /sys/kernel/debug/gpio-mockup/. Each line is represented by 332 * a file using the line's offset as the name under the chip's directory. 333 * 334 * Reading from the line's file yields the current *value*, writing to the 335 * line's file changes the current *pull*. Default pull for mockup lines is 336 * down. 337 * 338 * Examples: 339 * - when a line pulled down is requested in output mode and driven high, its 340 * value will return to 0 once it's released 341 * - when the line is requested in output mode and driven high, writing 0 to 342 * the corresponding debugfs file will change the pull to down but the 343 * reported value will still be 1 until the line is released 344 * - line requested in input mode always reports the same value as its pull 345 * configuration 346 * - when the line is requested in input mode and monitored for events, writing 347 * the same value to the debugfs file will be a noop, while writing the 348 * opposite value will generate a dummy interrupt with an appropriate edge 349 */ 350 static const struct file_operations gpio_mockup_debugfs_ops = { 351 .owner = THIS_MODULE, 352 .open = gpio_mockup_debugfs_open, 353 .read = gpio_mockup_debugfs_read, 354 .write = gpio_mockup_debugfs_write, 355 .release = single_release, 356 }; 357 358 static void gpio_mockup_debugfs_setup(struct device *dev, 359 struct gpio_mockup_chip *chip) 360 { 361 struct gpio_mockup_dbgfs_private *priv; 362 struct gpio_chip *gc; 363 const char *devname; 364 char *name; 365 int i; 366 367 gc = &chip->gc; 368 369 /* 370 * There can only be a single GPIO device per platform device in 371 * gpio-mockup so using device_find_any_child() is OK. 372 */ 373 struct device *child __free(put_device) = device_find_any_child(dev); 374 if (!child) 375 return; 376 377 devname = dev_name(child); 378 chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir); 379 380 for (i = 0; i < gc->ngpio; i++) { 381 name = devm_kasprintf(dev, GFP_KERNEL, "%d", i); 382 if (!name) 383 return; 384 385 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 386 if (!priv) 387 return; 388 389 priv->chip = chip; 390 priv->offset = i; 391 392 debugfs_create_file(name, 0600, chip->dbg_dir, priv, 393 &gpio_mockup_debugfs_ops); 394 } 395 } 396 397 static void gpio_mockup_debugfs_cleanup(void *data) 398 { 399 struct gpio_mockup_chip *chip = data; 400 401 debugfs_remove_recursive(chip->dbg_dir); 402 } 403 404 static void gpio_mockup_dispose_mappings(void *data) 405 { 406 struct gpio_mockup_chip *chip = data; 407 struct gpio_chip *gc = &chip->gc; 408 int i, irq; 409 410 for (i = 0; i < gc->ngpio; i++) { 411 irq = irq_find_mapping(chip->irq_sim_domain, i); 412 if (irq) 413 irq_dispose_mapping(irq); 414 } 415 } 416 417 static int gpio_mockup_probe(struct platform_device *pdev) 418 { 419 struct gpio_mockup_chip *chip; 420 struct gpio_chip *gc; 421 struct device *dev; 422 const char *name; 423 int rv, base, i; 424 u16 ngpio; 425 426 dev = &pdev->dev; 427 428 rv = device_property_read_u32(dev, "gpio-base", &base); 429 if (rv) 430 base = -1; 431 432 rv = device_property_read_u16(dev, "nr-gpios", &ngpio); 433 if (rv) 434 return rv; 435 436 rv = device_property_read_string(dev, "chip-label", &name); 437 if (rv) 438 name = dev_name(dev); 439 440 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); 441 if (!chip) 442 return -ENOMEM; 443 444 mutex_init(&chip->lock); 445 446 gc = &chip->gc; 447 gc->base = base; 448 gc->ngpio = ngpio; 449 gc->label = name; 450 gc->owner = THIS_MODULE; 451 gc->parent = dev; 452 gc->get = gpio_mockup_get; 453 gc->set = gpio_mockup_set; 454 gc->get_multiple = gpio_mockup_get_multiple; 455 gc->set_multiple = gpio_mockup_set_multiple; 456 gc->direction_output = gpio_mockup_dirout; 457 gc->direction_input = gpio_mockup_dirin; 458 gc->get_direction = gpio_mockup_get_direction; 459 gc->set_config = gpio_mockup_set_config; 460 gc->to_irq = gpio_mockup_to_irq; 461 gc->request = gpio_mockup_request; 462 gc->free = gpio_mockup_free; 463 464 chip->lines = devm_kcalloc(dev, gc->ngpio, 465 sizeof(*chip->lines), GFP_KERNEL); 466 if (!chip->lines) 467 return -ENOMEM; 468 469 for (i = 0; i < gc->ngpio; i++) 470 chip->lines[i].dir = GPIO_LINE_DIRECTION_IN; 471 472 chip->irq_sim_domain = devm_irq_domain_create_sim(dev, NULL, 473 gc->ngpio); 474 if (IS_ERR(chip->irq_sim_domain)) 475 return PTR_ERR(chip->irq_sim_domain); 476 477 rv = devm_add_action_or_reset(dev, gpio_mockup_dispose_mappings, chip); 478 if (rv) 479 return rv; 480 481 rv = devm_gpiochip_add_data(dev, &chip->gc, chip); 482 if (rv) 483 return rv; 484 485 gpio_mockup_debugfs_setup(dev, chip); 486 487 return devm_add_action_or_reset(dev, gpio_mockup_debugfs_cleanup, chip); 488 } 489 490 static const struct of_device_id gpio_mockup_of_match[] = { 491 { .compatible = "gpio-mockup", }, 492 {}, 493 }; 494 MODULE_DEVICE_TABLE(of, gpio_mockup_of_match); 495 496 static struct platform_driver gpio_mockup_driver = { 497 .driver = { 498 .name = "gpio-mockup", 499 .of_match_table = gpio_mockup_of_match, 500 }, 501 .probe = gpio_mockup_probe, 502 }; 503 504 static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC]; 505 506 static void gpio_mockup_unregister_pdevs(void) 507 { 508 struct platform_device *pdev; 509 struct fwnode_handle *fwnode; 510 int i; 511 512 for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) { 513 pdev = gpio_mockup_pdevs[i]; 514 if (!pdev) 515 continue; 516 517 fwnode = dev_fwnode(&pdev->dev); 518 platform_device_unregister(pdev); 519 fwnode_remove_software_node(fwnode); 520 } 521 } 522 523 static int __init gpio_mockup_register_chip(int idx) 524 { 525 struct property_entry properties[GPIO_MOCKUP_MAX_PROP]; 526 struct platform_device_info pdevinfo; 527 struct platform_device *pdev; 528 struct fwnode_handle *fwnode; 529 char **line_names = NULL; 530 char chip_label[32]; 531 int prop = 0, base; 532 u16 ngpio; 533 534 memset(properties, 0, sizeof(properties)); 535 memset(&pdevinfo, 0, sizeof(pdevinfo)); 536 537 snprintf(chip_label, sizeof(chip_label), "gpio-mockup-%c", idx + 'A'); 538 properties[prop++] = PROPERTY_ENTRY_STRING("chip-label", chip_label); 539 540 base = gpio_mockup_range_base(idx); 541 if (base >= 0) 542 properties[prop++] = PROPERTY_ENTRY_U32("gpio-base", base); 543 544 ngpio = base < 0 ? gpio_mockup_range_ngpio(idx) 545 : gpio_mockup_range_ngpio(idx) - base; 546 properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio); 547 548 if (gpio_mockup_named_lines) { 549 line_names = kasprintf_strarray(GFP_KERNEL, chip_label, ngpio); 550 if (!line_names) 551 return -ENOMEM; 552 553 properties[prop++] = PROPERTY_ENTRY_STRING_ARRAY_LEN( 554 "gpio-line-names", line_names, ngpio); 555 } 556 557 fwnode = fwnode_create_software_node(properties, NULL); 558 if (IS_ERR(fwnode)) { 559 kfree_strarray(line_names, ngpio); 560 return PTR_ERR(fwnode); 561 } 562 563 pdevinfo.name = "gpio-mockup"; 564 pdevinfo.id = idx; 565 pdevinfo.fwnode = fwnode; 566 567 pdev = platform_device_register_full(&pdevinfo); 568 kfree_strarray(line_names, ngpio); 569 if (IS_ERR(pdev)) { 570 fwnode_remove_software_node(fwnode); 571 pr_err("error registering device"); 572 return PTR_ERR(pdev); 573 } 574 575 gpio_mockup_pdevs[idx] = pdev; 576 577 return 0; 578 } 579 580 static int __init gpio_mockup_init(void) 581 { 582 int i, num_chips, err, base, ngpio; 583 584 if ((gpio_mockup_num_ranges % 2) || 585 (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES)) 586 return -EINVAL; 587 588 /* Each chip is described by two values. */ 589 num_chips = gpio_mockup_num_ranges / 2; 590 591 /* 592 * The second value in the <base GPIO - number of GPIOS> pair must 593 * always be greater than 0. 594 */ 595 for (i = 0; i < num_chips; i++) { 596 base = gpio_mockup_range_base(i); 597 ngpio = gpio_mockup_range_ngpio(i); 598 599 if (ngpio <= 0) 600 return -EINVAL; 601 602 if (base < 0) { 603 if (ngpio > U16_MAX) 604 return -EINVAL; 605 } else { 606 if (ngpio <= base || ngpio - base > U16_MAX) 607 return -EINVAL; 608 } 609 } 610 611 gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup", NULL); 612 613 err = platform_driver_register(&gpio_mockup_driver); 614 if (err) { 615 pr_err("error registering platform driver\n"); 616 debugfs_remove_recursive(gpio_mockup_dbg_dir); 617 return err; 618 } 619 620 for (i = 0; i < num_chips; i++) { 621 err = gpio_mockup_register_chip(i); 622 if (err) { 623 platform_driver_unregister(&gpio_mockup_driver); 624 gpio_mockup_unregister_pdevs(); 625 debugfs_remove_recursive(gpio_mockup_dbg_dir); 626 return err; 627 } 628 } 629 630 return 0; 631 } 632 633 static void __exit gpio_mockup_exit(void) 634 { 635 gpio_mockup_unregister_pdevs(); 636 debugfs_remove_recursive(gpio_mockup_dbg_dir); 637 platform_driver_unregister(&gpio_mockup_driver); 638 } 639 640 module_init(gpio_mockup_init); 641 module_exit(gpio_mockup_exit); 642 643 MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>"); 644 MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>"); 645 MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>"); 646 MODULE_DESCRIPTION("GPIO Testing driver"); 647 MODULE_LICENSE("GPL v2"); 648