blob: 1624c2a892a5511d759f0d98acda91a2b4b1ca25 [file] [log] [blame]
Greg Kroah-Hartman989d42e2017-11-07 17:30:07 +01001// SPDX-License-Identifier: GPL-2.0
Russell King2a41e602014-01-10 23:23:37 +00002/*
3 * Componentized device handling.
4 *
Russell King2a41e602014-01-10 23:23:37 +00005 * This is work in progress. We gather up the component devices into a list,
6 * and bind them when instructed. At the moment, we're specific to the DRM
7 * subsystem, and only handles one master device, but this doesn't have to be
8 * the case.
9 */
10#include <linux/component.h>
11#include <linux/device.h>
12#include <linux/kref.h>
13#include <linux/list.h>
14#include <linux/module.h>
15#include <linux/mutex.h>
16#include <linux/slab.h>
Maciej Purski59e73852017-12-01 14:23:16 +010017#include <linux/debugfs.h>
Russell King2a41e602014-01-10 23:23:37 +000018
Daniel Vetter4d69c802019-02-08 00:27:56 +010019/**
20 * DOC: overview
21 *
22 * The component helper allows drivers to collect a pile of sub-devices,
23 * including their bound drivers, into an aggregate driver. Various subsystems
24 * already provide functions to get hold of such components, e.g.
25 * of_clk_get_by_name(). The component helper can be used when such a
26 * subsystem-specific way to find a device is not available: The component
27 * helper fills the niche of aggregate drivers for specific hardware, where
28 * further standardization into a subsystem would not be practical. The common
29 * example is when a logical device (e.g. a DRM display driver) is spread around
30 * the SoC on various component (scanout engines, blending blocks, transcoders
31 * for various outputs and so on).
32 *
33 * The component helper also doesn't solve runtime dependencies, e.g. for system
34 * suspend and resume operations. See also :ref:`device links<device_link>`.
35 *
36 * Components are registered using component_add() and unregistered with
37 * component_del(), usually from the driver's probe and disconnect functions.
38 *
39 * Aggregate drivers first assemble a component match list of what they need
40 * using component_match_add(). This is then registered as an aggregate driver
41 * using component_master_add_with_match(), and unregistered using
42 * component_master_del().
43 */
44
Russell Kingffc30b72014-04-18 23:05:53 +010045struct component;
46
Russell Kingce657b12015-11-17 12:08:01 +000047struct component_match_array {
48 void *data;
49 int (*compare)(struct device *, void *);
50 void (*release)(struct device *, void *);
51 struct component *component;
52 bool duplicate;
53};
54
Russell King6955b582014-04-19 11:18:01 +010055struct component_match {
56 size_t alloc;
57 size_t num;
Russell Kingce657b12015-11-17 12:08:01 +000058 struct component_match_array *compare;
Russell King6955b582014-04-19 11:18:01 +010059};
60
Russell King2a41e602014-01-10 23:23:37 +000061struct master {
62 struct list_head node;
Russell King2a41e602014-01-10 23:23:37 +000063 bool bound;
64
65 const struct component_master_ops *ops;
66 struct device *dev;
Russell King6955b582014-04-19 11:18:01 +010067 struct component_match *match;
Maciej Purski59e73852017-12-01 14:23:16 +010068 struct dentry *dentry;
Russell King2a41e602014-01-10 23:23:37 +000069};
70
71struct component {
72 struct list_head node;
Russell King2a41e602014-01-10 23:23:37 +000073 struct master *master;
74 bool bound;
75
76 const struct component_ops *ops;
77 struct device *dev;
78};
79
80static DEFINE_MUTEX(component_mutex);
81static LIST_HEAD(component_list);
82static LIST_HEAD(masters);
83
Maciej Purski59e73852017-12-01 14:23:16 +010084#ifdef CONFIG_DEBUG_FS
85
86static struct dentry *component_debugfs_dir;
87
88static int component_devices_show(struct seq_file *s, void *data)
89{
90 struct master *m = s->private;
91 struct component_match *match = m->match;
92 size_t i;
93
94 mutex_lock(&component_mutex);
95 seq_printf(s, "%-40s %20s\n", "master name", "status");
96 seq_puts(s, "-------------------------------------------------------------\n");
97 seq_printf(s, "%-40s %20s\n\n",
98 dev_name(m->dev), m->bound ? "bound" : "not bound");
99
100 seq_printf(s, "%-40s %20s\n", "device name", "status");
101 seq_puts(s, "-------------------------------------------------------------\n");
102 for (i = 0; i < match->num; i++) {
103 struct device *d = (struct device *)match->compare[i].data;
104
105 seq_printf(s, "%-40s %20s\n", dev_name(d),
106 match->compare[i].component ?
107 "registered" : "not registered");
108 }
109 mutex_unlock(&component_mutex);
110
111 return 0;
112}
113
Yangtao Lic0b8a872018-12-15 03:36:36 -0500114DEFINE_SHOW_ATTRIBUTE(component_devices);
Maciej Purski59e73852017-12-01 14:23:16 +0100115
116static int __init component_debug_init(void)
117{
118 component_debugfs_dir = debugfs_create_dir("device_component", NULL);
119
120 return 0;
121}
122
123core_initcall(component_debug_init);
124
125static void component_master_debugfs_add(struct master *m)
126{
127 m->dentry = debugfs_create_file(dev_name(m->dev), 0444,
128 component_debugfs_dir,
129 m, &component_devices_fops);
130}
131
132static void component_master_debugfs_del(struct master *m)
133{
134 debugfs_remove(m->dentry);
135 m->dentry = NULL;
136}
137
138#else
139
140static void component_master_debugfs_add(struct master *m)
141{ }
142
143static void component_master_debugfs_del(struct master *m)
144{ }
145
146#endif
147
Russell King2a41e602014-01-10 23:23:37 +0000148static struct master *__master_find(struct device *dev,
149 const struct component_master_ops *ops)
150{
151 struct master *m;
152
153 list_for_each_entry(m, &masters, node)
154 if (m->dev == dev && (!ops || m->ops == ops))
155 return m;
156
157 return NULL;
158}
159
Russell Kingffc30b72014-04-18 23:05:53 +0100160static struct component *find_component(struct master *master,
Russell King2a41e602014-01-10 23:23:37 +0000161 int (*compare)(struct device *, void *), void *compare_data)
162{
163 struct component *c;
Russell King2a41e602014-01-10 23:23:37 +0000164
165 list_for_each_entry(c, &component_list, node) {
Russell Kingfcbcebc2014-04-18 20:16:22 +0100166 if (c->master && c->master != master)
Russell King2a41e602014-01-10 23:23:37 +0000167 continue;
168
Russell Kingffc30b72014-04-18 23:05:53 +0100169 if (compare(c->dev, compare_data))
170 return c;
Russell King2a41e602014-01-10 23:23:37 +0000171 }
172
Russell Kingffc30b72014-04-18 23:05:53 +0100173 return NULL;
Russell King2a41e602014-01-10 23:23:37 +0000174}
Russell King2a41e602014-01-10 23:23:37 +0000175
Russell King6955b582014-04-19 11:18:01 +0100176static int find_components(struct master *master)
177{
178 struct component_match *match = master->match;
179 size_t i;
180 int ret = 0;
181
Russell King6955b582014-04-19 11:18:01 +0100182 /*
183 * Scan the array of match functions and attach
184 * any components which are found to this master.
185 */
186 for (i = 0; i < match->num; i++) {
Russell Kingce657b12015-11-17 12:08:01 +0000187 struct component_match_array *mc = &match->compare[i];
Russell Kingffc30b72014-04-18 23:05:53 +0100188 struct component *c;
189
190 dev_dbg(master->dev, "Looking for component %zu\n", i);
191
192 if (match->compare[i].component)
193 continue;
194
Russell Kingce657b12015-11-17 12:08:01 +0000195 c = find_component(master, mc->compare, mc->data);
Russell Kingffc30b72014-04-18 23:05:53 +0100196 if (!c) {
197 ret = -ENXIO;
Russell King6955b582014-04-19 11:18:01 +0100198 break;
Russell Kingffc30b72014-04-18 23:05:53 +0100199 }
200
201 dev_dbg(master->dev, "found component %s, duplicate %u\n", dev_name(c->dev), !!c->master);
202
203 /* Attach this component to the master */
204 match->compare[i].duplicate = !!c->master;
205 match->compare[i].component = c;
206 c->master = master;
Russell King6955b582014-04-19 11:18:01 +0100207 }
208 return ret;
209}
210
Russell Kingffc30b72014-04-18 23:05:53 +0100211/* Detach component from associated master */
212static void remove_component(struct master *master, struct component *c)
Russell King2a41e602014-01-10 23:23:37 +0000213{
Russell Kingffc30b72014-04-18 23:05:53 +0100214 size_t i;
Russell King2a41e602014-01-10 23:23:37 +0000215
Russell Kingffc30b72014-04-18 23:05:53 +0100216 /* Detach the component from this master. */
217 for (i = 0; i < master->match->num; i++)
218 if (master->match->compare[i].component == c)
219 master->match->compare[i].component = NULL;
Russell King2a41e602014-01-10 23:23:37 +0000220}
221
222/*
223 * Try to bring up a master. If component is NULL, we're interested in
224 * this master, otherwise it's a component which must be present to try
225 * and bring up the master.
226 *
227 * Returns 1 for successful bringup, 0 if not ready, or -ve errno.
228 */
229static int try_to_bring_up_master(struct master *master,
230 struct component *component)
231{
Russell Kingc3349402014-04-23 10:52:17 +0100232 int ret;
Russell King2a41e602014-01-10 23:23:37 +0000233
Russell Kingffc30b72014-04-18 23:05:53 +0100234 dev_dbg(master->dev, "trying to bring up master\n");
235
Russell King6955b582014-04-19 11:18:01 +0100236 if (find_components(master)) {
Russell Kingffc30b72014-04-18 23:05:53 +0100237 dev_dbg(master->dev, "master has incomplete components\n");
238 return 0;
Russell King2a41e602014-01-10 23:23:37 +0000239 }
Russell Kingc3349402014-04-23 10:52:17 +0100240
241 if (component && component->master != master) {
Russell Kingffc30b72014-04-18 23:05:53 +0100242 dev_dbg(master->dev, "master is not for this component (%s)\n",
243 dev_name(component->dev));
244 return 0;
Russell Kingc3349402014-04-23 10:52:17 +0100245 }
246
Russell Kingffc30b72014-04-18 23:05:53 +0100247 if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
248 return -ENOMEM;
Russell Kingc3349402014-04-23 10:52:17 +0100249
250 /* Found all components */
251 ret = master->ops->bind(master->dev);
252 if (ret < 0) {
253 devres_release_group(master->dev, NULL);
254 dev_info(master->dev, "master bind failed: %d\n", ret);
Russell Kingffc30b72014-04-18 23:05:53 +0100255 return ret;
Russell Kingc3349402014-04-23 10:52:17 +0100256 }
257
258 master->bound = true;
259 return 1;
Russell King2a41e602014-01-10 23:23:37 +0000260}
261
262static int try_to_bring_up_masters(struct component *component)
263{
264 struct master *m;
265 int ret = 0;
266
267 list_for_each_entry(m, &masters, node) {
Russell King29f1c7f2014-04-23 10:46:11 +0100268 if (!m->bound) {
269 ret = try_to_bring_up_master(m, component);
270 if (ret != 0)
271 break;
272 }
Russell King2a41e602014-01-10 23:23:37 +0000273 }
274
275 return ret;
276}
277
278static void take_down_master(struct master *master)
279{
280 if (master->bound) {
281 master->ops->unbind(master->dev);
Russell King9e1ccb42014-02-07 20:09:27 +0000282 devres_release_group(master->dev, NULL);
Russell King2a41e602014-01-10 23:23:37 +0000283 master->bound = false;
284 }
Russell King2a41e602014-01-10 23:23:37 +0000285}
286
Russell Kingce657b12015-11-17 12:08:01 +0000287static void component_match_release(struct device *master,
288 struct component_match *match)
Russell King6955b582014-04-19 11:18:01 +0100289{
Russell Kingce657b12015-11-17 12:08:01 +0000290 unsigned int i;
291
292 for (i = 0; i < match->num; i++) {
293 struct component_match_array *mc = &match->compare[i];
294
295 if (mc->release)
296 mc->release(master, mc->data);
297 }
Russell King9a4e7842016-01-26 14:49:22 +0000298
299 kfree(match->compare);
Russell King6955b582014-04-19 11:18:01 +0100300}
301
Russell Kingce657b12015-11-17 12:08:01 +0000302static void devm_component_match_release(struct device *dev, void *res)
303{
304 component_match_release(dev, res);
305}
306
307static int component_match_realloc(struct device *dev,
Russell King6955b582014-04-19 11:18:01 +0100308 struct component_match *match, size_t num)
309{
Russell Kingce657b12015-11-17 12:08:01 +0000310 struct component_match_array *new;
Russell King6955b582014-04-19 11:18:01 +0100311
Russell Kingce657b12015-11-17 12:08:01 +0000312 if (match->alloc == num)
313 return 0;
Russell King6955b582014-04-19 11:18:01 +0100314
Russell King9a4e7842016-01-26 14:49:22 +0000315 new = kmalloc_array(num, sizeof(*new), GFP_KERNEL);
Russell King6955b582014-04-19 11:18:01 +0100316 if (!new)
Russell Kingce657b12015-11-17 12:08:01 +0000317 return -ENOMEM;
Russell King6955b582014-04-19 11:18:01 +0100318
Russell Kingce657b12015-11-17 12:08:01 +0000319 if (match->compare) {
320 memcpy(new, match->compare, sizeof(*new) *
321 min(match->num, num));
Russell King9a4e7842016-01-26 14:49:22 +0000322 kfree(match->compare);
Russell King6955b582014-04-19 11:18:01 +0100323 }
Russell Kingce657b12015-11-17 12:08:01 +0000324 match->compare = new;
325 match->alloc = num;
Russell King6955b582014-04-19 11:18:01 +0100326
Russell Kingce657b12015-11-17 12:08:01 +0000327 return 0;
Russell King6955b582014-04-19 11:18:01 +0100328}
329
Daniel Vetter4d69c802019-02-08 00:27:56 +0100330/**
331 * component_match_add_release - add a component match with release callback
332 * @master: device with the aggregate driver
333 * @matchptr: pointer to the list of component matches
334 * @release: release function for @compare_data
335 * @compare: compare function to match against all components
336 * @compare_data: opaque pointer passed to the @compare function
Russell King6955b582014-04-19 11:18:01 +0100337 *
Daniel Vetter4d69c802019-02-08 00:27:56 +0100338 * Adds a new component match to the list stored in @matchptr, which the @master
339 * aggregate driver needs to function. The list of component matches pointed to
340 * by @matchptr must be initialized to NULL before adding the first match.
341 *
342 * The allocated match list in @matchptr is automatically released using devm
343 * actions, where upon @release will be called to free any references held by
344 * @compare_data, e.g. when @compare_data is a &device_node that must be
345 * released with of_node_put().
346 *
347 * See also component_match_add().
Russell King6955b582014-04-19 11:18:01 +0100348 */
Russell Kingce657b12015-11-17 12:08:01 +0000349void component_match_add_release(struct device *master,
350 struct component_match **matchptr,
351 void (*release)(struct device *, void *),
Russell King6955b582014-04-19 11:18:01 +0100352 int (*compare)(struct device *, void *), void *compare_data)
353{
354 struct component_match *match = *matchptr;
355
356 if (IS_ERR(match))
357 return;
358
Russell Kingce657b12015-11-17 12:08:01 +0000359 if (!match) {
360 match = devres_alloc(devm_component_match_release,
361 sizeof(*match), GFP_KERNEL);
362 if (!match) {
363 *matchptr = ERR_PTR(-ENOMEM);
364 return;
365 }
Russell King6955b582014-04-19 11:18:01 +0100366
Russell Kingce657b12015-11-17 12:08:01 +0000367 devres_add(master, match);
Russell King6955b582014-04-19 11:18:01 +0100368
369 *matchptr = match;
Russell King6955b582014-04-19 11:18:01 +0100370 }
371
Russell Kingce657b12015-11-17 12:08:01 +0000372 if (match->num == match->alloc) {
Sudip Mukherjee4877bb92016-02-02 12:57:53 +0530373 size_t new_size = match->alloc + 16;
Russell Kingce657b12015-11-17 12:08:01 +0000374 int ret;
375
376 ret = component_match_realloc(master, match, new_size);
377 if (ret) {
378 *matchptr = ERR_PTR(ret);
379 return;
380 }
381 }
382
383 match->compare[match->num].compare = compare;
384 match->compare[match->num].release = release;
Russell King6955b582014-04-19 11:18:01 +0100385 match->compare[match->num].data = compare_data;
Russell Kingffc30b72014-04-18 23:05:53 +0100386 match->compare[match->num].component = NULL;
Russell King6955b582014-04-19 11:18:01 +0100387 match->num++;
388}
Russell Kingce657b12015-11-17 12:08:01 +0000389EXPORT_SYMBOL(component_match_add_release);
Russell King6955b582014-04-19 11:18:01 +0100390
Jon Medhurst (Tixy)57480482016-01-26 17:59:13 +0000391static void free_master(struct master *master)
392{
393 struct component_match *match = master->match;
394 int i;
395
Maciej Purski59e73852017-12-01 14:23:16 +0100396 component_master_debugfs_del(master);
Jon Medhurst (Tixy)57480482016-01-26 17:59:13 +0000397 list_del(&master->node);
398
399 if (match) {
400 for (i = 0; i < match->num; i++) {
401 struct component *c = match->compare[i].component;
402 if (c)
403 c->master = NULL;
404 }
405 }
406
407 kfree(master);
408}
409
Daniel Vetter4d69c802019-02-08 00:27:56 +0100410/**
411 * component_master_add_with_match - register an aggregate driver
412 * @dev: device with the aggregate driver
413 * @ops: callbacks for the aggregate driver
414 * @match: component match list for the aggregate driver
415 *
416 * Registers a new aggregate driver consisting of the components added to @match
417 * by calling one of the component_match_add() functions. Once all components in
418 * @match are available, it will be assembled by calling
419 * &component_master_ops.bind from @ops. Must be unregistered by calling
420 * component_master_del().
421 */
Russell King6955b582014-04-19 11:18:01 +0100422int component_master_add_with_match(struct device *dev,
423 const struct component_master_ops *ops,
424 struct component_match *match)
Russell King2a41e602014-01-10 23:23:37 +0000425{
426 struct master *master;
427 int ret;
428
Russell Kingfae9e2e2014-04-18 22:10:32 +0100429 /* Reallocate the match array for its true size */
Russell Kingce657b12015-11-17 12:08:01 +0000430 ret = component_match_realloc(dev, match, match->num);
431 if (ret)
432 return ret;
Russell King6955b582014-04-19 11:18:01 +0100433
Russell King2a41e602014-01-10 23:23:37 +0000434 master = kzalloc(sizeof(*master), GFP_KERNEL);
435 if (!master)
436 return -ENOMEM;
437
438 master->dev = dev;
439 master->ops = ops;
Russell King6955b582014-04-19 11:18:01 +0100440 master->match = match;
Russell King2a41e602014-01-10 23:23:37 +0000441
Maciej Purski59e73852017-12-01 14:23:16 +0100442 component_master_debugfs_add(master);
Russell King2a41e602014-01-10 23:23:37 +0000443 /* Add to the list of available masters. */
444 mutex_lock(&component_mutex);
445 list_add(&master->node, &masters);
446
447 ret = try_to_bring_up_master(master, NULL);
448
Jon Medhurst (Tixy)57480482016-01-26 17:59:13 +0000449 if (ret < 0)
450 free_master(master);
451
Russell King2a41e602014-01-10 23:23:37 +0000452 mutex_unlock(&component_mutex);
453
454 return ret < 0 ? ret : 0;
455}
Russell King6955b582014-04-19 11:18:01 +0100456EXPORT_SYMBOL_GPL(component_master_add_with_match);
457
Daniel Vetter4d69c802019-02-08 00:27:56 +0100458/**
459 * component_master_del - unregister an aggregate driver
460 * @dev: device with the aggregate driver
461 * @ops: callbacks for the aggregate driver
462 *
463 * Unregisters an aggregate driver registered with
464 * component_master_add_with_match(). If necessary the aggregate driver is first
465 * disassembled by calling &component_master_ops.unbind from @ops.
466 */
Russell King2a41e602014-01-10 23:23:37 +0000467void component_master_del(struct device *dev,
468 const struct component_master_ops *ops)
469{
470 struct master *master;
471
472 mutex_lock(&component_mutex);
473 master = __master_find(dev, ops);
474 if (master) {
475 take_down_master(master);
Jon Medhurst (Tixy)57480482016-01-26 17:59:13 +0000476 free_master(master);
Russell King2a41e602014-01-10 23:23:37 +0000477 }
478 mutex_unlock(&component_mutex);
479}
480EXPORT_SYMBOL_GPL(component_master_del);
481
482static void component_unbind(struct component *component,
483 struct master *master, void *data)
484{
485 WARN_ON(!component->bound);
486
487 component->ops->unbind(component->dev, master->dev, data);
488 component->bound = false;
489
490 /* Release all resources claimed in the binding of this component */
491 devres_release_group(component->dev, component);
492}
493
Daniel Vetter4d69c802019-02-08 00:27:56 +0100494/**
495 * component_unbind_all - unbind all component to an aggregate driver
496 * @master_dev: device with the aggregate driver
497 * @data: opaque pointer, passed to all components
498 *
499 * Unbinds all components to the aggregate @dev by passing @data to their
500 * &component_ops.unbind functions. Should be called from
501 * &component_master_ops.unbind.
502 */
Russell King2a41e602014-01-10 23:23:37 +0000503void component_unbind_all(struct device *master_dev, void *data)
504{
505 struct master *master;
506 struct component *c;
Russell Kingffc30b72014-04-18 23:05:53 +0100507 size_t i;
Russell King2a41e602014-01-10 23:23:37 +0000508
509 WARN_ON(!mutex_is_locked(&component_mutex));
510
511 master = __master_find(master_dev, NULL);
512 if (!master)
513 return;
514
Russell Kingffc30b72014-04-18 23:05:53 +0100515 /* Unbind components in reverse order */
516 for (i = master->match->num; i--; )
517 if (!master->match->compare[i].duplicate) {
518 c = master->match->compare[i].component;
519 component_unbind(c, master, data);
520 }
Russell King2a41e602014-01-10 23:23:37 +0000521}
522EXPORT_SYMBOL_GPL(component_unbind_all);
523
524static int component_bind(struct component *component, struct master *master,
525 void *data)
526{
527 int ret;
528
529 /*
530 * Each component initialises inside its own devres group.
531 * This allows us to roll-back a failed component without
532 * affecting anything else.
533 */
534 if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
535 return -ENOMEM;
536
537 /*
538 * Also open a group for the device itself: this allows us
539 * to release the resources claimed against the sub-device
540 * at the appropriate moment.
541 */
542 if (!devres_open_group(component->dev, component, GFP_KERNEL)) {
543 devres_release_group(master->dev, NULL);
544 return -ENOMEM;
545 }
546
547 dev_dbg(master->dev, "binding %s (ops %ps)\n",
548 dev_name(component->dev), component->ops);
549
550 ret = component->ops->bind(component->dev, master->dev, data);
551 if (!ret) {
552 component->bound = true;
553
554 /*
555 * Close the component device's group so that resources
556 * allocated in the binding are encapsulated for removal
557 * at unbind. Remove the group on the DRM device as we
558 * can clean those resources up independently.
559 */
560 devres_close_group(component->dev, NULL);
561 devres_remove_group(master->dev, NULL);
562
563 dev_info(master->dev, "bound %s (ops %ps)\n",
564 dev_name(component->dev), component->ops);
565 } else {
566 devres_release_group(component->dev, NULL);
567 devres_release_group(master->dev, NULL);
568
569 dev_err(master->dev, "failed to bind %s (ops %ps): %d\n",
570 dev_name(component->dev), component->ops, ret);
571 }
572
573 return ret;
574}
575
Daniel Vetter4d69c802019-02-08 00:27:56 +0100576/**
577 * component_bind_all - bind all component to an aggregate driver
578 * @master_dev: device with the aggregate driver
579 * @data: opaque pointer, passed to all components
580 *
581 * Binds all components to the aggregate @dev by passing @data to their
582 * &component_ops.bind functions. Should be called from
583 * &component_master_ops.bind.
584 */
Russell King2a41e602014-01-10 23:23:37 +0000585int component_bind_all(struct device *master_dev, void *data)
586{
587 struct master *master;
588 struct component *c;
Russell Kingffc30b72014-04-18 23:05:53 +0100589 size_t i;
Russell King2a41e602014-01-10 23:23:37 +0000590 int ret = 0;
591
592 WARN_ON(!mutex_is_locked(&component_mutex));
593
594 master = __master_find(master_dev, NULL);
595 if (!master)
596 return -EINVAL;
597
Russell Kingffc30b72014-04-18 23:05:53 +0100598 /* Bind components in match order */
599 for (i = 0; i < master->match->num; i++)
600 if (!master->match->compare[i].duplicate) {
601 c = master->match->compare[i].component;
602 ret = component_bind(c, master, data);
603 if (ret)
604 break;
605 }
Russell King2a41e602014-01-10 23:23:37 +0000606
607 if (ret != 0) {
Banajit Goswamibdae5662018-08-27 21:15:39 -0700608 for (; i > 0; i--)
609 if (!master->match->compare[i - 1].duplicate) {
610 c = master->match->compare[i - 1].component;
Russell Kingffc30b72014-04-18 23:05:53 +0100611 component_unbind(c, master, data);
612 }
Russell King2a41e602014-01-10 23:23:37 +0000613 }
614
615 return ret;
616}
617EXPORT_SYMBOL_GPL(component_bind_all);
618
Daniel Vetter4d69c802019-02-08 00:27:56 +0100619/**
620 * component_add - register a component
621 * @dev: component device
622 * @ops: component callbacks
623 *
624 * Register a new component for @dev. Functions in @ops will be called when the
625 * aggregate driver is ready to bind the overall driver by calling
626 * component_bind_all(). See also &struct component_ops.
627 *
628 * The component needs to be unregistered at driver unload/disconnect by calling
629 * component_del().
630 */
Russell King2a41e602014-01-10 23:23:37 +0000631int component_add(struct device *dev, const struct component_ops *ops)
632{
633 struct component *component;
634 int ret;
635
636 component = kzalloc(sizeof(*component), GFP_KERNEL);
637 if (!component)
638 return -ENOMEM;
639
640 component->ops = ops;
641 component->dev = dev;
642
643 dev_dbg(dev, "adding component (ops %ps)\n", ops);
644
645 mutex_lock(&component_mutex);
646 list_add_tail(&component->node, &component_list);
647
648 ret = try_to_bring_up_masters(component);
649 if (ret < 0) {
Daniel Stone8e7199c2016-02-08 21:12:58 +0000650 if (component->master)
651 remove_component(component->master, component);
Russell King2a41e602014-01-10 23:23:37 +0000652 list_del(&component->node);
653
654 kfree(component);
655 }
656 mutex_unlock(&component_mutex);
657
658 return ret < 0 ? ret : 0;
659}
660EXPORT_SYMBOL_GPL(component_add);
661
Daniel Vetter4d69c802019-02-08 00:27:56 +0100662/**
663 * component_del - unregister a component
664 * @dev: component device
665 * @ops: component callbacks
666 *
667 * Unregister a component added with component_add(). If the component is bound
668 * into an aggregate driver, this will force the entire aggregate driver, including
669 * all its components, to be unbound.
670 */
Russell King2a41e602014-01-10 23:23:37 +0000671void component_del(struct device *dev, const struct component_ops *ops)
672{
673 struct component *c, *component = NULL;
674
675 mutex_lock(&component_mutex);
676 list_for_each_entry(c, &component_list, node)
677 if (c->dev == dev && c->ops == ops) {
678 list_del(&c->node);
679 component = c;
680 break;
681 }
682
Russell Kingffc30b72014-04-18 23:05:53 +0100683 if (component && component->master) {
Russell King2a41e602014-01-10 23:23:37 +0000684 take_down_master(component->master);
Russell Kingffc30b72014-04-18 23:05:53 +0100685 remove_component(component->master, component);
686 }
Russell King2a41e602014-01-10 23:23:37 +0000687
688 mutex_unlock(&component_mutex);
689
690 WARN_ON(!component);
691 kfree(component);
692}
693EXPORT_SYMBOL_GPL(component_del);
694
695MODULE_LICENSE("GPL v2");