1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef COMPONENT_H
3*4882a593Smuzhiyun #define COMPONENT_H
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun #include <linux/stddef.h>
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun struct device;
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun /**
11*4882a593Smuzhiyun * struct component_ops - callbacks for component drivers
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * Components are registered with component_add() and unregistered with
14*4882a593Smuzhiyun * component_del().
15*4882a593Smuzhiyun */
16*4882a593Smuzhiyun struct component_ops {
17*4882a593Smuzhiyun /**
18*4882a593Smuzhiyun * @bind:
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * Called through component_bind_all() when the aggregate driver is
21*4882a593Smuzhiyun * ready to bind the overall driver.
22*4882a593Smuzhiyun */
23*4882a593Smuzhiyun int (*bind)(struct device *comp, struct device *master,
24*4882a593Smuzhiyun void *master_data);
25*4882a593Smuzhiyun /**
26*4882a593Smuzhiyun * @unbind:
27*4882a593Smuzhiyun *
28*4882a593Smuzhiyun * Called through component_unbind_all() when the aggregate driver is
29*4882a593Smuzhiyun * ready to bind the overall driver, or when component_bind_all() fails
30*4882a593Smuzhiyun * part-ways through and needs to unbind some already bound components.
31*4882a593Smuzhiyun */
32*4882a593Smuzhiyun void (*unbind)(struct device *comp, struct device *master,
33*4882a593Smuzhiyun void *master_data);
34*4882a593Smuzhiyun };
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun int component_add(struct device *, const struct component_ops *);
37*4882a593Smuzhiyun int component_add_typed(struct device *dev, const struct component_ops *ops,
38*4882a593Smuzhiyun int subcomponent);
39*4882a593Smuzhiyun void component_del(struct device *, const struct component_ops *);
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun int component_bind_all(struct device *master, void *master_data);
42*4882a593Smuzhiyun void component_unbind_all(struct device *master, void *master_data);
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun struct master;
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /**
47*4882a593Smuzhiyun * struct component_master_ops - callback for the aggregate driver
48*4882a593Smuzhiyun *
49*4882a593Smuzhiyun * Aggregate drivers are registered with component_master_add_with_match() and
50*4882a593Smuzhiyun * unregistered with component_master_del().
51*4882a593Smuzhiyun */
52*4882a593Smuzhiyun struct component_master_ops {
53*4882a593Smuzhiyun /**
54*4882a593Smuzhiyun * @bind:
55*4882a593Smuzhiyun *
56*4882a593Smuzhiyun * Called when all components or the aggregate driver, as specified in
57*4882a593Smuzhiyun * the match list passed to component_master_add_with_match(), are
58*4882a593Smuzhiyun * ready. Usually there are 3 steps to bind an aggregate driver:
59*4882a593Smuzhiyun *
60*4882a593Smuzhiyun * 1. Allocate a structure for the aggregate driver.
61*4882a593Smuzhiyun *
62*4882a593Smuzhiyun * 2. Bind all components to the aggregate driver by calling
63*4882a593Smuzhiyun * component_bind_all() with the aggregate driver structure as opaque
64*4882a593Smuzhiyun * pointer data.
65*4882a593Smuzhiyun *
66*4882a593Smuzhiyun * 3. Register the aggregate driver with the subsystem to publish its
67*4882a593Smuzhiyun * interfaces.
68*4882a593Smuzhiyun *
69*4882a593Smuzhiyun * Note that the lifetime of the aggregate driver does not align with
70*4882a593Smuzhiyun * any of the underlying &struct device instances. Therefore devm cannot
71*4882a593Smuzhiyun * be used and all resources acquired or allocated in this callback must
72*4882a593Smuzhiyun * be explicitly released in the @unbind callback.
73*4882a593Smuzhiyun */
74*4882a593Smuzhiyun int (*bind)(struct device *master);
75*4882a593Smuzhiyun /**
76*4882a593Smuzhiyun * @unbind:
77*4882a593Smuzhiyun *
78*4882a593Smuzhiyun * Called when either the aggregate driver, using
79*4882a593Smuzhiyun * component_master_del(), or one of its components, using
80*4882a593Smuzhiyun * component_del(), is unregistered.
81*4882a593Smuzhiyun */
82*4882a593Smuzhiyun void (*unbind)(struct device *master);
83*4882a593Smuzhiyun };
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun void component_master_del(struct device *,
86*4882a593Smuzhiyun const struct component_master_ops *);
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun struct component_match;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun int component_master_add_with_match(struct device *,
91*4882a593Smuzhiyun const struct component_master_ops *, struct component_match *);
92*4882a593Smuzhiyun void component_match_add_release(struct device *master,
93*4882a593Smuzhiyun struct component_match **matchptr,
94*4882a593Smuzhiyun void (*release)(struct device *, void *),
95*4882a593Smuzhiyun int (*compare)(struct device *, void *), void *compare_data);
96*4882a593Smuzhiyun void component_match_add_typed(struct device *master,
97*4882a593Smuzhiyun struct component_match **matchptr,
98*4882a593Smuzhiyun int (*compare_typed)(struct device *, int, void *), void *compare_data);
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /**
101*4882a593Smuzhiyun * component_match_add - add a component match entry
102*4882a593Smuzhiyun * @master: device with the aggregate driver
103*4882a593Smuzhiyun * @matchptr: pointer to the list of component matches
104*4882a593Smuzhiyun * @compare: compare function to match against all components
105*4882a593Smuzhiyun * @compare_data: opaque pointer passed to the @compare function
106*4882a593Smuzhiyun *
107*4882a593Smuzhiyun * Adds a new component match to the list stored in @matchptr, which the @master
108*4882a593Smuzhiyun * aggregate driver needs to function. The list of component matches pointed to
109*4882a593Smuzhiyun * by @matchptr must be initialized to NULL before adding the first match. This
110*4882a593Smuzhiyun * only matches against components added with component_add().
111*4882a593Smuzhiyun *
112*4882a593Smuzhiyun * The allocated match list in @matchptr is automatically released using devm
113*4882a593Smuzhiyun * actions.
114*4882a593Smuzhiyun *
115*4882a593Smuzhiyun * See also component_match_add_release() and component_match_add_typed().
116*4882a593Smuzhiyun */
component_match_add(struct device * master,struct component_match ** matchptr,int (* compare)(struct device *,void *),void * compare_data)117*4882a593Smuzhiyun static inline void component_match_add(struct device *master,
118*4882a593Smuzhiyun struct component_match **matchptr,
119*4882a593Smuzhiyun int (*compare)(struct device *, void *), void *compare_data)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun component_match_add_release(master, matchptr, NULL, compare,
122*4882a593Smuzhiyun compare_data);
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun #endif
126