| .. | .. |
|---|
| 11 | 11 | #include <linux/device.h> |
|---|
| 12 | 12 | #include <linux/kref.h> |
|---|
| 13 | 13 | #include <linux/list.h> |
|---|
| 14 | | -#include <linux/module.h> |
|---|
| 15 | 14 | #include <linux/mutex.h> |
|---|
| 16 | 15 | #include <linux/slab.h> |
|---|
| 17 | 16 | #include <linux/debugfs.h> |
|---|
| 17 | + |
|---|
| 18 | +/** |
|---|
| 19 | + * DOC: overview |
|---|
| 20 | + * |
|---|
| 21 | + * The component helper allows drivers to collect a pile of sub-devices, |
|---|
| 22 | + * including their bound drivers, into an aggregate driver. Various subsystems |
|---|
| 23 | + * already provide functions to get hold of such components, e.g. |
|---|
| 24 | + * of_clk_get_by_name(). The component helper can be used when such a |
|---|
| 25 | + * subsystem-specific way to find a device is not available: The component |
|---|
| 26 | + * helper fills the niche of aggregate drivers for specific hardware, where |
|---|
| 27 | + * further standardization into a subsystem would not be practical. The common |
|---|
| 28 | + * example is when a logical device (e.g. a DRM display driver) is spread around |
|---|
| 29 | + * the SoC on various components (scanout engines, blending blocks, transcoders |
|---|
| 30 | + * for various outputs and so on). |
|---|
| 31 | + * |
|---|
| 32 | + * The component helper also doesn't solve runtime dependencies, e.g. for system |
|---|
| 33 | + * suspend and resume operations. See also :ref:`device links<device_link>`. |
|---|
| 34 | + * |
|---|
| 35 | + * Components are registered using component_add() and unregistered with |
|---|
| 36 | + * component_del(), usually from the driver's probe and disconnect functions. |
|---|
| 37 | + * |
|---|
| 38 | + * Aggregate drivers first assemble a component match list of what they need |
|---|
| 39 | + * using component_match_add(). This is then registered as an aggregate driver |
|---|
| 40 | + * using component_master_add_with_match(), and unregistered using |
|---|
| 41 | + * component_master_del(). |
|---|
| 42 | + */ |
|---|
| 18 | 43 | |
|---|
| 19 | 44 | struct component; |
|---|
| 20 | 45 | |
|---|
| 21 | 46 | struct component_match_array { |
|---|
| 22 | 47 | void *data; |
|---|
| 23 | 48 | int (*compare)(struct device *, void *); |
|---|
| 49 | + int (*compare_typed)(struct device *, int, void *); |
|---|
| 24 | 50 | void (*release)(struct device *, void *); |
|---|
| 25 | 51 | struct component *component; |
|---|
| 26 | 52 | bool duplicate; |
|---|
| .. | .. |
|---|
| 48 | 74 | bool bound; |
|---|
| 49 | 75 | |
|---|
| 50 | 76 | const struct component_ops *ops; |
|---|
| 77 | + int subcomponent; |
|---|
| 51 | 78 | struct device *dev; |
|---|
| 52 | 79 | }; |
|---|
| 53 | 80 | |
|---|
| .. | .. |
|---|
| 85 | 112 | return 0; |
|---|
| 86 | 113 | } |
|---|
| 87 | 114 | |
|---|
| 88 | | -static int component_devices_open(struct inode *inode, struct file *file) |
|---|
| 89 | | -{ |
|---|
| 90 | | - return single_open(file, component_devices_show, inode->i_private); |
|---|
| 91 | | -} |
|---|
| 92 | | - |
|---|
| 93 | | -static const struct file_operations component_devices_fops = { |
|---|
| 94 | | - .open = component_devices_open, |
|---|
| 95 | | - .read = seq_read, |
|---|
| 96 | | - .llseek = seq_lseek, |
|---|
| 97 | | - .release = single_release, |
|---|
| 98 | | -}; |
|---|
| 115 | +DEFINE_SHOW_ATTRIBUTE(component_devices); |
|---|
| 99 | 116 | |
|---|
| 100 | 117 | static int __init component_debug_init(void) |
|---|
| 101 | 118 | { |
|---|
| .. | .. |
|---|
| 142 | 159 | } |
|---|
| 143 | 160 | |
|---|
| 144 | 161 | static struct component *find_component(struct master *master, |
|---|
| 145 | | - int (*compare)(struct device *, void *), void *compare_data) |
|---|
| 162 | + struct component_match_array *mc) |
|---|
| 146 | 163 | { |
|---|
| 147 | 164 | struct component *c; |
|---|
| 148 | 165 | |
|---|
| .. | .. |
|---|
| 150 | 167 | if (c->master && c->master != master) |
|---|
| 151 | 168 | continue; |
|---|
| 152 | 169 | |
|---|
| 153 | | - if (compare(c->dev, compare_data)) |
|---|
| 170 | + if (mc->compare && mc->compare(c->dev, mc->data)) |
|---|
| 171 | + return c; |
|---|
| 172 | + |
|---|
| 173 | + if (mc->compare_typed && |
|---|
| 174 | + mc->compare_typed(c->dev, c->subcomponent, mc->data)) |
|---|
| 154 | 175 | return c; |
|---|
| 155 | 176 | } |
|---|
| 156 | 177 | |
|---|
| .. | .. |
|---|
| 176 | 197 | if (match->compare[i].component) |
|---|
| 177 | 198 | continue; |
|---|
| 178 | 199 | |
|---|
| 179 | | - c = find_component(master, mc->compare, mc->data); |
|---|
| 200 | + c = find_component(master, mc); |
|---|
| 180 | 201 | if (!c) { |
|---|
| 181 | 202 | ret = -ENXIO; |
|---|
| 182 | 203 | break; |
|---|
| .. | .. |
|---|
| 312 | 333 | return 0; |
|---|
| 313 | 334 | } |
|---|
| 314 | 335 | |
|---|
| 315 | | -/* |
|---|
| 316 | | - * Add a component to be matched, with a release function. |
|---|
| 317 | | - * |
|---|
| 318 | | - * The match array is first created or extended if necessary. |
|---|
| 319 | | - */ |
|---|
| 320 | | -void component_match_add_release(struct device *master, |
|---|
| 336 | +static void __component_match_add(struct device *master, |
|---|
| 321 | 337 | struct component_match **matchptr, |
|---|
| 322 | 338 | void (*release)(struct device *, void *), |
|---|
| 323 | | - int (*compare)(struct device *, void *), void *compare_data) |
|---|
| 339 | + int (*compare)(struct device *, void *), |
|---|
| 340 | + int (*compare_typed)(struct device *, int, void *), |
|---|
| 341 | + void *compare_data) |
|---|
| 324 | 342 | { |
|---|
| 325 | 343 | struct component_match *match = *matchptr; |
|---|
| 326 | 344 | |
|---|
| .. | .. |
|---|
| 352 | 370 | } |
|---|
| 353 | 371 | |
|---|
| 354 | 372 | match->compare[match->num].compare = compare; |
|---|
| 373 | + match->compare[match->num].compare_typed = compare_typed; |
|---|
| 355 | 374 | match->compare[match->num].release = release; |
|---|
| 356 | 375 | match->compare[match->num].data = compare_data; |
|---|
| 357 | 376 | match->compare[match->num].component = NULL; |
|---|
| 358 | 377 | match->num++; |
|---|
| 359 | 378 | } |
|---|
| 379 | + |
|---|
| 380 | +/** |
|---|
| 381 | + * component_match_add_release - add a component match entry with release callback |
|---|
| 382 | + * @master: device with the aggregate driver |
|---|
| 383 | + * @matchptr: pointer to the list of component matches |
|---|
| 384 | + * @release: release function for @compare_data |
|---|
| 385 | + * @compare: compare function to match against all components |
|---|
| 386 | + * @compare_data: opaque pointer passed to the @compare function |
|---|
| 387 | + * |
|---|
| 388 | + * Adds a new component match to the list stored in @matchptr, which the @master |
|---|
| 389 | + * aggregate driver needs to function. The list of component matches pointed to |
|---|
| 390 | + * by @matchptr must be initialized to NULL before adding the first match. This |
|---|
| 391 | + * only matches against components added with component_add(). |
|---|
| 392 | + * |
|---|
| 393 | + * The allocated match list in @matchptr is automatically released using devm |
|---|
| 394 | + * actions, where upon @release will be called to free any references held by |
|---|
| 395 | + * @compare_data, e.g. when @compare_data is a &device_node that must be |
|---|
| 396 | + * released with of_node_put(). |
|---|
| 397 | + * |
|---|
| 398 | + * See also component_match_add() and component_match_add_typed(). |
|---|
| 399 | + */ |
|---|
| 400 | +void component_match_add_release(struct device *master, |
|---|
| 401 | + struct component_match **matchptr, |
|---|
| 402 | + void (*release)(struct device *, void *), |
|---|
| 403 | + int (*compare)(struct device *, void *), void *compare_data) |
|---|
| 404 | +{ |
|---|
| 405 | + __component_match_add(master, matchptr, release, compare, NULL, |
|---|
| 406 | + compare_data); |
|---|
| 407 | +} |
|---|
| 360 | 408 | EXPORT_SYMBOL(component_match_add_release); |
|---|
| 409 | + |
|---|
| 410 | +/** |
|---|
| 411 | + * component_match_add_typed - add a component match entry for a typed component |
|---|
| 412 | + * @master: device with the aggregate driver |
|---|
| 413 | + * @matchptr: pointer to the list of component matches |
|---|
| 414 | + * @compare_typed: compare function to match against all typed components |
|---|
| 415 | + * @compare_data: opaque pointer passed to the @compare function |
|---|
| 416 | + * |
|---|
| 417 | + * Adds a new component match to the list stored in @matchptr, which the @master |
|---|
| 418 | + * aggregate driver needs to function. The list of component matches pointed to |
|---|
| 419 | + * by @matchptr must be initialized to NULL before adding the first match. This |
|---|
| 420 | + * only matches against components added with component_add_typed(). |
|---|
| 421 | + * |
|---|
| 422 | + * The allocated match list in @matchptr is automatically released using devm |
|---|
| 423 | + * actions. |
|---|
| 424 | + * |
|---|
| 425 | + * See also component_match_add_release() and component_match_add_typed(). |
|---|
| 426 | + */ |
|---|
| 427 | +void component_match_add_typed(struct device *master, |
|---|
| 428 | + struct component_match **matchptr, |
|---|
| 429 | + int (*compare_typed)(struct device *, int, void *), void *compare_data) |
|---|
| 430 | +{ |
|---|
| 431 | + __component_match_add(master, matchptr, NULL, NULL, compare_typed, |
|---|
| 432 | + compare_data); |
|---|
| 433 | +} |
|---|
| 434 | +EXPORT_SYMBOL(component_match_add_typed); |
|---|
| 361 | 435 | |
|---|
| 362 | 436 | static void free_master(struct master *master) |
|---|
| 363 | 437 | { |
|---|
| .. | .. |
|---|
| 378 | 452 | kfree(master); |
|---|
| 379 | 453 | } |
|---|
| 380 | 454 | |
|---|
| 455 | +/** |
|---|
| 456 | + * component_master_add_with_match - register an aggregate driver |
|---|
| 457 | + * @dev: device with the aggregate driver |
|---|
| 458 | + * @ops: callbacks for the aggregate driver |
|---|
| 459 | + * @match: component match list for the aggregate driver |
|---|
| 460 | + * |
|---|
| 461 | + * Registers a new aggregate driver consisting of the components added to @match |
|---|
| 462 | + * by calling one of the component_match_add() functions. Once all components in |
|---|
| 463 | + * @match are available, it will be assembled by calling |
|---|
| 464 | + * &component_master_ops.bind from @ops. Must be unregistered by calling |
|---|
| 465 | + * component_master_del(). |
|---|
| 466 | + */ |
|---|
| 381 | 467 | int component_master_add_with_match(struct device *dev, |
|---|
| 382 | 468 | const struct component_master_ops *ops, |
|---|
| 383 | 469 | struct component_match *match) |
|---|
| .. | .. |
|---|
| 414 | 500 | } |
|---|
| 415 | 501 | EXPORT_SYMBOL_GPL(component_master_add_with_match); |
|---|
| 416 | 502 | |
|---|
| 503 | +/** |
|---|
| 504 | + * component_master_del - unregister an aggregate driver |
|---|
| 505 | + * @dev: device with the aggregate driver |
|---|
| 506 | + * @ops: callbacks for the aggregate driver |
|---|
| 507 | + * |
|---|
| 508 | + * Unregisters an aggregate driver registered with |
|---|
| 509 | + * component_master_add_with_match(). If necessary the aggregate driver is first |
|---|
| 510 | + * disassembled by calling &component_master_ops.unbind from @ops. |
|---|
| 511 | + */ |
|---|
| 417 | 512 | void component_master_del(struct device *dev, |
|---|
| 418 | 513 | const struct component_master_ops *ops) |
|---|
| 419 | 514 | { |
|---|
| .. | .. |
|---|
| 434 | 529 | { |
|---|
| 435 | 530 | WARN_ON(!component->bound); |
|---|
| 436 | 531 | |
|---|
| 437 | | - component->ops->unbind(component->dev, master->dev, data); |
|---|
| 532 | + if (component->ops && component->ops->unbind) |
|---|
| 533 | + component->ops->unbind(component->dev, master->dev, data); |
|---|
| 438 | 534 | component->bound = false; |
|---|
| 439 | 535 | |
|---|
| 440 | 536 | /* Release all resources claimed in the binding of this component */ |
|---|
| 441 | 537 | devres_release_group(component->dev, component); |
|---|
| 442 | 538 | } |
|---|
| 443 | 539 | |
|---|
| 540 | +/** |
|---|
| 541 | + * component_unbind_all - unbind all components of an aggregate driver |
|---|
| 542 | + * @master_dev: device with the aggregate driver |
|---|
| 543 | + * @data: opaque pointer, passed to all components |
|---|
| 544 | + * |
|---|
| 545 | + * Unbinds all components of the aggregate @dev by passing @data to their |
|---|
| 546 | + * &component_ops.unbind functions. Should be called from |
|---|
| 547 | + * &component_master_ops.unbind. |
|---|
| 548 | + */ |
|---|
| 444 | 549 | void component_unbind_all(struct device *master_dev, void *data) |
|---|
| 445 | 550 | { |
|---|
| 446 | 551 | struct master *master; |
|---|
| .. | .. |
|---|
| 515 | 620 | return ret; |
|---|
| 516 | 621 | } |
|---|
| 517 | 622 | |
|---|
| 623 | +/** |
|---|
| 624 | + * component_bind_all - bind all components of an aggregate driver |
|---|
| 625 | + * @master_dev: device with the aggregate driver |
|---|
| 626 | + * @data: opaque pointer, passed to all components |
|---|
| 627 | + * |
|---|
| 628 | + * Binds all components of the aggregate @dev by passing @data to their |
|---|
| 629 | + * &component_ops.bind functions. Should be called from |
|---|
| 630 | + * &component_master_ops.bind. |
|---|
| 631 | + */ |
|---|
| 518 | 632 | int component_bind_all(struct device *master_dev, void *data) |
|---|
| 519 | 633 | { |
|---|
| 520 | 634 | struct master *master; |
|---|
| .. | .. |
|---|
| 549 | 663 | } |
|---|
| 550 | 664 | EXPORT_SYMBOL_GPL(component_bind_all); |
|---|
| 551 | 665 | |
|---|
| 552 | | -int component_add(struct device *dev, const struct component_ops *ops) |
|---|
| 666 | +static int __component_add(struct device *dev, const struct component_ops *ops, |
|---|
| 667 | + int subcomponent) |
|---|
| 553 | 668 | { |
|---|
| 554 | 669 | struct component *component; |
|---|
| 555 | 670 | int ret; |
|---|
| .. | .. |
|---|
| 560 | 675 | |
|---|
| 561 | 676 | component->ops = ops; |
|---|
| 562 | 677 | component->dev = dev; |
|---|
| 678 | + component->subcomponent = subcomponent; |
|---|
| 563 | 679 | |
|---|
| 564 | 680 | dev_dbg(dev, "adding component (ops %ps)\n", ops); |
|---|
| 565 | 681 | |
|---|
| .. | .. |
|---|
| 578 | 694 | |
|---|
| 579 | 695 | return ret < 0 ? ret : 0; |
|---|
| 580 | 696 | } |
|---|
| 697 | + |
|---|
| 698 | +/** |
|---|
| 699 | + * component_add_typed - register a component |
|---|
| 700 | + * @dev: component device |
|---|
| 701 | + * @ops: component callbacks |
|---|
| 702 | + * @subcomponent: nonzero identifier for subcomponents |
|---|
| 703 | + * |
|---|
| 704 | + * Register a new component for @dev. Functions in @ops will be call when the |
|---|
| 705 | + * aggregate driver is ready to bind the overall driver by calling |
|---|
| 706 | + * component_bind_all(). See also &struct component_ops. |
|---|
| 707 | + * |
|---|
| 708 | + * @subcomponent must be nonzero and is used to differentiate between multiple |
|---|
| 709 | + * components registerd on the same device @dev. These components are match |
|---|
| 710 | + * using component_match_add_typed(). |
|---|
| 711 | + * |
|---|
| 712 | + * The component needs to be unregistered at driver unload/disconnect by |
|---|
| 713 | + * calling component_del(). |
|---|
| 714 | + * |
|---|
| 715 | + * See also component_add(). |
|---|
| 716 | + */ |
|---|
| 717 | +int component_add_typed(struct device *dev, const struct component_ops *ops, |
|---|
| 718 | + int subcomponent) |
|---|
| 719 | +{ |
|---|
| 720 | + if (WARN_ON(subcomponent == 0)) |
|---|
| 721 | + return -EINVAL; |
|---|
| 722 | + |
|---|
| 723 | + return __component_add(dev, ops, subcomponent); |
|---|
| 724 | +} |
|---|
| 725 | +EXPORT_SYMBOL_GPL(component_add_typed); |
|---|
| 726 | + |
|---|
| 727 | +/** |
|---|
| 728 | + * component_add - register a component |
|---|
| 729 | + * @dev: component device |
|---|
| 730 | + * @ops: component callbacks |
|---|
| 731 | + * |
|---|
| 732 | + * Register a new component for @dev. Functions in @ops will be called when the |
|---|
| 733 | + * aggregate driver is ready to bind the overall driver by calling |
|---|
| 734 | + * component_bind_all(). See also &struct component_ops. |
|---|
| 735 | + * |
|---|
| 736 | + * The component needs to be unregistered at driver unload/disconnect by |
|---|
| 737 | + * calling component_del(). |
|---|
| 738 | + * |
|---|
| 739 | + * See also component_add_typed() for a variant that allows multipled different |
|---|
| 740 | + * components on the same device. |
|---|
| 741 | + */ |
|---|
| 742 | +int component_add(struct device *dev, const struct component_ops *ops) |
|---|
| 743 | +{ |
|---|
| 744 | + return __component_add(dev, ops, 0); |
|---|
| 745 | +} |
|---|
| 581 | 746 | EXPORT_SYMBOL_GPL(component_add); |
|---|
| 582 | 747 | |
|---|
| 748 | +/** |
|---|
| 749 | + * component_del - unregister a component |
|---|
| 750 | + * @dev: component device |
|---|
| 751 | + * @ops: component callbacks |
|---|
| 752 | + * |
|---|
| 753 | + * Unregister a component added with component_add(). If the component is bound |
|---|
| 754 | + * into an aggregate driver, this will force the entire aggregate driver, including |
|---|
| 755 | + * all its components, to be unbound. |
|---|
| 756 | + */ |
|---|
| 583 | 757 | void component_del(struct device *dev, const struct component_ops *ops) |
|---|
| 584 | 758 | { |
|---|
| 585 | 759 | struct component *c, *component = NULL; |
|---|
| .. | .. |
|---|
| 603 | 777 | kfree(component); |
|---|
| 604 | 778 | } |
|---|
| 605 | 779 | EXPORT_SYMBOL_GPL(component_del); |
|---|
| 606 | | - |
|---|
| 607 | | -MODULE_LICENSE("GPL v2"); |
|---|