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