1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2018 Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
9 #include <linux/device.h>
10 #include <linux/property.h>
12 static DEFINE_MUTEX(devcon_lock);
13 static LIST_HEAD(devcon_list);
16 fwnode_graph_devcon_match(struct fwnode_handle *fwnode, const char *con_id,
17 void *data, devcon_match_fn_t match)
19 struct device_connection con = { .id = con_id };
20 struct fwnode_handle *ep;
23 fwnode_graph_for_each_endpoint(fwnode, ep) {
24 con.fwnode = fwnode_graph_get_remote_port_parent(ep);
25 if (!fwnode_device_is_available(con.fwnode))
28 ret = match(&con, -1, data);
29 fwnode_handle_put(con.fwnode);
31 fwnode_handle_put(ep);
39 fwnode_devcon_match(struct fwnode_handle *fwnode, const char *con_id,
40 void *data, devcon_match_fn_t match)
42 struct device_connection con = { };
47 con.fwnode = fwnode_find_reference(fwnode, con_id, i);
48 if (IS_ERR(con.fwnode))
51 ret = match(&con, -1, data);
52 fwnode_handle_put(con.fwnode);
61 * fwnode_connection_find_match - Find connection from a device node
62 * @fwnode: Device node with the connection
63 * @con_id: Identifier for the connection
64 * @data: Data for the match function
65 * @match: Function to check and convert the connection description
67 * Find a connection with unique identifier @con_id between @fwnode and another
68 * device node. @match will be used to convert the connection description to
69 * data the caller is expecting to be returned.
71 void *fwnode_connection_find_match(struct fwnode_handle *fwnode,
72 const char *con_id, void *data,
73 devcon_match_fn_t match)
77 if (!fwnode || !match)
80 ret = fwnode_graph_devcon_match(fwnode, con_id, data, match);
84 return fwnode_devcon_match(fwnode, con_id, data, match);
86 EXPORT_SYMBOL_GPL(fwnode_connection_find_match);
89 * device_connection_find_match - Find physical connection to a device
90 * @dev: Device with the connection
91 * @con_id: Identifier for the connection
92 * @data: Data for the match function
93 * @match: Function to check and convert the connection description
95 * Find a connection with unique identifier @con_id between @dev and another
96 * device. @match will be used to convert the connection description to data the
97 * caller is expecting to be returned.
99 void *device_connection_find_match(struct device *dev, const char *con_id,
100 void *data, devcon_match_fn_t match)
102 struct fwnode_handle *fwnode = dev_fwnode(dev);
103 const char *devname = dev_name(dev);
104 struct device_connection *con;
111 ret = fwnode_connection_find_match(fwnode, con_id, data, match);
115 mutex_lock(&devcon_lock);
117 list_for_each_entry(con, &devcon_list, list) {
118 ep = match_string(con->endpoint, 2, devname);
122 if (con_id && strcmp(con->id, con_id))
125 ret = match(con, !ep, data);
130 mutex_unlock(&devcon_lock);
134 EXPORT_SYMBOL_GPL(device_connection_find_match);
136 extern struct bus_type platform_bus_type;
137 extern struct bus_type pci_bus_type;
138 extern struct bus_type i2c_bus_type;
139 extern struct bus_type spi_bus_type;
141 static struct bus_type *generic_match_buses[] = {
149 #ifdef CONFIG_SPI_MASTER
155 static void *device_connection_fwnode_match(struct device_connection *con)
157 struct bus_type *bus;
160 for (bus = generic_match_buses[0]; bus; bus++) {
161 dev = bus_find_device_by_fwnode(bus, con->fwnode);
162 if (dev && !strncmp(dev_name(dev), con->id, strlen(con->id)))
170 /* This tries to find the device from the most common bus types by name. */
171 static void *generic_match(struct device_connection *con, int ep, void *data)
173 struct bus_type *bus;
177 return device_connection_fwnode_match(con);
179 for (bus = generic_match_buses[0]; bus; bus++) {
180 dev = bus_find_device_by_name(bus, NULL, con->endpoint[ep]);
186 * We only get called if a connection was found, tell the caller to
187 * wait for the other device to show up.
189 return ERR_PTR(-EPROBE_DEFER);
193 * device_connection_find - Find two devices connected together
194 * @dev: Device with the connection
195 * @con_id: Identifier for the connection
197 * Find a connection with unique identifier @con_id between @dev and
198 * another device. On success returns handle to the device that is connected
199 * to @dev, with the reference count for the found device incremented. Returns
200 * NULL if no matching connection was found, or ERR_PTR(-EPROBE_DEFER) when a
201 * connection was found but the other device has not been enumerated yet.
203 struct device *device_connection_find(struct device *dev, const char *con_id)
205 return device_connection_find_match(dev, con_id, NULL, generic_match);
207 EXPORT_SYMBOL_GPL(device_connection_find);
210 * device_connection_add - Register a connection description
211 * @con: The connection description to be registered
213 void device_connection_add(struct device_connection *con)
215 mutex_lock(&devcon_lock);
216 list_add_tail(&con->list, &devcon_list);
217 mutex_unlock(&devcon_lock);
219 EXPORT_SYMBOL_GPL(device_connection_add);
222 * device_connections_remove - Unregister connection description
223 * @con: The connection description to be unregistered
225 void device_connection_remove(struct device_connection *con)
227 mutex_lock(&devcon_lock);
228 list_del(&con->list);
229 mutex_unlock(&devcon_lock);
231 EXPORT_SYMBOL_GPL(device_connection_remove);