hc
2024-05-13 9d77db3c730780c8ef5ccd4b66403ff5675cfe4e
kernel/drivers/base/component.c
....@@ -11,16 +11,42 @@
1111 #include <linux/device.h>
1212 #include <linux/kref.h>
1313 #include <linux/list.h>
14
-#include <linux/module.h>
1514 #include <linux/mutex.h>
1615 #include <linux/slab.h>
1716 #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
+ */
1843
1944 struct component;
2045
2146 struct component_match_array {
2247 void *data;
2348 int (*compare)(struct device *, void *);
49
+ int (*compare_typed)(struct device *, int, void *);
2450 void (*release)(struct device *, void *);
2551 struct component *component;
2652 bool duplicate;
....@@ -48,6 +74,7 @@
4874 bool bound;
4975
5076 const struct component_ops *ops;
77
+ int subcomponent;
5178 struct device *dev;
5279 };
5380
....@@ -85,17 +112,7 @@
85112 return 0;
86113 }
87114
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);
99116
100117 static int __init component_debug_init(void)
101118 {
....@@ -142,7 +159,7 @@
142159 }
143160
144161 static struct component *find_component(struct master *master,
145
- int (*compare)(struct device *, void *), void *compare_data)
162
+ struct component_match_array *mc)
146163 {
147164 struct component *c;
148165
....@@ -150,7 +167,11 @@
150167 if (c->master && c->master != master)
151168 continue;
152169
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))
154175 return c;
155176 }
156177
....@@ -176,7 +197,7 @@
176197 if (match->compare[i].component)
177198 continue;
178199
179
- c = find_component(master, mc->compare, mc->data);
200
+ c = find_component(master, mc);
180201 if (!c) {
181202 ret = -ENXIO;
182203 break;
....@@ -312,15 +333,12 @@
312333 return 0;
313334 }
314335
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,
321337 struct component_match **matchptr,
322338 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)
324342 {
325343 struct component_match *match = *matchptr;
326344
....@@ -352,12 +370,68 @@
352370 }
353371
354372 match->compare[match->num].compare = compare;
373
+ match->compare[match->num].compare_typed = compare_typed;
355374 match->compare[match->num].release = release;
356375 match->compare[match->num].data = compare_data;
357376 match->compare[match->num].component = NULL;
358377 match->num++;
359378 }
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
+}
360408 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);
361435
362436 static void free_master(struct master *master)
363437 {
....@@ -378,6 +452,18 @@
378452 kfree(master);
379453 }
380454
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
+ */
381467 int component_master_add_with_match(struct device *dev,
382468 const struct component_master_ops *ops,
383469 struct component_match *match)
....@@ -414,6 +500,15 @@
414500 }
415501 EXPORT_SYMBOL_GPL(component_master_add_with_match);
416502
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
+ */
417512 void component_master_del(struct device *dev,
418513 const struct component_master_ops *ops)
419514 {
....@@ -434,13 +529,23 @@
434529 {
435530 WARN_ON(!component->bound);
436531
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);
438534 component->bound = false;
439535
440536 /* Release all resources claimed in the binding of this component */
441537 devres_release_group(component->dev, component);
442538 }
443539
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
+ */
444549 void component_unbind_all(struct device *master_dev, void *data)
445550 {
446551 struct master *master;
....@@ -515,6 +620,15 @@
515620 return ret;
516621 }
517622
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
+ */
518632 int component_bind_all(struct device *master_dev, void *data)
519633 {
520634 struct master *master;
....@@ -549,7 +663,8 @@
549663 }
550664 EXPORT_SYMBOL_GPL(component_bind_all);
551665
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)
553668 {
554669 struct component *component;
555670 int ret;
....@@ -560,6 +675,7 @@
560675
561676 component->ops = ops;
562677 component->dev = dev;
678
+ component->subcomponent = subcomponent;
563679
564680 dev_dbg(dev, "adding component (ops %ps)\n", ops);
565681
....@@ -578,8 +694,66 @@
578694
579695 return ret < 0 ? ret : 0;
580696 }
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
+}
581746 EXPORT_SYMBOL_GPL(component_add);
582747
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
+ */
583757 void component_del(struct device *dev, const struct component_ops *ops)
584758 {
585759 struct component *c, *component = NULL;
....@@ -603,5 +777,3 @@
603777 kfree(component);
604778 }
605779 EXPORT_SYMBOL_GPL(component_del);
606
-
607
-MODULE_LICENSE("GPL v2");