1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Componentized device handling.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * This is work in progress. We gather up the component devices into a list,
6*4882a593Smuzhiyun * and bind them when instructed. At the moment, we're specific to the DRM
7*4882a593Smuzhiyun * subsystem, and only handles one master device, but this doesn't have to be
8*4882a593Smuzhiyun * the case.
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun #include <linux/component.h>
11*4882a593Smuzhiyun #include <linux/device.h>
12*4882a593Smuzhiyun #include <linux/kref.h>
13*4882a593Smuzhiyun #include <linux/list.h>
14*4882a593Smuzhiyun #include <linux/mutex.h>
15*4882a593Smuzhiyun #include <linux/slab.h>
16*4882a593Smuzhiyun #include <linux/debugfs.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun /**
19*4882a593Smuzhiyun * DOC: overview
20*4882a593Smuzhiyun *
21*4882a593Smuzhiyun * The component helper allows drivers to collect a pile of sub-devices,
22*4882a593Smuzhiyun * including their bound drivers, into an aggregate driver. Various subsystems
23*4882a593Smuzhiyun * already provide functions to get hold of such components, e.g.
24*4882a593Smuzhiyun * of_clk_get_by_name(). The component helper can be used when such a
25*4882a593Smuzhiyun * subsystem-specific way to find a device is not available: The component
26*4882a593Smuzhiyun * helper fills the niche of aggregate drivers for specific hardware, where
27*4882a593Smuzhiyun * further standardization into a subsystem would not be practical. The common
28*4882a593Smuzhiyun * example is when a logical device (e.g. a DRM display driver) is spread around
29*4882a593Smuzhiyun * the SoC on various components (scanout engines, blending blocks, transcoders
30*4882a593Smuzhiyun * for various outputs and so on).
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * The component helper also doesn't solve runtime dependencies, e.g. for system
33*4882a593Smuzhiyun * suspend and resume operations. See also :ref:`device links<device_link>`.
34*4882a593Smuzhiyun *
35*4882a593Smuzhiyun * Components are registered using component_add() and unregistered with
36*4882a593Smuzhiyun * component_del(), usually from the driver's probe and disconnect functions.
37*4882a593Smuzhiyun *
38*4882a593Smuzhiyun * Aggregate drivers first assemble a component match list of what they need
39*4882a593Smuzhiyun * using component_match_add(). This is then registered as an aggregate driver
40*4882a593Smuzhiyun * using component_master_add_with_match(), and unregistered using
41*4882a593Smuzhiyun * component_master_del().
42*4882a593Smuzhiyun */
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun struct component;
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun struct component_match_array {
47*4882a593Smuzhiyun void *data;
48*4882a593Smuzhiyun int (*compare)(struct device *, void *);
49*4882a593Smuzhiyun int (*compare_typed)(struct device *, int, void *);
50*4882a593Smuzhiyun void (*release)(struct device *, void *);
51*4882a593Smuzhiyun struct component *component;
52*4882a593Smuzhiyun bool duplicate;
53*4882a593Smuzhiyun };
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun struct component_match {
56*4882a593Smuzhiyun size_t alloc;
57*4882a593Smuzhiyun size_t num;
58*4882a593Smuzhiyun struct component_match_array *compare;
59*4882a593Smuzhiyun };
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun struct master {
62*4882a593Smuzhiyun struct list_head node;
63*4882a593Smuzhiyun bool bound;
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun const struct component_master_ops *ops;
66*4882a593Smuzhiyun struct device *dev;
67*4882a593Smuzhiyun struct component_match *match;
68*4882a593Smuzhiyun struct dentry *dentry;
69*4882a593Smuzhiyun };
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun struct component {
72*4882a593Smuzhiyun struct list_head node;
73*4882a593Smuzhiyun struct master *master;
74*4882a593Smuzhiyun bool bound;
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun const struct component_ops *ops;
77*4882a593Smuzhiyun int subcomponent;
78*4882a593Smuzhiyun struct device *dev;
79*4882a593Smuzhiyun };
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun static DEFINE_MUTEX(component_mutex);
82*4882a593Smuzhiyun static LIST_HEAD(component_list);
83*4882a593Smuzhiyun static LIST_HEAD(masters);
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_FS
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun static struct dentry *component_debugfs_dir;
88*4882a593Smuzhiyun
component_devices_show(struct seq_file * s,void * data)89*4882a593Smuzhiyun static int component_devices_show(struct seq_file *s, void *data)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun struct master *m = s->private;
92*4882a593Smuzhiyun struct component_match *match = m->match;
93*4882a593Smuzhiyun size_t i;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun mutex_lock(&component_mutex);
96*4882a593Smuzhiyun seq_printf(s, "%-40s %20s\n", "master name", "status");
97*4882a593Smuzhiyun seq_puts(s, "-------------------------------------------------------------\n");
98*4882a593Smuzhiyun seq_printf(s, "%-40s %20s\n\n",
99*4882a593Smuzhiyun dev_name(m->dev), m->bound ? "bound" : "not bound");
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun seq_printf(s, "%-40s %20s\n", "device name", "status");
102*4882a593Smuzhiyun seq_puts(s, "-------------------------------------------------------------\n");
103*4882a593Smuzhiyun for (i = 0; i < match->num; i++) {
104*4882a593Smuzhiyun struct component *component = match->compare[i].component;
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun seq_printf(s, "%-40s %20s\n",
107*4882a593Smuzhiyun component ? dev_name(component->dev) : "(unknown)",
108*4882a593Smuzhiyun component ? (component->bound ? "bound" : "not bound") : "not registered");
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun mutex_unlock(&component_mutex);
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun return 0;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun DEFINE_SHOW_ATTRIBUTE(component_devices);
116*4882a593Smuzhiyun
component_debug_init(void)117*4882a593Smuzhiyun static int __init component_debug_init(void)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun component_debugfs_dir = debugfs_create_dir("device_component", NULL);
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun return 0;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun core_initcall(component_debug_init);
125*4882a593Smuzhiyun
component_master_debugfs_add(struct master * m)126*4882a593Smuzhiyun static void component_master_debugfs_add(struct master *m)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun m->dentry = debugfs_create_file(dev_name(m->dev), 0444,
129*4882a593Smuzhiyun component_debugfs_dir,
130*4882a593Smuzhiyun m, &component_devices_fops);
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
component_master_debugfs_del(struct master * m)133*4882a593Smuzhiyun static void component_master_debugfs_del(struct master *m)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun debugfs_remove(m->dentry);
136*4882a593Smuzhiyun m->dentry = NULL;
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun #else
140*4882a593Smuzhiyun
component_master_debugfs_add(struct master * m)141*4882a593Smuzhiyun static void component_master_debugfs_add(struct master *m)
142*4882a593Smuzhiyun { }
143*4882a593Smuzhiyun
component_master_debugfs_del(struct master * m)144*4882a593Smuzhiyun static void component_master_debugfs_del(struct master *m)
145*4882a593Smuzhiyun { }
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun #endif
148*4882a593Smuzhiyun
__master_find(struct device * dev,const struct component_master_ops * ops)149*4882a593Smuzhiyun static struct master *__master_find(struct device *dev,
150*4882a593Smuzhiyun const struct component_master_ops *ops)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun struct master *m;
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun list_for_each_entry(m, &masters, node)
155*4882a593Smuzhiyun if (m->dev == dev && (!ops || m->ops == ops))
156*4882a593Smuzhiyun return m;
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun return NULL;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun
find_component(struct master * master,struct component_match_array * mc)161*4882a593Smuzhiyun static struct component *find_component(struct master *master,
162*4882a593Smuzhiyun struct component_match_array *mc)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun struct component *c;
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun list_for_each_entry(c, &component_list, node) {
167*4882a593Smuzhiyun if (c->master && c->master != master)
168*4882a593Smuzhiyun continue;
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun if (mc->compare && mc->compare(c->dev, mc->data))
171*4882a593Smuzhiyun return c;
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun if (mc->compare_typed &&
174*4882a593Smuzhiyun mc->compare_typed(c->dev, c->subcomponent, mc->data))
175*4882a593Smuzhiyun return c;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun return NULL;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
find_components(struct master * master)181*4882a593Smuzhiyun static int find_components(struct master *master)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun struct component_match *match = master->match;
184*4882a593Smuzhiyun size_t i;
185*4882a593Smuzhiyun int ret = 0;
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun /*
188*4882a593Smuzhiyun * Scan the array of match functions and attach
189*4882a593Smuzhiyun * any components which are found to this master.
190*4882a593Smuzhiyun */
191*4882a593Smuzhiyun for (i = 0; i < match->num; i++) {
192*4882a593Smuzhiyun struct component_match_array *mc = &match->compare[i];
193*4882a593Smuzhiyun struct component *c;
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun dev_dbg(master->dev, "Looking for component %zu\n", i);
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun if (match->compare[i].component)
198*4882a593Smuzhiyun continue;
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun c = find_component(master, mc);
201*4882a593Smuzhiyun if (!c) {
202*4882a593Smuzhiyun ret = -ENXIO;
203*4882a593Smuzhiyun break;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun dev_dbg(master->dev, "found component %s, duplicate %u\n", dev_name(c->dev), !!c->master);
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun /* Attach this component to the master */
209*4882a593Smuzhiyun match->compare[i].duplicate = !!c->master;
210*4882a593Smuzhiyun match->compare[i].component = c;
211*4882a593Smuzhiyun c->master = master;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun return ret;
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun /* Detach component from associated master */
remove_component(struct master * master,struct component * c)217*4882a593Smuzhiyun static void remove_component(struct master *master, struct component *c)
218*4882a593Smuzhiyun {
219*4882a593Smuzhiyun size_t i;
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun /* Detach the component from this master. */
222*4882a593Smuzhiyun for (i = 0; i < master->match->num; i++)
223*4882a593Smuzhiyun if (master->match->compare[i].component == c)
224*4882a593Smuzhiyun master->match->compare[i].component = NULL;
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun /*
228*4882a593Smuzhiyun * Try to bring up a master. If component is NULL, we're interested in
229*4882a593Smuzhiyun * this master, otherwise it's a component which must be present to try
230*4882a593Smuzhiyun * and bring up the master.
231*4882a593Smuzhiyun *
232*4882a593Smuzhiyun * Returns 1 for successful bringup, 0 if not ready, or -ve errno.
233*4882a593Smuzhiyun */
try_to_bring_up_master(struct master * master,struct component * component)234*4882a593Smuzhiyun static int try_to_bring_up_master(struct master *master,
235*4882a593Smuzhiyun struct component *component)
236*4882a593Smuzhiyun {
237*4882a593Smuzhiyun int ret;
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun dev_dbg(master->dev, "trying to bring up master\n");
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun if (find_components(master)) {
242*4882a593Smuzhiyun dev_dbg(master->dev, "master has incomplete components\n");
243*4882a593Smuzhiyun return 0;
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun if (component && component->master != master) {
247*4882a593Smuzhiyun dev_dbg(master->dev, "master is not for this component (%s)\n",
248*4882a593Smuzhiyun dev_name(component->dev));
249*4882a593Smuzhiyun return 0;
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
253*4882a593Smuzhiyun return -ENOMEM;
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun /* Found all components */
256*4882a593Smuzhiyun ret = master->ops->bind(master->dev);
257*4882a593Smuzhiyun if (ret < 0) {
258*4882a593Smuzhiyun devres_release_group(master->dev, NULL);
259*4882a593Smuzhiyun if (ret != -EPROBE_DEFER)
260*4882a593Smuzhiyun dev_info(master->dev, "master bind failed: %d\n", ret);
261*4882a593Smuzhiyun return ret;
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun master->bound = true;
265*4882a593Smuzhiyun return 1;
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun
try_to_bring_up_masters(struct component * component)268*4882a593Smuzhiyun static int try_to_bring_up_masters(struct component *component)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun struct master *m;
271*4882a593Smuzhiyun int ret = 0;
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun list_for_each_entry(m, &masters, node) {
274*4882a593Smuzhiyun if (!m->bound) {
275*4882a593Smuzhiyun ret = try_to_bring_up_master(m, component);
276*4882a593Smuzhiyun if (ret != 0)
277*4882a593Smuzhiyun break;
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun return ret;
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun
take_down_master(struct master * master)284*4882a593Smuzhiyun static void take_down_master(struct master *master)
285*4882a593Smuzhiyun {
286*4882a593Smuzhiyun if (master->bound) {
287*4882a593Smuzhiyun master->ops->unbind(master->dev);
288*4882a593Smuzhiyun devres_release_group(master->dev, NULL);
289*4882a593Smuzhiyun master->bound = false;
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun
component_match_release(struct device * master,struct component_match * match)293*4882a593Smuzhiyun static void component_match_release(struct device *master,
294*4882a593Smuzhiyun struct component_match *match)
295*4882a593Smuzhiyun {
296*4882a593Smuzhiyun unsigned int i;
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun for (i = 0; i < match->num; i++) {
299*4882a593Smuzhiyun struct component_match_array *mc = &match->compare[i];
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun if (mc->release)
302*4882a593Smuzhiyun mc->release(master, mc->data);
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun kfree(match->compare);
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun
devm_component_match_release(struct device * dev,void * res)308*4882a593Smuzhiyun static void devm_component_match_release(struct device *dev, void *res)
309*4882a593Smuzhiyun {
310*4882a593Smuzhiyun component_match_release(dev, res);
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun
component_match_realloc(struct device * dev,struct component_match * match,size_t num)313*4882a593Smuzhiyun static int component_match_realloc(struct device *dev,
314*4882a593Smuzhiyun struct component_match *match, size_t num)
315*4882a593Smuzhiyun {
316*4882a593Smuzhiyun struct component_match_array *new;
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun if (match->alloc == num)
319*4882a593Smuzhiyun return 0;
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun new = kmalloc_array(num, sizeof(*new), GFP_KERNEL);
322*4882a593Smuzhiyun if (!new)
323*4882a593Smuzhiyun return -ENOMEM;
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun if (match->compare) {
326*4882a593Smuzhiyun memcpy(new, match->compare, sizeof(*new) *
327*4882a593Smuzhiyun min(match->num, num));
328*4882a593Smuzhiyun kfree(match->compare);
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun match->compare = new;
331*4882a593Smuzhiyun match->alloc = num;
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun return 0;
334*4882a593Smuzhiyun }
335*4882a593Smuzhiyun
__component_match_add(struct device * master,struct component_match ** matchptr,void (* release)(struct device *,void *),int (* compare)(struct device *,void *),int (* compare_typed)(struct device *,int,void *),void * compare_data)336*4882a593Smuzhiyun static void __component_match_add(struct device *master,
337*4882a593Smuzhiyun struct component_match **matchptr,
338*4882a593Smuzhiyun void (*release)(struct device *, void *),
339*4882a593Smuzhiyun int (*compare)(struct device *, void *),
340*4882a593Smuzhiyun int (*compare_typed)(struct device *, int, void *),
341*4882a593Smuzhiyun void *compare_data)
342*4882a593Smuzhiyun {
343*4882a593Smuzhiyun struct component_match *match = *matchptr;
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun if (IS_ERR(match))
346*4882a593Smuzhiyun return;
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun if (!match) {
349*4882a593Smuzhiyun match = devres_alloc(devm_component_match_release,
350*4882a593Smuzhiyun sizeof(*match), GFP_KERNEL);
351*4882a593Smuzhiyun if (!match) {
352*4882a593Smuzhiyun *matchptr = ERR_PTR(-ENOMEM);
353*4882a593Smuzhiyun return;
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun devres_add(master, match);
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun *matchptr = match;
359*4882a593Smuzhiyun }
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun if (match->num == match->alloc) {
362*4882a593Smuzhiyun size_t new_size = match->alloc + 16;
363*4882a593Smuzhiyun int ret;
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun ret = component_match_realloc(master, match, new_size);
366*4882a593Smuzhiyun if (ret) {
367*4882a593Smuzhiyun *matchptr = ERR_PTR(ret);
368*4882a593Smuzhiyun return;
369*4882a593Smuzhiyun }
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun match->compare[match->num].compare = compare;
373*4882a593Smuzhiyun match->compare[match->num].compare_typed = compare_typed;
374*4882a593Smuzhiyun match->compare[match->num].release = release;
375*4882a593Smuzhiyun match->compare[match->num].data = compare_data;
376*4882a593Smuzhiyun match->compare[match->num].component = NULL;
377*4882a593Smuzhiyun match->num++;
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun /**
381*4882a593Smuzhiyun * component_match_add_release - add a component match entry with release callback
382*4882a593Smuzhiyun * @master: device with the aggregate driver
383*4882a593Smuzhiyun * @matchptr: pointer to the list of component matches
384*4882a593Smuzhiyun * @release: release function for @compare_data
385*4882a593Smuzhiyun * @compare: compare function to match against all components
386*4882a593Smuzhiyun * @compare_data: opaque pointer passed to the @compare function
387*4882a593Smuzhiyun *
388*4882a593Smuzhiyun * Adds a new component match to the list stored in @matchptr, which the @master
389*4882a593Smuzhiyun * aggregate driver needs to function. The list of component matches pointed to
390*4882a593Smuzhiyun * by @matchptr must be initialized to NULL before adding the first match. This
391*4882a593Smuzhiyun * only matches against components added with component_add().
392*4882a593Smuzhiyun *
393*4882a593Smuzhiyun * The allocated match list in @matchptr is automatically released using devm
394*4882a593Smuzhiyun * actions, where upon @release will be called to free any references held by
395*4882a593Smuzhiyun * @compare_data, e.g. when @compare_data is a &device_node that must be
396*4882a593Smuzhiyun * released with of_node_put().
397*4882a593Smuzhiyun *
398*4882a593Smuzhiyun * See also component_match_add() and component_match_add_typed().
399*4882a593Smuzhiyun */
component_match_add_release(struct device * master,struct component_match ** matchptr,void (* release)(struct device *,void *),int (* compare)(struct device *,void *),void * compare_data)400*4882a593Smuzhiyun void component_match_add_release(struct device *master,
401*4882a593Smuzhiyun struct component_match **matchptr,
402*4882a593Smuzhiyun void (*release)(struct device *, void *),
403*4882a593Smuzhiyun int (*compare)(struct device *, void *), void *compare_data)
404*4882a593Smuzhiyun {
405*4882a593Smuzhiyun __component_match_add(master, matchptr, release, compare, NULL,
406*4882a593Smuzhiyun compare_data);
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun EXPORT_SYMBOL(component_match_add_release);
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun /**
411*4882a593Smuzhiyun * component_match_add_typed - add a component match entry for a typed component
412*4882a593Smuzhiyun * @master: device with the aggregate driver
413*4882a593Smuzhiyun * @matchptr: pointer to the list of component matches
414*4882a593Smuzhiyun * @compare_typed: compare function to match against all typed components
415*4882a593Smuzhiyun * @compare_data: opaque pointer passed to the @compare function
416*4882a593Smuzhiyun *
417*4882a593Smuzhiyun * Adds a new component match to the list stored in @matchptr, which the @master
418*4882a593Smuzhiyun * aggregate driver needs to function. The list of component matches pointed to
419*4882a593Smuzhiyun * by @matchptr must be initialized to NULL before adding the first match. This
420*4882a593Smuzhiyun * only matches against components added with component_add_typed().
421*4882a593Smuzhiyun *
422*4882a593Smuzhiyun * The allocated match list in @matchptr is automatically released using devm
423*4882a593Smuzhiyun * actions.
424*4882a593Smuzhiyun *
425*4882a593Smuzhiyun * See also component_match_add_release() and component_match_add_typed().
426*4882a593Smuzhiyun */
component_match_add_typed(struct device * master,struct component_match ** matchptr,int (* compare_typed)(struct device *,int,void *),void * compare_data)427*4882a593Smuzhiyun void component_match_add_typed(struct device *master,
428*4882a593Smuzhiyun struct component_match **matchptr,
429*4882a593Smuzhiyun int (*compare_typed)(struct device *, int, void *), void *compare_data)
430*4882a593Smuzhiyun {
431*4882a593Smuzhiyun __component_match_add(master, matchptr, NULL, NULL, compare_typed,
432*4882a593Smuzhiyun compare_data);
433*4882a593Smuzhiyun }
434*4882a593Smuzhiyun EXPORT_SYMBOL(component_match_add_typed);
435*4882a593Smuzhiyun
free_master(struct master * master)436*4882a593Smuzhiyun static void free_master(struct master *master)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun struct component_match *match = master->match;
439*4882a593Smuzhiyun int i;
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun component_master_debugfs_del(master);
442*4882a593Smuzhiyun list_del(&master->node);
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun if (match) {
445*4882a593Smuzhiyun for (i = 0; i < match->num; i++) {
446*4882a593Smuzhiyun struct component *c = match->compare[i].component;
447*4882a593Smuzhiyun if (c)
448*4882a593Smuzhiyun c->master = NULL;
449*4882a593Smuzhiyun }
450*4882a593Smuzhiyun }
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun kfree(master);
453*4882a593Smuzhiyun }
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun /**
456*4882a593Smuzhiyun * component_master_add_with_match - register an aggregate driver
457*4882a593Smuzhiyun * @dev: device with the aggregate driver
458*4882a593Smuzhiyun * @ops: callbacks for the aggregate driver
459*4882a593Smuzhiyun * @match: component match list for the aggregate driver
460*4882a593Smuzhiyun *
461*4882a593Smuzhiyun * Registers a new aggregate driver consisting of the components added to @match
462*4882a593Smuzhiyun * by calling one of the component_match_add() functions. Once all components in
463*4882a593Smuzhiyun * @match are available, it will be assembled by calling
464*4882a593Smuzhiyun * &component_master_ops.bind from @ops. Must be unregistered by calling
465*4882a593Smuzhiyun * component_master_del().
466*4882a593Smuzhiyun */
component_master_add_with_match(struct device * dev,const struct component_master_ops * ops,struct component_match * match)467*4882a593Smuzhiyun int component_master_add_with_match(struct device *dev,
468*4882a593Smuzhiyun const struct component_master_ops *ops,
469*4882a593Smuzhiyun struct component_match *match)
470*4882a593Smuzhiyun {
471*4882a593Smuzhiyun struct master *master;
472*4882a593Smuzhiyun int ret;
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun /* Reallocate the match array for its true size */
475*4882a593Smuzhiyun ret = component_match_realloc(dev, match, match->num);
476*4882a593Smuzhiyun if (ret)
477*4882a593Smuzhiyun return ret;
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun master = kzalloc(sizeof(*master), GFP_KERNEL);
480*4882a593Smuzhiyun if (!master)
481*4882a593Smuzhiyun return -ENOMEM;
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun master->dev = dev;
484*4882a593Smuzhiyun master->ops = ops;
485*4882a593Smuzhiyun master->match = match;
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun component_master_debugfs_add(master);
488*4882a593Smuzhiyun /* Add to the list of available masters. */
489*4882a593Smuzhiyun mutex_lock(&component_mutex);
490*4882a593Smuzhiyun list_add(&master->node, &masters);
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun ret = try_to_bring_up_master(master, NULL);
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun if (ret < 0)
495*4882a593Smuzhiyun free_master(master);
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun mutex_unlock(&component_mutex);
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun return ret < 0 ? ret : 0;
500*4882a593Smuzhiyun }
501*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(component_master_add_with_match);
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun /**
504*4882a593Smuzhiyun * component_master_del - unregister an aggregate driver
505*4882a593Smuzhiyun * @dev: device with the aggregate driver
506*4882a593Smuzhiyun * @ops: callbacks for the aggregate driver
507*4882a593Smuzhiyun *
508*4882a593Smuzhiyun * Unregisters an aggregate driver registered with
509*4882a593Smuzhiyun * component_master_add_with_match(). If necessary the aggregate driver is first
510*4882a593Smuzhiyun * disassembled by calling &component_master_ops.unbind from @ops.
511*4882a593Smuzhiyun */
component_master_del(struct device * dev,const struct component_master_ops * ops)512*4882a593Smuzhiyun void component_master_del(struct device *dev,
513*4882a593Smuzhiyun const struct component_master_ops *ops)
514*4882a593Smuzhiyun {
515*4882a593Smuzhiyun struct master *master;
516*4882a593Smuzhiyun
517*4882a593Smuzhiyun mutex_lock(&component_mutex);
518*4882a593Smuzhiyun master = __master_find(dev, ops);
519*4882a593Smuzhiyun if (master) {
520*4882a593Smuzhiyun take_down_master(master);
521*4882a593Smuzhiyun free_master(master);
522*4882a593Smuzhiyun }
523*4882a593Smuzhiyun mutex_unlock(&component_mutex);
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(component_master_del);
526*4882a593Smuzhiyun
component_unbind(struct component * component,struct master * master,void * data)527*4882a593Smuzhiyun static void component_unbind(struct component *component,
528*4882a593Smuzhiyun struct master *master, void *data)
529*4882a593Smuzhiyun {
530*4882a593Smuzhiyun WARN_ON(!component->bound);
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun if (component->ops && component->ops->unbind)
533*4882a593Smuzhiyun component->ops->unbind(component->dev, master->dev, data);
534*4882a593Smuzhiyun component->bound = false;
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun /* Release all resources claimed in the binding of this component */
537*4882a593Smuzhiyun devres_release_group(component->dev, component);
538*4882a593Smuzhiyun }
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun /**
541*4882a593Smuzhiyun * component_unbind_all - unbind all components of an aggregate driver
542*4882a593Smuzhiyun * @master_dev: device with the aggregate driver
543*4882a593Smuzhiyun * @data: opaque pointer, passed to all components
544*4882a593Smuzhiyun *
545*4882a593Smuzhiyun * Unbinds all components of the aggregate @dev by passing @data to their
546*4882a593Smuzhiyun * &component_ops.unbind functions. Should be called from
547*4882a593Smuzhiyun * &component_master_ops.unbind.
548*4882a593Smuzhiyun */
component_unbind_all(struct device * master_dev,void * data)549*4882a593Smuzhiyun void component_unbind_all(struct device *master_dev, void *data)
550*4882a593Smuzhiyun {
551*4882a593Smuzhiyun struct master *master;
552*4882a593Smuzhiyun struct component *c;
553*4882a593Smuzhiyun size_t i;
554*4882a593Smuzhiyun
555*4882a593Smuzhiyun WARN_ON(!mutex_is_locked(&component_mutex));
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun master = __master_find(master_dev, NULL);
558*4882a593Smuzhiyun if (!master)
559*4882a593Smuzhiyun return;
560*4882a593Smuzhiyun
561*4882a593Smuzhiyun /* Unbind components in reverse order */
562*4882a593Smuzhiyun for (i = master->match->num; i--; )
563*4882a593Smuzhiyun if (!master->match->compare[i].duplicate) {
564*4882a593Smuzhiyun c = master->match->compare[i].component;
565*4882a593Smuzhiyun component_unbind(c, master, data);
566*4882a593Smuzhiyun }
567*4882a593Smuzhiyun }
568*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(component_unbind_all);
569*4882a593Smuzhiyun
component_bind(struct component * component,struct master * master,void * data)570*4882a593Smuzhiyun static int component_bind(struct component *component, struct master *master,
571*4882a593Smuzhiyun void *data)
572*4882a593Smuzhiyun {
573*4882a593Smuzhiyun int ret;
574*4882a593Smuzhiyun
575*4882a593Smuzhiyun /*
576*4882a593Smuzhiyun * Each component initialises inside its own devres group.
577*4882a593Smuzhiyun * This allows us to roll-back a failed component without
578*4882a593Smuzhiyun * affecting anything else.
579*4882a593Smuzhiyun */
580*4882a593Smuzhiyun if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
581*4882a593Smuzhiyun return -ENOMEM;
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun /*
584*4882a593Smuzhiyun * Also open a group for the device itself: this allows us
585*4882a593Smuzhiyun * to release the resources claimed against the sub-device
586*4882a593Smuzhiyun * at the appropriate moment.
587*4882a593Smuzhiyun */
588*4882a593Smuzhiyun if (!devres_open_group(component->dev, component, GFP_KERNEL)) {
589*4882a593Smuzhiyun devres_release_group(master->dev, NULL);
590*4882a593Smuzhiyun return -ENOMEM;
591*4882a593Smuzhiyun }
592*4882a593Smuzhiyun
593*4882a593Smuzhiyun dev_dbg(master->dev, "binding %s (ops %ps)\n",
594*4882a593Smuzhiyun dev_name(component->dev), component->ops);
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun ret = component->ops->bind(component->dev, master->dev, data);
597*4882a593Smuzhiyun if (!ret) {
598*4882a593Smuzhiyun component->bound = true;
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun /*
601*4882a593Smuzhiyun * Close the component device's group so that resources
602*4882a593Smuzhiyun * allocated in the binding are encapsulated for removal
603*4882a593Smuzhiyun * at unbind. Remove the group on the DRM device as we
604*4882a593Smuzhiyun * can clean those resources up independently.
605*4882a593Smuzhiyun */
606*4882a593Smuzhiyun devres_close_group(component->dev, NULL);
607*4882a593Smuzhiyun devres_remove_group(master->dev, NULL);
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun dev_info(master->dev, "bound %s (ops %ps)\n",
610*4882a593Smuzhiyun dev_name(component->dev), component->ops);
611*4882a593Smuzhiyun } else {
612*4882a593Smuzhiyun devres_release_group(component->dev, NULL);
613*4882a593Smuzhiyun devres_release_group(master->dev, NULL);
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun if (ret != -EPROBE_DEFER)
616*4882a593Smuzhiyun dev_err(master->dev, "failed to bind %s (ops %ps): %d\n",
617*4882a593Smuzhiyun dev_name(component->dev), component->ops, ret);
618*4882a593Smuzhiyun }
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun return ret;
621*4882a593Smuzhiyun }
622*4882a593Smuzhiyun
623*4882a593Smuzhiyun /**
624*4882a593Smuzhiyun * component_bind_all - bind all components of an aggregate driver
625*4882a593Smuzhiyun * @master_dev: device with the aggregate driver
626*4882a593Smuzhiyun * @data: opaque pointer, passed to all components
627*4882a593Smuzhiyun *
628*4882a593Smuzhiyun * Binds all components of the aggregate @dev by passing @data to their
629*4882a593Smuzhiyun * &component_ops.bind functions. Should be called from
630*4882a593Smuzhiyun * &component_master_ops.bind.
631*4882a593Smuzhiyun */
component_bind_all(struct device * master_dev,void * data)632*4882a593Smuzhiyun int component_bind_all(struct device *master_dev, void *data)
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun struct master *master;
635*4882a593Smuzhiyun struct component *c;
636*4882a593Smuzhiyun size_t i;
637*4882a593Smuzhiyun int ret = 0;
638*4882a593Smuzhiyun
639*4882a593Smuzhiyun WARN_ON(!mutex_is_locked(&component_mutex));
640*4882a593Smuzhiyun
641*4882a593Smuzhiyun master = __master_find(master_dev, NULL);
642*4882a593Smuzhiyun if (!master)
643*4882a593Smuzhiyun return -EINVAL;
644*4882a593Smuzhiyun
645*4882a593Smuzhiyun /* Bind components in match order */
646*4882a593Smuzhiyun for (i = 0; i < master->match->num; i++)
647*4882a593Smuzhiyun if (!master->match->compare[i].duplicate) {
648*4882a593Smuzhiyun c = master->match->compare[i].component;
649*4882a593Smuzhiyun ret = component_bind(c, master, data);
650*4882a593Smuzhiyun if (ret)
651*4882a593Smuzhiyun break;
652*4882a593Smuzhiyun }
653*4882a593Smuzhiyun
654*4882a593Smuzhiyun if (ret != 0) {
655*4882a593Smuzhiyun for (; i > 0; i--)
656*4882a593Smuzhiyun if (!master->match->compare[i - 1].duplicate) {
657*4882a593Smuzhiyun c = master->match->compare[i - 1].component;
658*4882a593Smuzhiyun component_unbind(c, master, data);
659*4882a593Smuzhiyun }
660*4882a593Smuzhiyun }
661*4882a593Smuzhiyun
662*4882a593Smuzhiyun return ret;
663*4882a593Smuzhiyun }
664*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(component_bind_all);
665*4882a593Smuzhiyun
__component_add(struct device * dev,const struct component_ops * ops,int subcomponent)666*4882a593Smuzhiyun static int __component_add(struct device *dev, const struct component_ops *ops,
667*4882a593Smuzhiyun int subcomponent)
668*4882a593Smuzhiyun {
669*4882a593Smuzhiyun struct component *component;
670*4882a593Smuzhiyun int ret;
671*4882a593Smuzhiyun
672*4882a593Smuzhiyun component = kzalloc(sizeof(*component), GFP_KERNEL);
673*4882a593Smuzhiyun if (!component)
674*4882a593Smuzhiyun return -ENOMEM;
675*4882a593Smuzhiyun
676*4882a593Smuzhiyun component->ops = ops;
677*4882a593Smuzhiyun component->dev = dev;
678*4882a593Smuzhiyun component->subcomponent = subcomponent;
679*4882a593Smuzhiyun
680*4882a593Smuzhiyun dev_dbg(dev, "adding component (ops %ps)\n", ops);
681*4882a593Smuzhiyun
682*4882a593Smuzhiyun mutex_lock(&component_mutex);
683*4882a593Smuzhiyun list_add_tail(&component->node, &component_list);
684*4882a593Smuzhiyun
685*4882a593Smuzhiyun ret = try_to_bring_up_masters(component);
686*4882a593Smuzhiyun if (ret < 0) {
687*4882a593Smuzhiyun if (component->master)
688*4882a593Smuzhiyun remove_component(component->master, component);
689*4882a593Smuzhiyun list_del(&component->node);
690*4882a593Smuzhiyun
691*4882a593Smuzhiyun kfree(component);
692*4882a593Smuzhiyun }
693*4882a593Smuzhiyun mutex_unlock(&component_mutex);
694*4882a593Smuzhiyun
695*4882a593Smuzhiyun return ret < 0 ? ret : 0;
696*4882a593Smuzhiyun }
697*4882a593Smuzhiyun
698*4882a593Smuzhiyun /**
699*4882a593Smuzhiyun * component_add_typed - register a component
700*4882a593Smuzhiyun * @dev: component device
701*4882a593Smuzhiyun * @ops: component callbacks
702*4882a593Smuzhiyun * @subcomponent: nonzero identifier for subcomponents
703*4882a593Smuzhiyun *
704*4882a593Smuzhiyun * Register a new component for @dev. Functions in @ops will be call when the
705*4882a593Smuzhiyun * aggregate driver is ready to bind the overall driver by calling
706*4882a593Smuzhiyun * component_bind_all(). See also &struct component_ops.
707*4882a593Smuzhiyun *
708*4882a593Smuzhiyun * @subcomponent must be nonzero and is used to differentiate between multiple
709*4882a593Smuzhiyun * components registerd on the same device @dev. These components are match
710*4882a593Smuzhiyun * using component_match_add_typed().
711*4882a593Smuzhiyun *
712*4882a593Smuzhiyun * The component needs to be unregistered at driver unload/disconnect by
713*4882a593Smuzhiyun * calling component_del().
714*4882a593Smuzhiyun *
715*4882a593Smuzhiyun * See also component_add().
716*4882a593Smuzhiyun */
component_add_typed(struct device * dev,const struct component_ops * ops,int subcomponent)717*4882a593Smuzhiyun int component_add_typed(struct device *dev, const struct component_ops *ops,
718*4882a593Smuzhiyun int subcomponent)
719*4882a593Smuzhiyun {
720*4882a593Smuzhiyun if (WARN_ON(subcomponent == 0))
721*4882a593Smuzhiyun return -EINVAL;
722*4882a593Smuzhiyun
723*4882a593Smuzhiyun return __component_add(dev, ops, subcomponent);
724*4882a593Smuzhiyun }
725*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(component_add_typed);
726*4882a593Smuzhiyun
727*4882a593Smuzhiyun /**
728*4882a593Smuzhiyun * component_add - register a component
729*4882a593Smuzhiyun * @dev: component device
730*4882a593Smuzhiyun * @ops: component callbacks
731*4882a593Smuzhiyun *
732*4882a593Smuzhiyun * Register a new component for @dev. Functions in @ops will be called when the
733*4882a593Smuzhiyun * aggregate driver is ready to bind the overall driver by calling
734*4882a593Smuzhiyun * component_bind_all(). See also &struct component_ops.
735*4882a593Smuzhiyun *
736*4882a593Smuzhiyun * The component needs to be unregistered at driver unload/disconnect by
737*4882a593Smuzhiyun * calling component_del().
738*4882a593Smuzhiyun *
739*4882a593Smuzhiyun * See also component_add_typed() for a variant that allows multipled different
740*4882a593Smuzhiyun * components on the same device.
741*4882a593Smuzhiyun */
component_add(struct device * dev,const struct component_ops * ops)742*4882a593Smuzhiyun int component_add(struct device *dev, const struct component_ops *ops)
743*4882a593Smuzhiyun {
744*4882a593Smuzhiyun return __component_add(dev, ops, 0);
745*4882a593Smuzhiyun }
746*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(component_add);
747*4882a593Smuzhiyun
748*4882a593Smuzhiyun /**
749*4882a593Smuzhiyun * component_del - unregister a component
750*4882a593Smuzhiyun * @dev: component device
751*4882a593Smuzhiyun * @ops: component callbacks
752*4882a593Smuzhiyun *
753*4882a593Smuzhiyun * Unregister a component added with component_add(). If the component is bound
754*4882a593Smuzhiyun * into an aggregate driver, this will force the entire aggregate driver, including
755*4882a593Smuzhiyun * all its components, to be unbound.
756*4882a593Smuzhiyun */
component_del(struct device * dev,const struct component_ops * ops)757*4882a593Smuzhiyun void component_del(struct device *dev, const struct component_ops *ops)
758*4882a593Smuzhiyun {
759*4882a593Smuzhiyun struct component *c, *component = NULL;
760*4882a593Smuzhiyun
761*4882a593Smuzhiyun mutex_lock(&component_mutex);
762*4882a593Smuzhiyun list_for_each_entry(c, &component_list, node)
763*4882a593Smuzhiyun if (c->dev == dev && c->ops == ops) {
764*4882a593Smuzhiyun list_del(&c->node);
765*4882a593Smuzhiyun component = c;
766*4882a593Smuzhiyun break;
767*4882a593Smuzhiyun }
768*4882a593Smuzhiyun
769*4882a593Smuzhiyun if (component && component->master) {
770*4882a593Smuzhiyun take_down_master(component->master);
771*4882a593Smuzhiyun remove_component(component->master, component);
772*4882a593Smuzhiyun }
773*4882a593Smuzhiyun
774*4882a593Smuzhiyun mutex_unlock(&component_mutex);
775*4882a593Smuzhiyun
776*4882a593Smuzhiyun WARN_ON(!component);
777*4882a593Smuzhiyun kfree(component);
778*4882a593Smuzhiyun }
779*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(component_del);
780