1 // SPDX-License-Identifier: GPL-2.0-only 2 // 3 // GPIO Aggregator 4 // 5 // Copyright (C) 2019-2020 Glider bv 6 7 #define DRV_NAME "gpio-aggregator" 8 #define pr_fmt(fmt) DRV_NAME ": " fmt 9 10 #include <linux/bitmap.h> 11 #include <linux/bitops.h> 12 #include <linux/configfs.h> 13 #include <linux/ctype.h> 14 #include <linux/delay.h> 15 #include <linux/export.h> 16 #include <linux/idr.h> 17 #include <linux/kernel.h> 18 #include <linux/list.h> 19 #include <linux/lockdep.h> 20 #include <linux/module.h> 21 #include <linux/mutex.h> 22 #include <linux/overflow.h> 23 #include <linux/platform_device.h> 24 #include <linux/property.h> 25 #include <linux/slab.h> 26 #include <linux/spinlock.h> 27 #include <linux/string.h> 28 29 #include <linux/gpio/consumer.h> 30 #include <linux/gpio/driver.h> 31 #include <linux/gpio/forwarder.h> 32 #include <linux/gpio/machine.h> 33 34 #define AGGREGATOR_MAX_GPIOS 512 35 #define AGGREGATOR_LEGACY_PREFIX "_sysfs" 36 37 /* 38 * GPIO Aggregator sysfs interface 39 */ 40 41 struct gpio_aggregator { 42 struct platform_device *pdev; 43 struct config_group group; 44 struct gpiod_lookup_table *lookups; 45 struct mutex lock; 46 int id; 47 48 /* List of gpio_aggregator_line. Always added in order */ 49 struct list_head list_head; 50 51 /* used by legacy sysfs interface only */ 52 bool init_via_sysfs; 53 char args[]; 54 }; 55 56 struct gpio_aggregator_line { 57 struct config_group group; 58 struct gpio_aggregator *parent; 59 struct list_head entry; 60 61 /* Line index within the aggregator device */ 62 unsigned int idx; 63 64 /* Custom name for the virtual line */ 65 const char *name; 66 /* GPIO chip label or line name */ 67 const char *key; 68 /* Can be negative to indicate lookup by line name */ 69 int offset; 70 71 enum gpio_lookup_flags flags; 72 }; 73 74 struct gpio_aggregator_pdev_meta { 75 bool init_via_sysfs; 76 }; 77 78 static DEFINE_MUTEX(gpio_aggregator_lock); /* protects idr */ 79 static DEFINE_IDR(gpio_aggregator_idr); 80 81 static int gpio_aggregator_alloc(struct gpio_aggregator **aggr, size_t arg_size) 82 { 83 int ret; 84 85 struct gpio_aggregator *new __free(kfree) = kzalloc( 86 sizeof(*new) + arg_size, GFP_KERNEL); 87 if (!new) 88 return -ENOMEM; 89 90 scoped_guard(mutex, &gpio_aggregator_lock) 91 ret = idr_alloc(&gpio_aggregator_idr, new, 0, 0, GFP_KERNEL); 92 93 if (ret < 0) 94 return ret; 95 96 new->id = ret; 97 INIT_LIST_HEAD(&new->list_head); 98 mutex_init(&new->lock); 99 *aggr = no_free_ptr(new); 100 return 0; 101 } 102 103 static void gpio_aggregator_free(struct gpio_aggregator *aggr) 104 { 105 scoped_guard(mutex, &gpio_aggregator_lock) 106 idr_remove(&gpio_aggregator_idr, aggr->id); 107 108 mutex_destroy(&aggr->lock); 109 kfree(aggr); 110 } 111 112 static int gpio_aggregator_add_gpio(struct gpio_aggregator *aggr, 113 const char *key, int hwnum, unsigned int *n) 114 { 115 struct gpiod_lookup_table *lookups; 116 117 lookups = krealloc(aggr->lookups, struct_size(lookups, table, *n + 2), 118 GFP_KERNEL); 119 if (!lookups) 120 return -ENOMEM; 121 122 lookups->table[*n] = GPIO_LOOKUP_IDX(key, hwnum, NULL, *n, 0); 123 124 (*n)++; 125 memset(&lookups->table[*n], 0, sizeof(lookups->table[*n])); 126 127 aggr->lookups = lookups; 128 return 0; 129 } 130 131 static bool gpio_aggregator_is_active(struct gpio_aggregator *aggr) 132 { 133 lockdep_assert_held(&aggr->lock); 134 135 return aggr->pdev && platform_get_drvdata(aggr->pdev); 136 } 137 138 /* Only aggregators created via legacy sysfs can be "activating". */ 139 static bool gpio_aggregator_is_activating(struct gpio_aggregator *aggr) 140 { 141 lockdep_assert_held(&aggr->lock); 142 143 return aggr->pdev && !platform_get_drvdata(aggr->pdev); 144 } 145 146 static size_t gpio_aggregator_count_lines(struct gpio_aggregator *aggr) 147 { 148 lockdep_assert_held(&aggr->lock); 149 150 return list_count_nodes(&aggr->list_head); 151 } 152 153 static struct gpio_aggregator_line * 154 gpio_aggregator_line_alloc(struct gpio_aggregator *parent, unsigned int idx, 155 char *key, int offset) 156 { 157 struct gpio_aggregator_line *line; 158 159 line = kzalloc_obj(*line); 160 if (!line) 161 return ERR_PTR(-ENOMEM); 162 163 if (key) { 164 line->key = kstrdup(key, GFP_KERNEL); 165 if (!line->key) { 166 kfree(line); 167 return ERR_PTR(-ENOMEM); 168 } 169 } 170 171 line->flags = GPIO_LOOKUP_FLAGS_DEFAULT; 172 line->parent = parent; 173 line->idx = idx; 174 line->offset = offset; 175 INIT_LIST_HEAD(&line->entry); 176 177 return line; 178 } 179 180 static void gpio_aggregator_line_add(struct gpio_aggregator *aggr, 181 struct gpio_aggregator_line *line) 182 { 183 struct gpio_aggregator_line *tmp; 184 185 lockdep_assert_held(&aggr->lock); 186 187 list_for_each_entry(tmp, &aggr->list_head, entry) { 188 if (tmp->idx > line->idx) { 189 list_add_tail(&line->entry, &tmp->entry); 190 return; 191 } 192 } 193 list_add_tail(&line->entry, &aggr->list_head); 194 } 195 196 static void gpio_aggregator_line_del(struct gpio_aggregator *aggr, 197 struct gpio_aggregator_line *line) 198 { 199 lockdep_assert_held(&aggr->lock); 200 201 list_del(&line->entry); 202 } 203 204 static void gpio_aggregator_free_lines(struct gpio_aggregator *aggr) 205 { 206 struct gpio_aggregator_line *line, *tmp; 207 208 list_for_each_entry_safe(line, tmp, &aggr->list_head, entry) { 209 configfs_unregister_group(&line->group); 210 /* 211 * Normally, we acquire aggr->lock within the configfs 212 * callback. However, in the legacy sysfs interface case, 213 * calling configfs_(un)register_group while holding 214 * aggr->lock could cause a deadlock. Fortunately, this is 215 * unnecessary because the new_device/delete_device path 216 * and the module unload path are mutually exclusive, 217 * thanks to an explicit try_module_get. That's why this 218 * minimal scoped_guard suffices. 219 */ 220 scoped_guard(mutex, &aggr->lock) 221 gpio_aggregator_line_del(aggr, line); 222 kfree(line->key); 223 kfree(line->name); 224 kfree(line); 225 } 226 } 227 228 229 /* 230 * GPIO Forwarder 231 */ 232 233 struct gpiochip_fwd_timing { 234 u32 ramp_up_us; 235 u32 ramp_down_us; 236 }; 237 238 struct gpiochip_fwd { 239 struct gpio_chip chip; 240 struct gpio_desc **descs; 241 union { 242 struct mutex mlock; /* protects tmp[] if can_sleep */ 243 spinlock_t slock; /* protects tmp[] if !can_sleep */ 244 }; 245 struct gpiochip_fwd_timing *delay_timings; 246 void *data; 247 unsigned long *valid_mask; 248 unsigned long tmp[]; /* values and descs for multiple ops */ 249 }; 250 251 #define fwd_tmp_values(fwd) (&(fwd)->tmp[0]) 252 #define fwd_tmp_descs(fwd) ((void *)&(fwd)->tmp[BITS_TO_LONGS((fwd)->chip.ngpio)]) 253 254 #define fwd_tmp_size(ngpios) (BITS_TO_LONGS((ngpios)) + (ngpios)) 255 256 static int gpio_fwd_request(struct gpio_chip *chip, unsigned int offset) 257 { 258 struct gpiochip_fwd *fwd = gpiochip_get_data(chip); 259 260 return test_bit(offset, fwd->valid_mask) ? 0 : -ENODEV; 261 } 262 263 static int gpio_fwd_get_direction(struct gpio_chip *chip, unsigned int offset) 264 { 265 struct gpiochip_fwd *fwd = gpiochip_get_data(chip); 266 267 /* 268 * get_direction() is called during gpiochip registration, return 269 * -ENODEV if there is no GPIO desc for the line. 270 */ 271 if (!test_bit(offset, fwd->valid_mask)) 272 return -ENODEV; 273 274 return gpiod_get_direction(fwd->descs[offset]); 275 } 276 277 static int gpio_fwd_direction_input(struct gpio_chip *chip, unsigned int offset) 278 { 279 struct gpiochip_fwd *fwd = gpiochip_get_data(chip); 280 281 return gpiod_direction_input(fwd->descs[offset]); 282 } 283 284 static int gpio_fwd_direction_output(struct gpio_chip *chip, 285 unsigned int offset, int value) 286 { 287 struct gpiochip_fwd *fwd = gpiochip_get_data(chip); 288 289 return gpiod_direction_output(fwd->descs[offset], value); 290 } 291 292 static int gpio_fwd_get(struct gpio_chip *chip, unsigned int offset) 293 { 294 struct gpiochip_fwd *fwd = gpiochip_get_data(chip); 295 296 return chip->can_sleep ? gpiod_get_value_cansleep(fwd->descs[offset]) 297 : gpiod_get_value(fwd->descs[offset]); 298 } 299 300 static int gpio_fwd_get_multiple(struct gpiochip_fwd *fwd, unsigned long *mask, 301 unsigned long *bits) 302 { 303 struct gpio_desc **descs = fwd_tmp_descs(fwd); 304 unsigned long *values = fwd_tmp_values(fwd); 305 unsigned int i, j = 0; 306 int error; 307 308 bitmap_clear(values, 0, fwd->chip.ngpio); 309 for_each_set_bit(i, mask, fwd->chip.ngpio) 310 descs[j++] = fwd->descs[i]; 311 312 if (fwd->chip.can_sleep) 313 error = gpiod_get_array_value_cansleep(j, descs, NULL, values); 314 else 315 error = gpiod_get_array_value(j, descs, NULL, values); 316 if (error) 317 return error; 318 319 j = 0; 320 for_each_set_bit(i, mask, fwd->chip.ngpio) 321 __assign_bit(i, bits, test_bit(j++, values)); 322 323 return 0; 324 } 325 326 static int gpio_fwd_get_multiple_locked(struct gpio_chip *chip, 327 unsigned long *mask, unsigned long *bits) 328 { 329 struct gpiochip_fwd *fwd = gpiochip_get_data(chip); 330 unsigned long flags; 331 int error; 332 333 if (chip->can_sleep) { 334 mutex_lock(&fwd->mlock); 335 error = gpio_fwd_get_multiple(fwd, mask, bits); 336 mutex_unlock(&fwd->mlock); 337 } else { 338 spin_lock_irqsave(&fwd->slock, flags); 339 error = gpio_fwd_get_multiple(fwd, mask, bits); 340 spin_unlock_irqrestore(&fwd->slock, flags); 341 } 342 343 return error; 344 } 345 346 static void gpio_fwd_delay(struct gpio_chip *chip, unsigned int offset, int value) 347 { 348 struct gpiochip_fwd *fwd = gpiochip_get_data(chip); 349 const struct gpiochip_fwd_timing *delay_timings; 350 bool is_active_low = gpiod_is_active_low(fwd->descs[offset]); 351 u32 delay_us; 352 353 delay_timings = &fwd->delay_timings[offset]; 354 if ((!is_active_low && value) || (is_active_low && !value)) 355 delay_us = delay_timings->ramp_up_us; 356 else 357 delay_us = delay_timings->ramp_down_us; 358 if (!delay_us) 359 return; 360 361 if (chip->can_sleep) 362 fsleep(delay_us); 363 else 364 udelay(delay_us); 365 } 366 367 static int gpio_fwd_set(struct gpio_chip *chip, unsigned int offset, int value) 368 { 369 struct gpiochip_fwd *fwd = gpiochip_get_data(chip); 370 int ret; 371 372 if (chip->can_sleep) 373 ret = gpiod_set_value_cansleep(fwd->descs[offset], value); 374 else 375 ret = gpiod_set_value(fwd->descs[offset], value); 376 if (ret) 377 return ret; 378 379 if (fwd->delay_timings) 380 gpio_fwd_delay(chip, offset, value); 381 382 return ret; 383 } 384 385 static int gpio_fwd_set_multiple(struct gpiochip_fwd *fwd, unsigned long *mask, 386 unsigned long *bits) 387 { 388 struct gpio_desc **descs = fwd_tmp_descs(fwd); 389 unsigned long *values = fwd_tmp_values(fwd); 390 unsigned int i, j = 0, ret; 391 392 for_each_set_bit(i, mask, fwd->chip.ngpio) { 393 __assign_bit(j, values, test_bit(i, bits)); 394 descs[j++] = fwd->descs[i]; 395 } 396 397 if (fwd->chip.can_sleep) 398 ret = gpiod_set_array_value_cansleep(j, descs, NULL, values); 399 else 400 ret = gpiod_set_array_value(j, descs, NULL, values); 401 402 return ret; 403 } 404 405 static int gpio_fwd_set_multiple_locked(struct gpio_chip *chip, 406 unsigned long *mask, unsigned long *bits) 407 { 408 struct gpiochip_fwd *fwd = gpiochip_get_data(chip); 409 unsigned long flags; 410 int ret; 411 412 if (chip->can_sleep) { 413 mutex_lock(&fwd->mlock); 414 ret = gpio_fwd_set_multiple(fwd, mask, bits); 415 mutex_unlock(&fwd->mlock); 416 } else { 417 spin_lock_irqsave(&fwd->slock, flags); 418 ret = gpio_fwd_set_multiple(fwd, mask, bits); 419 spin_unlock_irqrestore(&fwd->slock, flags); 420 } 421 422 return ret; 423 } 424 425 static int gpio_fwd_set_config(struct gpio_chip *chip, unsigned int offset, 426 unsigned long config) 427 { 428 struct gpiochip_fwd *fwd = gpiochip_get_data(chip); 429 430 return gpiod_set_config(fwd->descs[offset], config); 431 } 432 433 static int gpio_fwd_to_irq(struct gpio_chip *chip, unsigned int offset) 434 { 435 struct gpiochip_fwd *fwd = gpiochip_get_data(chip); 436 437 return gpiod_to_irq(fwd->descs[offset]); 438 } 439 440 /* 441 * The GPIO delay provides a way to configure platform specific delays 442 * for the GPIO ramp-up or ramp-down delays. This can serve the following 443 * purposes: 444 * - Open-drain output using an RC filter 445 */ 446 #define FWD_FEATURE_DELAY BIT(0) 447 448 #ifdef CONFIG_OF_GPIO 449 static int gpiochip_fwd_delay_of_xlate(struct gpio_chip *chip, 450 const struct of_phandle_args *gpiospec, 451 u32 *flags) 452 { 453 struct gpiochip_fwd *fwd = gpiochip_get_data(chip); 454 struct gpiochip_fwd_timing *timings; 455 u32 line; 456 457 if (gpiospec->args_count != chip->of_gpio_n_cells) 458 return -EINVAL; 459 460 line = gpiospec->args[0]; 461 if (line >= chip->ngpio) 462 return -EINVAL; 463 464 timings = &fwd->delay_timings[line]; 465 timings->ramp_up_us = gpiospec->args[1]; 466 timings->ramp_down_us = gpiospec->args[2]; 467 468 return line; 469 } 470 471 static int gpiochip_fwd_setup_delay_line(struct gpiochip_fwd *fwd) 472 { 473 struct gpio_chip *chip = &fwd->chip; 474 475 fwd->delay_timings = devm_kcalloc(chip->parent, chip->ngpio, 476 sizeof(*fwd->delay_timings), 477 GFP_KERNEL); 478 if (!fwd->delay_timings) 479 return -ENOMEM; 480 481 chip->of_xlate = gpiochip_fwd_delay_of_xlate; 482 chip->of_gpio_n_cells = 3; 483 484 return 0; 485 } 486 #else 487 static int gpiochip_fwd_setup_delay_line(struct gpiochip_fwd *fwd) 488 { 489 return 0; 490 } 491 #endif /* !CONFIG_OF_GPIO */ 492 493 /** 494 * gpiochip_fwd_get_gpiochip - Get the GPIO chip for the GPIO forwarder 495 * @fwd: GPIO forwarder 496 * 497 * Returns: The GPIO chip for the GPIO forwarder 498 */ 499 struct gpio_chip *gpiochip_fwd_get_gpiochip(struct gpiochip_fwd *fwd) 500 { 501 return &fwd->chip; 502 } 503 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_get_gpiochip, "GPIO_FORWARDER"); 504 505 /** 506 * gpiochip_fwd_get_data - Get driver-private data for the GPIO forwarder 507 * @fwd: GPIO forwarder 508 * 509 * Returns: The driver-private data for the GPIO forwarder 510 */ 511 void *gpiochip_fwd_get_data(struct gpiochip_fwd *fwd) 512 { 513 return fwd->data; 514 } 515 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_get_data, "GPIO_FORWARDER"); 516 517 /** 518 * gpiochip_fwd_gpio_request - Request a line of the GPIO forwarder 519 * @fwd: GPIO forwarder 520 * @offset: the offset of the line to request 521 * 522 * Returns: 0 on success, or negative errno on failure. 523 */ 524 int gpiochip_fwd_gpio_request(struct gpiochip_fwd *fwd, unsigned int offset) 525 { 526 struct gpio_chip *gc = gpiochip_fwd_get_gpiochip(fwd); 527 528 return gpio_fwd_request(gc, offset); 529 } 530 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_gpio_request, "GPIO_FORWARDER"); 531 532 /** 533 * gpiochip_fwd_gpio_get_direction - Return the current direction of a GPIO forwarder line 534 * @fwd: GPIO forwarder 535 * @offset: the offset of the line 536 * 537 * Returns: 0 for output, 1 for input, or an error code in case of error. 538 */ 539 int gpiochip_fwd_gpio_get_direction(struct gpiochip_fwd *fwd, unsigned int offset) 540 { 541 struct gpio_chip *gc = gpiochip_fwd_get_gpiochip(fwd); 542 543 return gpio_fwd_get_direction(gc, offset); 544 } 545 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_gpio_get_direction, "GPIO_FORWARDER"); 546 547 /** 548 * gpiochip_fwd_gpio_direction_output - Set a GPIO forwarder line direction to 549 * output 550 * @fwd: GPIO forwarder 551 * @offset: the offset of the line 552 * @value: value to set 553 * 554 * Returns: 0 on success, or negative errno on failure. 555 */ 556 int gpiochip_fwd_gpio_direction_output(struct gpiochip_fwd *fwd, unsigned int offset, 557 int value) 558 { 559 struct gpio_chip *gc = gpiochip_fwd_get_gpiochip(fwd); 560 561 return gpio_fwd_direction_output(gc, offset, value); 562 } 563 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_gpio_direction_output, "GPIO_FORWARDER"); 564 565 /** 566 * gpiochip_fwd_gpio_direction_input - Set a GPIO forwarder line direction to input 567 * @fwd: GPIO forwarder 568 * @offset: the offset of the line 569 * 570 * Returns: 0 on success, or negative errno on failure. 571 */ 572 int gpiochip_fwd_gpio_direction_input(struct gpiochip_fwd *fwd, unsigned int offset) 573 { 574 struct gpio_chip *gc = gpiochip_fwd_get_gpiochip(fwd); 575 576 return gpio_fwd_direction_input(gc, offset); 577 } 578 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_gpio_direction_input, "GPIO_FORWARDER"); 579 580 /** 581 * gpiochip_fwd_gpio_get - Return a GPIO forwarder line's value 582 * @fwd: GPIO forwarder 583 * @offset: the offset of the line 584 * 585 * Returns: The GPIO's logical value, i.e. taking the ACTIVE_LOW status into 586 * account, or negative errno on failure. 587 */ 588 int gpiochip_fwd_gpio_get(struct gpiochip_fwd *fwd, unsigned int offset) 589 { 590 struct gpio_chip *gc = gpiochip_fwd_get_gpiochip(fwd); 591 592 return gpio_fwd_get(gc, offset); 593 } 594 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_gpio_get, "GPIO_FORWARDER"); 595 596 /** 597 * gpiochip_fwd_gpio_get_multiple - Get values for multiple GPIO forwarder lines 598 * @fwd: GPIO forwarder 599 * @mask: bit mask array; one bit per line; BITS_PER_LONG bits per word defines 600 * which lines are to be read 601 * @bits: bit value array; one bit per line; BITS_PER_LONG bits per word will 602 * contains the read values for the lines specified by mask 603 * 604 * Returns: 0 on success, or negative errno on failure. 605 */ 606 int gpiochip_fwd_gpio_get_multiple(struct gpiochip_fwd *fwd, unsigned long *mask, 607 unsigned long *bits) 608 { 609 struct gpio_chip *gc = gpiochip_fwd_get_gpiochip(fwd); 610 611 return gpio_fwd_get_multiple_locked(gc, mask, bits); 612 } 613 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_gpio_get_multiple, "GPIO_FORWARDER"); 614 615 /** 616 * gpiochip_fwd_gpio_set - Assign value to a GPIO forwarder line. 617 * @fwd: GPIO forwarder 618 * @offset: the offset of the line 619 * @value: value to set 620 * 621 * Returns: 0 on success, or negative errno on failure. 622 */ 623 int gpiochip_fwd_gpio_set(struct gpiochip_fwd *fwd, unsigned int offset, int value) 624 { 625 struct gpio_chip *gc = gpiochip_fwd_get_gpiochip(fwd); 626 627 return gpio_fwd_set(gc, offset, value); 628 } 629 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_gpio_set, "GPIO_FORWARDER"); 630 631 /** 632 * gpiochip_fwd_gpio_set_multiple - Assign values to multiple GPIO forwarder lines 633 * @fwd: GPIO forwarder 634 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word 635 * defines which outputs are to be changed 636 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word 637 * defines the values the outputs specified by mask are to be set to 638 * 639 * Returns: 0 on success, or negative errno on failure. 640 */ 641 int gpiochip_fwd_gpio_set_multiple(struct gpiochip_fwd *fwd, unsigned long *mask, 642 unsigned long *bits) 643 { 644 struct gpio_chip *gc = gpiochip_fwd_get_gpiochip(fwd); 645 646 return gpio_fwd_set_multiple_locked(gc, mask, bits); 647 } 648 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_gpio_set_multiple, "GPIO_FORWARDER"); 649 650 /** 651 * gpiochip_fwd_gpio_set_config - Set @config for a GPIO forwarder line 652 * @fwd: GPIO forwarder 653 * @offset: the offset of the line 654 * @config: Same packed config format as generic pinconf 655 * 656 * Returns: 0 on success, %-ENOTSUPP if the controller doesn't support setting 657 * the configuration. 658 */ 659 int gpiochip_fwd_gpio_set_config(struct gpiochip_fwd *fwd, unsigned int offset, 660 unsigned long config) 661 { 662 struct gpio_chip *gc = gpiochip_fwd_get_gpiochip(fwd); 663 664 return gpio_fwd_set_config(gc, offset, config); 665 } 666 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_gpio_set_config, "GPIO_FORWARDER"); 667 668 /** 669 * gpiochip_fwd_gpio_to_irq - Return the IRQ corresponding to a GPIO forwarder line 670 * @fwd: GPIO forwarder 671 * @offset: the offset of the line 672 * 673 * Returns: The Linux IRQ corresponding to the passed line, or an error code in 674 * case of error. 675 */ 676 int gpiochip_fwd_gpio_to_irq(struct gpiochip_fwd *fwd, unsigned int offset) 677 { 678 struct gpio_chip *gc = gpiochip_fwd_get_gpiochip(fwd); 679 680 return gpio_fwd_to_irq(gc, offset); 681 } 682 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_gpio_to_irq, "GPIO_FORWARDER"); 683 684 /** 685 * devm_gpiochip_fwd_alloc - Allocate and initialize a new GPIO forwarder 686 * @dev: Parent device pointer 687 * @ngpios: Number of GPIOs in the forwarder 688 * 689 * Returns: An opaque object pointer, or an ERR_PTR()-encoded negative error 690 * code on failure. 691 */ 692 struct gpiochip_fwd *devm_gpiochip_fwd_alloc(struct device *dev, 693 unsigned int ngpios) 694 { 695 struct gpiochip_fwd *fwd; 696 struct gpio_chip *chip; 697 698 fwd = devm_kzalloc(dev, struct_size(fwd, tmp, fwd_tmp_size(ngpios)), GFP_KERNEL); 699 if (!fwd) 700 return ERR_PTR(-ENOMEM); 701 702 fwd->descs = devm_kcalloc(dev, ngpios, sizeof(*fwd->descs), GFP_KERNEL); 703 if (!fwd->descs) 704 return ERR_PTR(-ENOMEM); 705 706 fwd->valid_mask = devm_bitmap_zalloc(dev, ngpios, GFP_KERNEL); 707 if (!fwd->valid_mask) 708 return ERR_PTR(-ENOMEM); 709 710 chip = &fwd->chip; 711 712 chip->label = dev_name(dev); 713 chip->parent = dev; 714 chip->owner = THIS_MODULE; 715 chip->request = gpio_fwd_request; 716 chip->get_direction = gpio_fwd_get_direction; 717 chip->direction_input = gpio_fwd_direction_input; 718 chip->direction_output = gpio_fwd_direction_output; 719 chip->get = gpio_fwd_get; 720 chip->get_multiple = gpio_fwd_get_multiple_locked; 721 chip->set = gpio_fwd_set; 722 chip->set_multiple = gpio_fwd_set_multiple_locked; 723 chip->set_config = gpio_fwd_set_config; 724 chip->to_irq = gpio_fwd_to_irq; 725 chip->base = -1; 726 chip->ngpio = ngpios; 727 728 return fwd; 729 } 730 EXPORT_SYMBOL_NS_GPL(devm_gpiochip_fwd_alloc, "GPIO_FORWARDER"); 731 732 /** 733 * gpiochip_fwd_desc_add - Add a GPIO desc in the forwarder 734 * @fwd: GPIO forwarder 735 * @desc: GPIO descriptor to register 736 * @offset: offset for the GPIO in the forwarder 737 * 738 * Returns: 0 on success, or negative errno on failure. 739 */ 740 int gpiochip_fwd_desc_add(struct gpiochip_fwd *fwd, struct gpio_desc *desc, 741 unsigned int offset) 742 { 743 struct gpio_chip *chip = &fwd->chip; 744 745 if (offset >= chip->ngpio) 746 return -EINVAL; 747 748 if (test_and_set_bit(offset, fwd->valid_mask)) 749 return -EEXIST; 750 751 /* 752 * If any of the GPIO lines are sleeping, then the entire forwarder 753 * will be sleeping. 754 */ 755 if (gpiod_cansleep(desc)) 756 chip->can_sleep = true; 757 758 fwd->descs[offset] = desc; 759 760 dev_dbg(chip->parent, "%u => gpio %d irq %d\n", offset, 761 desc_to_gpio(desc), gpiod_to_irq(desc)); 762 763 return 0; 764 } 765 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_desc_add, "GPIO_FORWARDER"); 766 767 /** 768 * gpiochip_fwd_desc_free - Remove a GPIO desc from the forwarder 769 * @fwd: GPIO forwarder 770 * @offset: offset of GPIO desc to remove 771 */ 772 void gpiochip_fwd_desc_free(struct gpiochip_fwd *fwd, unsigned int offset) 773 { 774 if (test_and_clear_bit(offset, fwd->valid_mask)) 775 gpiod_put(fwd->descs[offset]); 776 } 777 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_desc_free, "GPIO_FORWARDER"); 778 779 /** 780 * gpiochip_fwd_register - Register a GPIO forwarder 781 * @fwd: GPIO forwarder 782 * @data: driver-private data associated with this forwarder 783 * 784 * Returns: 0 on success, or negative errno on failure. 785 */ 786 int gpiochip_fwd_register(struct gpiochip_fwd *fwd, void *data) 787 { 788 struct gpio_chip *chip = &fwd->chip; 789 790 /* 791 * Some gpio_desc were not registered. They will be registered at runtime 792 * but we have to suppose they can sleep. 793 */ 794 if (!bitmap_full(fwd->valid_mask, chip->ngpio)) 795 chip->can_sleep = true; 796 797 if (chip->can_sleep) 798 mutex_init(&fwd->mlock); 799 else 800 spin_lock_init(&fwd->slock); 801 802 fwd->data = data; 803 804 return devm_gpiochip_add_data(chip->parent, chip, fwd); 805 } 806 EXPORT_SYMBOL_NS_GPL(gpiochip_fwd_register, "GPIO_FORWARDER"); 807 808 /** 809 * gpiochip_fwd_create() - Create a new GPIO forwarder 810 * @dev: Parent device pointer 811 * @ngpios: Number of GPIOs in the forwarder. 812 * @descs: Array containing the GPIO descriptors to forward to. 813 * This array must contain @ngpios entries, and can be deallocated 814 * as the forwarder has its own array. 815 * @features: Bitwise ORed features as defined with FWD_FEATURE_*. 816 * 817 * This function creates a new gpiochip, which forwards all GPIO operations to 818 * the passed GPIO descriptors. 819 * 820 * Return: An opaque object pointer, or an ERR_PTR()-encoded negative error 821 * code on failure. 822 */ 823 static struct gpiochip_fwd *gpiochip_fwd_create(struct device *dev, 824 unsigned int ngpios, 825 struct gpio_desc *descs[], 826 unsigned long features) 827 { 828 struct gpiochip_fwd *fwd; 829 unsigned int i; 830 int error; 831 832 fwd = devm_gpiochip_fwd_alloc(dev, ngpios); 833 if (IS_ERR(fwd)) 834 return fwd; 835 836 for (i = 0; i < ngpios; i++) { 837 error = gpiochip_fwd_desc_add(fwd, descs[i], i); 838 if (error) 839 return ERR_PTR(error); 840 } 841 842 if (features & FWD_FEATURE_DELAY) { 843 error = gpiochip_fwd_setup_delay_line(fwd); 844 if (error) 845 return ERR_PTR(error); 846 } 847 848 error = gpiochip_fwd_register(fwd, NULL); 849 if (error) 850 return ERR_PTR(error); 851 852 return fwd; 853 } 854 855 /* 856 * Configfs interface 857 */ 858 859 static struct gpio_aggregator * 860 to_gpio_aggregator(struct config_item *item) 861 { 862 struct config_group *group = to_config_group(item); 863 864 return container_of(group, struct gpio_aggregator, group); 865 } 866 867 static struct gpio_aggregator_line * 868 to_gpio_aggregator_line(struct config_item *item) 869 { 870 struct config_group *group = to_config_group(item); 871 872 return container_of(group, struct gpio_aggregator_line, group); 873 } 874 875 static struct fwnode_handle * 876 gpio_aggregator_make_device_sw_node(struct gpio_aggregator *aggr) 877 { 878 struct property_entry properties[2]; 879 struct gpio_aggregator_line *line; 880 size_t num_lines; 881 int n = 0; 882 883 memset(properties, 0, sizeof(properties)); 884 885 num_lines = gpio_aggregator_count_lines(aggr); 886 if (num_lines == 0) 887 return NULL; 888 889 const char **line_names __free(kfree) = kcalloc( 890 num_lines, sizeof(*line_names), GFP_KERNEL); 891 if (!line_names) 892 return ERR_PTR(-ENOMEM); 893 894 /* The list is always sorted as new elements are inserted in order. */ 895 list_for_each_entry(line, &aggr->list_head, entry) 896 line_names[n++] = line->name ?: ""; 897 898 properties[0] = PROPERTY_ENTRY_STRING_ARRAY_LEN( 899 "gpio-line-names", 900 line_names, num_lines); 901 902 return fwnode_create_software_node(properties, NULL); 903 } 904 905 static int gpio_aggregator_activate(struct gpio_aggregator *aggr) 906 { 907 struct platform_device_info pdevinfo; 908 struct gpio_aggregator_line *line; 909 struct platform_device *pdev; 910 struct fwnode_handle *swnode; 911 unsigned int n = 0; 912 int ret = 0; 913 914 if (gpio_aggregator_count_lines(aggr) == 0) 915 return -EINVAL; 916 917 aggr->lookups = kzalloc_flex(*aggr->lookups, table, 1); 918 if (!aggr->lookups) 919 return -ENOMEM; 920 921 swnode = gpio_aggregator_make_device_sw_node(aggr); 922 if (IS_ERR(swnode)) { 923 ret = PTR_ERR(swnode); 924 goto err_remove_lookups; 925 } 926 927 memset(&pdevinfo, 0, sizeof(pdevinfo)); 928 pdevinfo.name = DRV_NAME; 929 pdevinfo.id = aggr->id; 930 pdevinfo.fwnode = swnode; 931 932 /* The list is always sorted as new elements are inserted in order. */ 933 list_for_each_entry(line, &aggr->list_head, entry) { 934 /* 935 * - Either GPIO chip label or line name must be configured 936 * (i.e. line->key must be non-NULL) 937 * - Line directories must be named with sequential numeric 938 * suffixes starting from 0. (i.e. ./line0, ./line1, ...) 939 */ 940 if (!line->key || line->idx != n) { 941 ret = -EINVAL; 942 goto err_remove_swnode; 943 } 944 945 if (line->offset < 0) 946 ret = gpio_aggregator_add_gpio(aggr, line->key, 947 U16_MAX, &n); 948 else 949 ret = gpio_aggregator_add_gpio(aggr, line->key, 950 line->offset, &n); 951 if (ret) 952 goto err_remove_swnode; 953 } 954 955 aggr->lookups->dev_id = kasprintf(GFP_KERNEL, "%s.%d", DRV_NAME, aggr->id); 956 if (!aggr->lookups->dev_id) { 957 ret = -ENOMEM; 958 goto err_remove_swnode; 959 } 960 961 gpiod_add_lookup_table(aggr->lookups); 962 963 pdev = platform_device_register_full(&pdevinfo); 964 if (IS_ERR(pdev)) { 965 ret = PTR_ERR(pdev); 966 goto err_remove_lookup_table; 967 } 968 969 wait_for_device_probe(); 970 971 scoped_guard(device, &pdev->dev) { 972 if (!device_is_bound(&pdev->dev)) { 973 ret = -ENXIO; 974 goto err_unregister_pdev; 975 } 976 } 977 978 aggr->pdev = pdev; 979 return 0; 980 981 err_unregister_pdev: 982 platform_device_unregister(pdev); 983 err_remove_lookup_table: 984 gpiod_remove_lookup_table(aggr->lookups); 985 kfree(aggr->lookups->dev_id); 986 err_remove_swnode: 987 fwnode_remove_software_node(swnode); 988 err_remove_lookups: 989 kfree(aggr->lookups); 990 991 return ret; 992 } 993 994 static void gpio_aggregator_deactivate(struct gpio_aggregator *aggr) 995 { 996 struct fwnode_handle *swnode; 997 998 swnode = dev_fwnode(&aggr->pdev->dev); 999 platform_device_unregister(aggr->pdev); 1000 aggr->pdev = NULL; 1001 gpiod_remove_lookup_table(aggr->lookups); 1002 kfree(aggr->lookups->dev_id); 1003 kfree(aggr->lookups); 1004 fwnode_remove_software_node(swnode); 1005 } 1006 1007 static void gpio_aggregator_lockup_configfs(struct gpio_aggregator *aggr, 1008 bool lock) 1009 { 1010 struct configfs_subsystem *subsys = aggr->group.cg_subsys; 1011 struct gpio_aggregator_line *line; 1012 1013 /* 1014 * The device only needs to depend on leaf lines. This is 1015 * sufficient to lock up all the configfs entries that the 1016 * instantiated, alive device depends on. 1017 */ 1018 list_for_each_entry(line, &aggr->list_head, entry) { 1019 if (lock) 1020 configfs_depend_item_unlocked( 1021 subsys, &line->group.cg_item); 1022 else 1023 configfs_undepend_item_unlocked( 1024 &line->group.cg_item); 1025 } 1026 } 1027 1028 static ssize_t 1029 gpio_aggregator_line_key_show(struct config_item *item, char *page) 1030 { 1031 struct gpio_aggregator_line *line = to_gpio_aggregator_line(item); 1032 struct gpio_aggregator *aggr = line->parent; 1033 1034 guard(mutex)(&aggr->lock); 1035 1036 return sysfs_emit(page, "%s\n", line->key ?: ""); 1037 } 1038 1039 static ssize_t 1040 gpio_aggregator_line_key_store(struct config_item *item, const char *page, 1041 size_t count) 1042 { 1043 struct gpio_aggregator_line *line = to_gpio_aggregator_line(item); 1044 struct gpio_aggregator *aggr = line->parent; 1045 1046 char *key __free(kfree) = kstrndup(skip_spaces(page), count, 1047 GFP_KERNEL); 1048 if (!key) 1049 return -ENOMEM; 1050 1051 strim(key); 1052 1053 guard(mutex)(&aggr->lock); 1054 1055 if (gpio_aggregator_is_activating(aggr) || 1056 gpio_aggregator_is_active(aggr)) 1057 return -EBUSY; 1058 1059 kfree(line->key); 1060 line->key = no_free_ptr(key); 1061 1062 return count; 1063 } 1064 CONFIGFS_ATTR(gpio_aggregator_line_, key); 1065 1066 static ssize_t 1067 gpio_aggregator_line_name_show(struct config_item *item, char *page) 1068 { 1069 struct gpio_aggregator_line *line = to_gpio_aggregator_line(item); 1070 struct gpio_aggregator *aggr = line->parent; 1071 1072 guard(mutex)(&aggr->lock); 1073 1074 return sysfs_emit(page, "%s\n", line->name ?: ""); 1075 } 1076 1077 static ssize_t 1078 gpio_aggregator_line_name_store(struct config_item *item, const char *page, 1079 size_t count) 1080 { 1081 struct gpio_aggregator_line *line = to_gpio_aggregator_line(item); 1082 struct gpio_aggregator *aggr = line->parent; 1083 1084 char *name __free(kfree) = kstrndup(skip_spaces(page), count, 1085 GFP_KERNEL); 1086 if (!name) 1087 return -ENOMEM; 1088 1089 strim(name); 1090 1091 guard(mutex)(&aggr->lock); 1092 1093 if (gpio_aggregator_is_activating(aggr) || 1094 gpio_aggregator_is_active(aggr)) 1095 return -EBUSY; 1096 1097 kfree(line->name); 1098 line->name = no_free_ptr(name); 1099 1100 return count; 1101 } 1102 CONFIGFS_ATTR(gpio_aggregator_line_, name); 1103 1104 static ssize_t 1105 gpio_aggregator_line_offset_show(struct config_item *item, char *page) 1106 { 1107 struct gpio_aggregator_line *line = to_gpio_aggregator_line(item); 1108 struct gpio_aggregator *aggr = line->parent; 1109 1110 guard(mutex)(&aggr->lock); 1111 1112 return sysfs_emit(page, "%d\n", line->offset); 1113 } 1114 1115 static ssize_t 1116 gpio_aggregator_line_offset_store(struct config_item *item, const char *page, 1117 size_t count) 1118 { 1119 struct gpio_aggregator_line *line = to_gpio_aggregator_line(item); 1120 struct gpio_aggregator *aggr = line->parent; 1121 int offset, ret; 1122 1123 ret = kstrtoint(page, 0, &offset); 1124 if (ret) 1125 return ret; 1126 1127 /* 1128 * When offset == -1, 'key' represents a line name to lookup. 1129 * When 0 <= offset < 65535, 'key' represents the label of the chip with 1130 * the 'offset' value representing the line within that chip. 1131 * 1132 * GPIOLIB uses the U16_MAX value to indicate lookup by line name so 1133 * the greatest offset we can accept is (U16_MAX - 1). 1134 */ 1135 if (offset > (U16_MAX - 1) || offset < -1) 1136 return -EINVAL; 1137 1138 guard(mutex)(&aggr->lock); 1139 1140 if (gpio_aggregator_is_activating(aggr) || 1141 gpio_aggregator_is_active(aggr)) 1142 return -EBUSY; 1143 1144 line->offset = offset; 1145 1146 return count; 1147 } 1148 CONFIGFS_ATTR(gpio_aggregator_line_, offset); 1149 1150 static struct configfs_attribute *gpio_aggregator_line_attrs[] = { 1151 &gpio_aggregator_line_attr_key, 1152 &gpio_aggregator_line_attr_name, 1153 &gpio_aggregator_line_attr_offset, 1154 NULL 1155 }; 1156 1157 static ssize_t 1158 gpio_aggregator_device_dev_name_show(struct config_item *item, char *page) 1159 { 1160 struct gpio_aggregator *aggr = to_gpio_aggregator(item); 1161 struct platform_device *pdev; 1162 1163 guard(mutex)(&aggr->lock); 1164 1165 pdev = aggr->pdev; 1166 if (pdev) 1167 return sysfs_emit(page, "%s\n", dev_name(&pdev->dev)); 1168 1169 return sysfs_emit(page, "%s.%d\n", DRV_NAME, aggr->id); 1170 } 1171 CONFIGFS_ATTR_RO(gpio_aggregator_device_, dev_name); 1172 1173 static ssize_t 1174 gpio_aggregator_device_live_show(struct config_item *item, char *page) 1175 { 1176 struct gpio_aggregator *aggr = to_gpio_aggregator(item); 1177 1178 guard(mutex)(&aggr->lock); 1179 1180 return sysfs_emit(page, "%c\n", 1181 gpio_aggregator_is_active(aggr) ? '1' : '0'); 1182 } 1183 1184 static ssize_t 1185 gpio_aggregator_device_live_store(struct config_item *item, const char *page, 1186 size_t count) 1187 { 1188 struct gpio_aggregator *aggr = to_gpio_aggregator(item); 1189 int ret = 0; 1190 bool live; 1191 1192 ret = kstrtobool(page, &live); 1193 if (ret) 1194 return ret; 1195 1196 if (!try_module_get(THIS_MODULE)) 1197 return -ENOENT; 1198 1199 if (live && !aggr->init_via_sysfs) 1200 gpio_aggregator_lockup_configfs(aggr, true); 1201 1202 scoped_guard(mutex, &aggr->lock) { 1203 if (gpio_aggregator_is_activating(aggr) || 1204 (live == gpio_aggregator_is_active(aggr))) 1205 ret = -EPERM; 1206 else if (live) 1207 ret = gpio_aggregator_activate(aggr); 1208 else 1209 gpio_aggregator_deactivate(aggr); 1210 } 1211 1212 /* 1213 * Undepend is required only if device disablement (live == 0) 1214 * succeeds or if device enablement (live == 1) fails. 1215 */ 1216 if (live == !!ret && !aggr->init_via_sysfs) 1217 gpio_aggregator_lockup_configfs(aggr, false); 1218 1219 module_put(THIS_MODULE); 1220 1221 return ret ?: count; 1222 } 1223 CONFIGFS_ATTR(gpio_aggregator_device_, live); 1224 1225 static struct configfs_attribute *gpio_aggregator_device_attrs[] = { 1226 &gpio_aggregator_device_attr_dev_name, 1227 &gpio_aggregator_device_attr_live, 1228 NULL 1229 }; 1230 1231 static void 1232 gpio_aggregator_line_release(struct config_item *item) 1233 { 1234 struct gpio_aggregator_line *line = to_gpio_aggregator_line(item); 1235 struct gpio_aggregator *aggr = line->parent; 1236 1237 guard(mutex)(&aggr->lock); 1238 1239 gpio_aggregator_line_del(aggr, line); 1240 kfree(line->key); 1241 kfree(line->name); 1242 kfree(line); 1243 } 1244 1245 static const struct configfs_item_operations gpio_aggregator_line_item_ops = { 1246 .release = gpio_aggregator_line_release, 1247 }; 1248 1249 static const struct config_item_type gpio_aggregator_line_type = { 1250 .ct_item_ops = &gpio_aggregator_line_item_ops, 1251 .ct_attrs = gpio_aggregator_line_attrs, 1252 .ct_owner = THIS_MODULE, 1253 }; 1254 1255 static void gpio_aggregator_device_release(struct config_item *item) 1256 { 1257 struct gpio_aggregator *aggr = to_gpio_aggregator(item); 1258 1259 /* 1260 * At this point, aggr is neither active nor activating, 1261 * so calling gpio_aggregator_deactivate() is always unnecessary. 1262 */ 1263 gpio_aggregator_free(aggr); 1264 } 1265 1266 static const struct configfs_item_operations gpio_aggregator_device_item_ops = { 1267 .release = gpio_aggregator_device_release, 1268 }; 1269 1270 static struct config_group * 1271 gpio_aggregator_device_make_group(struct config_group *group, const char *name) 1272 { 1273 struct gpio_aggregator *aggr = to_gpio_aggregator(&group->cg_item); 1274 struct gpio_aggregator_line *line; 1275 unsigned int idx; 1276 int ret, nchar; 1277 1278 ret = sscanf(name, "line%u%n", &idx, &nchar); 1279 if (ret != 1 || nchar != strlen(name)) 1280 return ERR_PTR(-EINVAL); 1281 1282 if (aggr->init_via_sysfs) 1283 /* 1284 * Aggregators created via legacy sysfs interface are exposed as 1285 * default groups, which means rmdir(2) is prohibited for them. 1286 * For simplicity, and to avoid confusion, we also prohibit 1287 * mkdir(2). 1288 */ 1289 return ERR_PTR(-EPERM); 1290 1291 guard(mutex)(&aggr->lock); 1292 1293 if (gpio_aggregator_is_active(aggr)) 1294 return ERR_PTR(-EBUSY); 1295 1296 list_for_each_entry(line, &aggr->list_head, entry) 1297 if (line->idx == idx) 1298 return ERR_PTR(-EINVAL); 1299 1300 line = gpio_aggregator_line_alloc(aggr, idx, NULL, -1); 1301 if (IS_ERR(line)) 1302 return ERR_CAST(line); 1303 1304 config_group_init_type_name(&line->group, name, &gpio_aggregator_line_type); 1305 1306 gpio_aggregator_line_add(aggr, line); 1307 1308 return &line->group; 1309 } 1310 1311 static const struct configfs_group_operations gpio_aggregator_device_group_ops = { 1312 .make_group = gpio_aggregator_device_make_group, 1313 }; 1314 1315 static const struct config_item_type gpio_aggregator_device_type = { 1316 .ct_group_ops = &gpio_aggregator_device_group_ops, 1317 .ct_item_ops = &gpio_aggregator_device_item_ops, 1318 .ct_attrs = gpio_aggregator_device_attrs, 1319 .ct_owner = THIS_MODULE, 1320 }; 1321 1322 static struct config_group * 1323 gpio_aggregator_make_group(struct config_group *group, const char *name) 1324 { 1325 struct gpio_aggregator *aggr; 1326 int ret; 1327 1328 /* 1329 * "_sysfs" prefix is reserved for auto-generated config group 1330 * for devices create via legacy sysfs interface. 1331 */ 1332 if (strncmp(name, AGGREGATOR_LEGACY_PREFIX, 1333 sizeof(AGGREGATOR_LEGACY_PREFIX) - 1) == 0) 1334 return ERR_PTR(-EINVAL); 1335 1336 /* arg space is unneeded */ 1337 ret = gpio_aggregator_alloc(&aggr, 0); 1338 if (ret) 1339 return ERR_PTR(ret); 1340 1341 config_group_init_type_name(&aggr->group, name, &gpio_aggregator_device_type); 1342 1343 return &aggr->group; 1344 } 1345 1346 static const struct configfs_group_operations gpio_aggregator_group_ops = { 1347 .make_group = gpio_aggregator_make_group, 1348 }; 1349 1350 static const struct config_item_type gpio_aggregator_type = { 1351 .ct_group_ops = &gpio_aggregator_group_ops, 1352 .ct_owner = THIS_MODULE, 1353 }; 1354 1355 static struct configfs_subsystem gpio_aggregator_subsys = { 1356 .su_group = { 1357 .cg_item = { 1358 .ci_namebuf = DRV_NAME, 1359 .ci_type = &gpio_aggregator_type, 1360 }, 1361 }, 1362 }; 1363 1364 /* 1365 * Sysfs interface 1366 */ 1367 static int gpio_aggregator_parse(struct gpio_aggregator *aggr) 1368 { 1369 char *args = skip_spaces(aggr->args); 1370 struct gpio_aggregator_line *line; 1371 char name[CONFIGFS_ITEM_NAME_LEN]; 1372 char *key, *offsets, *p; 1373 unsigned int i, n = 0; 1374 int error = 0; 1375 1376 unsigned long *bitmap __free(bitmap) = 1377 bitmap_alloc(AGGREGATOR_MAX_GPIOS, GFP_KERNEL); 1378 if (!bitmap) 1379 return -ENOMEM; 1380 1381 args = next_arg(args, &key, &p); 1382 while (*args) { 1383 args = next_arg(args, &offsets, &p); 1384 1385 p = get_options(offsets, 0, &error); 1386 if (error == 0 || *p) { 1387 /* Named GPIO line */ 1388 scnprintf(name, sizeof(name), "line%u", n); 1389 line = gpio_aggregator_line_alloc(aggr, n, key, -1); 1390 if (IS_ERR(line)) { 1391 error = PTR_ERR(line); 1392 goto err; 1393 } 1394 config_group_init_type_name(&line->group, name, 1395 &gpio_aggregator_line_type); 1396 error = configfs_register_group(&aggr->group, 1397 &line->group); 1398 if (error) 1399 goto err; 1400 scoped_guard(mutex, &aggr->lock) 1401 gpio_aggregator_line_add(aggr, line); 1402 1403 error = gpio_aggregator_add_gpio(aggr, key, U16_MAX, &n); 1404 if (error) 1405 goto err; 1406 1407 key = offsets; 1408 continue; 1409 } 1410 1411 /* GPIO chip + offset(s) */ 1412 error = bitmap_parselist(offsets, bitmap, AGGREGATOR_MAX_GPIOS); 1413 if (error) { 1414 pr_err("Cannot parse %s: %d\n", offsets, error); 1415 goto err; 1416 } 1417 1418 for_each_set_bit(i, bitmap, AGGREGATOR_MAX_GPIOS) { 1419 scnprintf(name, sizeof(name), "line%u", n); 1420 line = gpio_aggregator_line_alloc(aggr, n, key, i); 1421 if (IS_ERR(line)) { 1422 error = PTR_ERR(line); 1423 goto err; 1424 } 1425 config_group_init_type_name(&line->group, name, 1426 &gpio_aggregator_line_type); 1427 error = configfs_register_group(&aggr->group, 1428 &line->group); 1429 if (error) 1430 goto err; 1431 scoped_guard(mutex, &aggr->lock) 1432 gpio_aggregator_line_add(aggr, line); 1433 1434 error = gpio_aggregator_add_gpio(aggr, key, i, &n); 1435 if (error) 1436 goto err; 1437 } 1438 1439 args = next_arg(args, &key, &p); 1440 } 1441 1442 if (!n) { 1443 pr_err("No GPIOs specified\n"); 1444 error = -EINVAL; 1445 goto err; 1446 } 1447 1448 return 0; 1449 1450 err: 1451 gpio_aggregator_free_lines(aggr); 1452 return error; 1453 } 1454 1455 static ssize_t gpio_aggregator_new_device_store(struct device_driver *driver, 1456 const char *buf, size_t count) 1457 { 1458 struct gpio_aggregator_pdev_meta meta = { .init_via_sysfs = true }; 1459 char name[CONFIGFS_ITEM_NAME_LEN]; 1460 struct gpio_aggregator *aggr; 1461 struct platform_device *pdev; 1462 int res; 1463 1464 if (!try_module_get(THIS_MODULE)) 1465 return -ENOENT; 1466 1467 /* kernfs guarantees string termination, so count + 1 is safe */ 1468 res = gpio_aggregator_alloc(&aggr, count + 1); 1469 if (res) 1470 goto put_module; 1471 1472 memcpy(aggr->args, buf, count + 1); 1473 1474 aggr->init_via_sysfs = true; 1475 aggr->lookups = kzalloc_flex(*aggr->lookups, table, 1); 1476 if (!aggr->lookups) { 1477 res = -ENOMEM; 1478 goto free_ga; 1479 } 1480 1481 aggr->lookups->dev_id = kasprintf(GFP_KERNEL, "%s.%d", DRV_NAME, aggr->id); 1482 if (!aggr->lookups->dev_id) { 1483 res = -ENOMEM; 1484 goto free_table; 1485 } 1486 1487 scnprintf(name, sizeof(name), "%s.%d", AGGREGATOR_LEGACY_PREFIX, aggr->id); 1488 config_group_init_type_name(&aggr->group, name, &gpio_aggregator_device_type); 1489 1490 /* Expose to configfs */ 1491 res = configfs_register_group(&gpio_aggregator_subsys.su_group, 1492 &aggr->group); 1493 if (res) 1494 goto free_dev_id; 1495 1496 res = gpio_aggregator_parse(aggr); 1497 if (res) 1498 goto unregister_group; 1499 1500 gpiod_add_lookup_table(aggr->lookups); 1501 1502 pdev = platform_device_register_data(NULL, DRV_NAME, aggr->id, &meta, sizeof(meta)); 1503 if (IS_ERR(pdev)) { 1504 res = PTR_ERR(pdev); 1505 goto remove_table; 1506 } 1507 1508 aggr->pdev = pdev; 1509 module_put(THIS_MODULE); 1510 return count; 1511 1512 remove_table: 1513 gpiod_remove_lookup_table(aggr->lookups); 1514 unregister_group: 1515 configfs_unregister_group(&aggr->group); 1516 free_dev_id: 1517 kfree(aggr->lookups->dev_id); 1518 free_table: 1519 kfree(aggr->lookups); 1520 free_ga: 1521 gpio_aggregator_free(aggr); 1522 put_module: 1523 module_put(THIS_MODULE); 1524 return res; 1525 } 1526 1527 static struct driver_attribute driver_attr_gpio_aggregator_new_device = 1528 __ATTR(new_device, 0200, NULL, gpio_aggregator_new_device_store); 1529 1530 static void gpio_aggregator_destroy(struct gpio_aggregator *aggr) 1531 { 1532 scoped_guard(mutex, &aggr->lock) { 1533 if (gpio_aggregator_is_activating(aggr) || 1534 gpio_aggregator_is_active(aggr)) 1535 gpio_aggregator_deactivate(aggr); 1536 } 1537 gpio_aggregator_free_lines(aggr); 1538 configfs_unregister_group(&aggr->group); 1539 kfree(aggr); 1540 } 1541 1542 static ssize_t gpio_aggregator_delete_device_store(struct device_driver *driver, 1543 const char *buf, size_t count) 1544 { 1545 struct gpio_aggregator *aggr; 1546 unsigned int id; 1547 int error; 1548 1549 if (!str_has_prefix(buf, DRV_NAME ".")) 1550 return -EINVAL; 1551 1552 error = kstrtouint(buf + strlen(DRV_NAME "."), 10, &id); 1553 if (error) 1554 return error; 1555 1556 if (!try_module_get(THIS_MODULE)) 1557 return -ENOENT; 1558 1559 mutex_lock(&gpio_aggregator_lock); 1560 aggr = idr_find(&gpio_aggregator_idr, id); 1561 /* 1562 * For simplicity, devices created via configfs cannot be deleted 1563 * via sysfs. 1564 */ 1565 if (aggr && aggr->init_via_sysfs) 1566 idr_remove(&gpio_aggregator_idr, id); 1567 else { 1568 mutex_unlock(&gpio_aggregator_lock); 1569 module_put(THIS_MODULE); 1570 return -ENOENT; 1571 } 1572 mutex_unlock(&gpio_aggregator_lock); 1573 1574 gpio_aggregator_destroy(aggr); 1575 module_put(THIS_MODULE); 1576 return count; 1577 } 1578 1579 static struct driver_attribute driver_attr_gpio_aggregator_delete_device = 1580 __ATTR(delete_device, 0200, NULL, gpio_aggregator_delete_device_store); 1581 1582 static struct attribute *gpio_aggregator_attrs[] = { 1583 &driver_attr_gpio_aggregator_new_device.attr, 1584 &driver_attr_gpio_aggregator_delete_device.attr, 1585 NULL 1586 }; 1587 ATTRIBUTE_GROUPS(gpio_aggregator); 1588 1589 /* 1590 * GPIO Aggregator platform device 1591 */ 1592 1593 static int gpio_aggregator_probe(struct platform_device *pdev) 1594 { 1595 struct gpio_aggregator_pdev_meta *meta; 1596 struct device *dev = &pdev->dev; 1597 bool init_via_sysfs = false; 1598 struct gpio_desc **descs; 1599 struct gpiochip_fwd *fwd; 1600 unsigned long features; 1601 int i, n; 1602 1603 n = gpiod_count(dev, NULL); 1604 if (n < 0) 1605 return n; 1606 1607 descs = devm_kmalloc_array(dev, n, sizeof(*descs), GFP_KERNEL); 1608 if (!descs) 1609 return -ENOMEM; 1610 1611 meta = dev_get_platdata(&pdev->dev); 1612 if (meta && meta->init_via_sysfs) 1613 init_via_sysfs = true; 1614 1615 for (i = 0; i < n; i++) { 1616 descs[i] = devm_gpiod_get_index(dev, NULL, i, GPIOD_ASIS); 1617 if (IS_ERR(descs[i])) { 1618 /* 1619 * Deferred probing is not suitable when the aggregator 1620 * is created via configfs. They should just retry later 1621 * whenever they like. For device creation via sysfs, 1622 * error is propagated without overriding for backward 1623 * compatibility. .prevent_deferred_probe is kept unset 1624 * for other cases. 1625 */ 1626 if (!init_via_sysfs && !dev_of_node(dev) && 1627 descs[i] == ERR_PTR(-EPROBE_DEFER)) { 1628 pr_warn("Deferred probe canceled for creation via configfs.\n"); 1629 return -ENODEV; 1630 } 1631 return PTR_ERR(descs[i]); 1632 } 1633 } 1634 1635 features = (uintptr_t)device_get_match_data(dev); 1636 fwd = gpiochip_fwd_create(dev, n, descs, features); 1637 if (IS_ERR(fwd)) 1638 return PTR_ERR(fwd); 1639 1640 platform_set_drvdata(pdev, fwd); 1641 devm_kfree(dev, descs); 1642 return 0; 1643 } 1644 1645 static const struct of_device_id gpio_aggregator_dt_ids[] = { 1646 { 1647 .compatible = "gpio-delay", 1648 .data = (void *)FWD_FEATURE_DELAY, 1649 }, 1650 /* 1651 * Add GPIO-operated devices controlled from userspace below, 1652 * or use "driver_override" in sysfs. 1653 */ 1654 {} 1655 }; 1656 MODULE_DEVICE_TABLE(of, gpio_aggregator_dt_ids); 1657 1658 static struct platform_driver gpio_aggregator_driver = { 1659 .probe = gpio_aggregator_probe, 1660 .driver = { 1661 .name = DRV_NAME, 1662 .groups = gpio_aggregator_groups, 1663 .of_match_table = gpio_aggregator_dt_ids, 1664 }, 1665 }; 1666 1667 static int __exit gpio_aggregator_idr_remove(int id, void *p, void *data) 1668 { 1669 /* 1670 * There should be no aggregator created via configfs, as their 1671 * presence would prevent module unloading. 1672 */ 1673 gpio_aggregator_destroy(p); 1674 return 0; 1675 } 1676 1677 static void __exit gpio_aggregator_remove_all(void) 1678 { 1679 /* 1680 * Configfs callbacks acquire gpio_aggregator_lock when accessing 1681 * gpio_aggregator_idr, so to prevent lock inversion deadlock, we 1682 * cannot protect idr_for_each invocation here with 1683 * gpio_aggregator_lock, as gpio_aggregator_idr_remove() accesses 1684 * configfs groups. Fortunately, the new_device/delete_device path 1685 * and the module unload path are mutually exclusive, thanks to an 1686 * explicit try_module_get inside of those driver attr handlers. 1687 * Also, when we reach here, no configfs entries present or being 1688 * created. Therefore, no need to protect with gpio_aggregator_lock 1689 * below. 1690 */ 1691 idr_for_each(&gpio_aggregator_idr, gpio_aggregator_idr_remove, NULL); 1692 idr_destroy(&gpio_aggregator_idr); 1693 } 1694 1695 static int __init gpio_aggregator_init(void) 1696 { 1697 int ret = 0; 1698 1699 config_group_init(&gpio_aggregator_subsys.su_group); 1700 mutex_init(&gpio_aggregator_subsys.su_mutex); 1701 ret = configfs_register_subsystem(&gpio_aggregator_subsys); 1702 if (ret) { 1703 pr_err("Failed to register the '%s' configfs subsystem: %d\n", 1704 gpio_aggregator_subsys.su_group.cg_item.ci_namebuf, ret); 1705 mutex_destroy(&gpio_aggregator_subsys.su_mutex); 1706 return ret; 1707 } 1708 1709 /* 1710 * CAVEAT: This must occur after configfs registration. Otherwise, 1711 * a race condition could arise: driver attribute groups might be 1712 * exposed and accessed by users before configfs registration 1713 * completes. new_device_store() does not expect a partially 1714 * initialized configfs state. 1715 */ 1716 ret = platform_driver_register(&gpio_aggregator_driver); 1717 if (ret) { 1718 pr_err("Failed to register the platform driver: %d\n", ret); 1719 mutex_destroy(&gpio_aggregator_subsys.su_mutex); 1720 configfs_unregister_subsystem(&gpio_aggregator_subsys); 1721 } 1722 1723 return ret; 1724 } 1725 module_init(gpio_aggregator_init); 1726 1727 static void __exit gpio_aggregator_exit(void) 1728 { 1729 gpio_aggregator_remove_all(); 1730 platform_driver_unregister(&gpio_aggregator_driver); 1731 configfs_unregister_subsystem(&gpio_aggregator_subsys); 1732 } 1733 module_exit(gpio_aggregator_exit); 1734 1735 MODULE_AUTHOR("Geert Uytterhoeven <geert+renesas@glider.be>"); 1736 MODULE_DESCRIPTION("GPIO Aggregator"); 1737 MODULE_LICENSE("GPL v2"); 1738