1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2018 Intel Corporation
9 #include <linux/device.h>
10 #include <linux/property.h>
12 static DEFINE_MUTEX(devcon_lock);
13 static LIST_HEAD(devcon_list);
15 typedef void *(*devcon_match_fn_t)(struct device_connection *con, int ep,
19 fwnode_graph_devcon_match(struct fwnode_handle *fwnode, const char *con_id,
20 void *data, devcon_match_fn_t match)
22 struct device_connection con = { .id = con_id };
23 struct fwnode_handle *ep;
26 fwnode_graph_for_each_endpoint(fwnode, ep) {
27 con.fwnode = fwnode_graph_get_remote_port_parent(ep);
28 if (!fwnode_device_is_available(con.fwnode))
31 ret = match(&con, -1, data);
32 fwnode_handle_put(con.fwnode);
34 fwnode_handle_put(ep);
42 fwnode_devcon_match(struct fwnode_handle *fwnode, const char *con_id,
43 void *data, devcon_match_fn_t match)
45 struct device_connection con = { };
50 con.fwnode = fwnode_find_reference(fwnode, con_id, i);
51 if (IS_ERR(con.fwnode))
54 ret = match(&con, -1, data);
55 fwnode_handle_put(con.fwnode);
64 * device_connection_find_match - Find physical connection to a device
65 * @dev: Device with the connection
66 * @con_id: Identifier for the connection
67 * @data: Data for the match function
68 * @match: Function to check and convert the connection description
70 * Find a connection with unique identifier @con_id between @dev and another
71 * device. @match will be used to convert the connection description to data the
72 * caller is expecting to be returned.
74 void *device_connection_find_match(struct device *dev, const char *con_id,
75 void *data, devcon_match_fn_t match)
77 struct fwnode_handle *fwnode = dev_fwnode(dev);
78 const char *devname = dev_name(dev);
79 struct device_connection *con;
87 ret = fwnode_graph_devcon_match(fwnode, con_id, data, match);
91 ret = fwnode_devcon_match(fwnode, con_id, data, match);
96 mutex_lock(&devcon_lock);
98 list_for_each_entry(con, &devcon_list, list) {
99 ep = match_string(con->endpoint, 2, devname);
103 if (con_id && strcmp(con->id, con_id))
106 ret = match(con, !ep, data);
111 mutex_unlock(&devcon_lock);
115 EXPORT_SYMBOL_GPL(device_connection_find_match);
117 extern struct bus_type platform_bus_type;
118 extern struct bus_type pci_bus_type;
119 extern struct bus_type i2c_bus_type;
120 extern struct bus_type spi_bus_type;
122 static struct bus_type *generic_match_buses[] = {
130 #ifdef CONFIG_SPI_MASTER
136 static int device_fwnode_match(struct device *dev, const void *fwnode)
138 return dev_fwnode(dev) == fwnode;
141 static void *device_connection_fwnode_match(struct device_connection *con)
143 struct bus_type *bus;
146 for (bus = generic_match_buses[0]; bus; bus++) {
147 dev = bus_find_device(bus, NULL, (void *)con->fwnode,
148 device_fwnode_match);
149 if (dev && !strncmp(dev_name(dev), con->id, strlen(con->id)))
157 /* This tries to find the device from the most common bus types by name. */
158 static void *generic_match(struct device_connection *con, int ep, void *data)
160 struct bus_type *bus;
164 return device_connection_fwnode_match(con);
166 for (bus = generic_match_buses[0]; bus; bus++) {
167 dev = bus_find_device_by_name(bus, NULL, con->endpoint[ep]);
173 * We only get called if a connection was found, tell the caller to
174 * wait for the other device to show up.
176 return ERR_PTR(-EPROBE_DEFER);
180 * device_connection_find - Find two devices connected together
181 * @dev: Device with the connection
182 * @con_id: Identifier for the connection
184 * Find a connection with unique identifier @con_id between @dev and
185 * another device. On success returns handle to the device that is connected
186 * to @dev, with the reference count for the found device incremented. Returns
187 * NULL if no matching connection was found, or ERR_PTR(-EPROBE_DEFER) when a
188 * connection was found but the other device has not been enumerated yet.
190 struct device *device_connection_find(struct device *dev, const char *con_id)
192 return device_connection_find_match(dev, con_id, NULL, generic_match);
194 EXPORT_SYMBOL_GPL(device_connection_find);
197 * device_connection_add - Register a connection description
198 * @con: The connection description to be registered
200 void device_connection_add(struct device_connection *con)
202 mutex_lock(&devcon_lock);
203 list_add_tail(&con->list, &devcon_list);
204 mutex_unlock(&devcon_lock);
206 EXPORT_SYMBOL_GPL(device_connection_add);
209 * device_connections_remove - Unregister connection description
210 * @con: The connection description to be unregistered
212 void device_connection_remove(struct device_connection *con)
214 mutex_lock(&devcon_lock);
215 list_del(&con->list);
216 mutex_unlock(&devcon_lock);
218 EXPORT_SYMBOL_GPL(device_connection_remove);