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
);
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 * device_connection_find_match - Find physical connection to a device
43 * @dev: Device with the connection
44 * @con_id: Identifier for the connection
45 * @data: Data for the match function
46 * @match: Function to check and convert the connection description
48 * Find a connection with unique identifier @con_id between @dev and another
49 * device. @match will be used to convert the connection description to data the
50 * caller is expecting to be returned.
52 void *device_connection_find_match(struct device
*dev
, const char *con_id
,
53 void *data
, devcon_match_fn_t match
)
55 struct fwnode_handle
*fwnode
= dev_fwnode(dev
);
56 const char *devname
= dev_name(dev
);
57 struct device_connection
*con
;
65 ret
= fwnode_graph_devcon_match(fwnode
, con_id
, data
, match
);
70 mutex_lock(&devcon_lock
);
72 list_for_each_entry(con
, &devcon_list
, list
) {
73 ep
= match_string(con
->endpoint
, 2, devname
);
77 if (con_id
&& strcmp(con
->id
, con_id
))
80 ret
= match(con
, !ep
, data
);
85 mutex_unlock(&devcon_lock
);
89 EXPORT_SYMBOL_GPL(device_connection_find_match
);
91 extern struct bus_type platform_bus_type
;
92 extern struct bus_type pci_bus_type
;
93 extern struct bus_type i2c_bus_type
;
94 extern struct bus_type spi_bus_type
;
96 static struct bus_type
*generic_match_buses
[] = {
104 #ifdef CONFIG_SPI_MASTER
110 static int device_fwnode_match(struct device
*dev
, void *fwnode
)
112 return dev_fwnode(dev
) == fwnode
;
115 static void *device_connection_fwnode_match(struct device_connection
*con
)
117 struct bus_type
*bus
;
120 for (bus
= generic_match_buses
[0]; bus
; bus
++) {
121 dev
= bus_find_device(bus
, NULL
, (void *)con
->fwnode
,
122 device_fwnode_match
);
123 if (dev
&& !strncmp(dev_name(dev
), con
->id
, strlen(con
->id
)))
131 /* This tries to find the device from the most common bus types by name. */
132 static void *generic_match(struct device_connection
*con
, int ep
, void *data
)
134 struct bus_type
*bus
;
138 return device_connection_fwnode_match(con
);
140 for (bus
= generic_match_buses
[0]; bus
; bus
++) {
141 dev
= bus_find_device_by_name(bus
, NULL
, con
->endpoint
[ep
]);
147 * We only get called if a connection was found, tell the caller to
148 * wait for the other device to show up.
150 return ERR_PTR(-EPROBE_DEFER
);
154 * device_connection_find - Find two devices connected together
155 * @dev: Device with the connection
156 * @con_id: Identifier for the connection
158 * Find a connection with unique identifier @con_id between @dev and
159 * another device. On success returns handle to the device that is connected
160 * to @dev, with the reference count for the found device incremented. Returns
161 * NULL if no matching connection was found, or ERR_PTR(-EPROBE_DEFER) when a
162 * connection was found but the other device has not been enumerated yet.
164 struct device
*device_connection_find(struct device
*dev
, const char *con_id
)
166 return device_connection_find_match(dev
, con_id
, NULL
, generic_match
);
168 EXPORT_SYMBOL_GPL(device_connection_find
);
171 * device_connection_add - Register a connection description
172 * @con: The connection description to be registered
174 void device_connection_add(struct device_connection
*con
)
176 mutex_lock(&devcon_lock
);
177 list_add_tail(&con
->list
, &devcon_list
);
178 mutex_unlock(&devcon_lock
);
180 EXPORT_SYMBOL_GPL(device_connection_add
);
183 * device_connections_remove - Unregister connection description
184 * @con: The connection description to be unregistered
186 void device_connection_remove(struct device_connection
*con
)
188 mutex_lock(&devcon_lock
);
189 list_del(&con
->list
);
190 mutex_unlock(&devcon_lock
);
192 EXPORT_SYMBOL_GPL(device_connection_remove
);