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

Configure Feed

Select the types of activity you want to include in your feed.

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