aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHeikki Krogerus <heikki.krogerus@linux.intel.com>2018-03-20 08:57:02 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2018-03-22 08:10:29 -0400
commitf2d9b66d84f3ff5ea3aff111e6a403e04fa8bf37 (patch)
treefa1d83bf1777d68f0bb53305d0f941c5886775f1
parent2699126bcf18db672451b82b26a1c8e954784fad (diff)
drivers: base: Unified device connection lookup
Several frameworks - clk, gpio, phy, pmw, etc. - maintain lookup tables for describing connections and provide custom API for handling them. This introduces a single generic lookup table and API for the connections. The motivation for this commit is centralizing the connection lookup, but the goal is to ultimately extract the connection descriptions also from firmware by using the fwnode_graph_* functions and other mechanisms that are available. Reviewed-by: Hans de Goede <hdegoede@redhat.com> Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com> Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r--Documentation/driver-api/device_connection.rst43
-rw-r--r--drivers/base/Makefile3
-rw-r--r--drivers/base/devcon.c136
-rw-r--r--include/linux/device.h22
4 files changed, 203 insertions, 1 deletions
diff --git a/Documentation/driver-api/device_connection.rst b/Documentation/driver-api/device_connection.rst
new file mode 100644
index 000000000000..affbc5566ab0
--- /dev/null
+++ b/Documentation/driver-api/device_connection.rst
@@ -0,0 +1,43 @@
1==================
2Device connections
3==================
4
5Introduction
6------------
7
8Devices often have connections to other devices that are outside of the direct
9child/parent relationship. A serial or network communication controller, which
10could be a PCI device, may need to be able to get a reference to its PHY
11component, which could be attached for example to the I2C bus. Some device
12drivers need to be able to control the clocks or the GPIOs for their devices,
13and so on.
14
15Device connections are generic descriptions of any type of connection between
16two separate devices.
17
18Device connections alone do not create a dependency between the two devices.
19They are only descriptions which are not tied to either of the devices directly.
20A dependency between the two devices exists only if one of the two endpoint
21devices requests a reference to the other. The descriptions themselves can be
22defined in firmware (not yet supported) or they can be built-in.
23
24Usage
25-----
26
27Device connections should exist before device ``->probe`` callback is called for
28either endpoint device in the description. If the connections are defined in
29firmware, this is not a problem. It should be considered if the connection
30descriptions are "built-in", and need to be added separately.
31
32The connection description consists of the names of the two devices with the
33connection, i.e. the endpoints, and unique identifier for the connection which
34is needed if there are multiple connections between the two devices.
35
36After a description exists, the devices in it can request reference to the other
37endpoint device, or they can request the description itself.
38
39API
40---
41
42.. kernel-doc:: drivers/base/devcon.c
43 : functions: device_connection_find_match device_connection_find device_connection_add device_connection_remove
diff --git a/drivers/base/Makefile b/drivers/base/Makefile
index e32a52490051..12a7f64d35a9 100644
--- a/drivers/base/Makefile
+++ b/drivers/base/Makefile
@@ -5,7 +5,8 @@ obj-y := component.o core.o bus.o dd.o syscore.o \
5 driver.o class.o platform.o \ 5 driver.o class.o platform.o \
6 cpu.o firmware.o init.o map.o devres.o \ 6 cpu.o firmware.o init.o map.o devres.o \
7 attribute_container.o transport_class.o \ 7 attribute_container.o transport_class.o \
8 topology.o container.o property.o cacheinfo.o 8 topology.o container.o property.o cacheinfo.o \
9 devcon.o
9obj-$(CONFIG_DEVTMPFS) += devtmpfs.o 10obj-$(CONFIG_DEVTMPFS) += devtmpfs.o
10obj-$(CONFIG_DMA_CMA) += dma-contiguous.o 11obj-$(CONFIG_DMA_CMA) += dma-contiguous.o
11obj-y += power/ 12obj-y += power/
diff --git a/drivers/base/devcon.c b/drivers/base/devcon.c
new file mode 100644
index 000000000000..d427e806cd73
--- /dev/null
+++ b/drivers/base/devcon.c
@@ -0,0 +1,136 @@
1// SPDX-License-Identifier: GPL-2.0
2/**
3 * Device connections
4 *
5 * Copyright (C) 2018 Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7 */
8
9#include <linux/device.h>
10
11static DEFINE_MUTEX(devcon_lock);
12static LIST_HEAD(devcon_list);
13
14/**
15 * device_connection_find_match - Find physical connection to a device
16 * @dev: Device with the connection
17 * @con_id: Identifier for the connection
18 * @data: Data for the match function
19 * @match: Function to check and convert the connection description
20 *
21 * Find a connection with unique identifier @con_id between @dev and another
22 * device. @match will be used to convert the connection description to data the
23 * caller is expecting to be returned.
24 */
25void *device_connection_find_match(struct device *dev, const char *con_id,
26 void *data,
27 void *(*match)(struct device_connection *con,
28 int ep, void *data))
29{
30 const char *devname = dev_name(dev);
31 struct device_connection *con;
32 void *ret = NULL;
33 int ep;
34
35 if (!match)
36 return NULL;
37
38 mutex_lock(&devcon_lock);
39
40 list_for_each_entry(con, &devcon_list, list) {
41 ep = match_string(con->endpoint, 2, devname);
42 if (ep < 0)
43 continue;
44
45 if (con_id && strcmp(con->id, con_id))
46 continue;
47
48 ret = match(con, !ep, data);
49 if (ret)
50 break;
51 }
52
53 mutex_unlock(&devcon_lock);
54
55 return ret;
56}
57EXPORT_SYMBOL_GPL(device_connection_find_match);
58
59extern struct bus_type platform_bus_type;
60extern struct bus_type pci_bus_type;
61extern struct bus_type i2c_bus_type;
62extern struct bus_type spi_bus_type;
63
64static struct bus_type *generic_match_buses[] = {
65 &platform_bus_type,
66#ifdef CONFIG_PCI
67 &pci_bus_type,
68#endif
69#ifdef CONFIG_I2C
70 &i2c_bus_type,
71#endif
72#ifdef CONFIG_SPI_MASTER
73 &spi_bus_type,
74#endif
75 NULL,
76};
77
78/* This tries to find the device from the most common bus types by name. */
79static void *generic_match(struct device_connection *con, int ep, void *data)
80{
81 struct bus_type *bus;
82 struct device *dev;
83
84 for (bus = generic_match_buses[0]; bus; bus++) {
85 dev = bus_find_device_by_name(bus, NULL, con->endpoint[ep]);
86 if (dev)
87 return dev;
88 }
89
90 /*
91 * We only get called if a connection was found, tell the caller to
92 * wait for the other device to show up.
93 */
94 return ERR_PTR(-EPROBE_DEFER);
95}
96
97/**
98 * device_connection_find - Find two devices connected together
99 * @dev: Device with the connection
100 * @con_id: Identifier for the connection
101 *
102 * Find a connection with unique identifier @con_id between @dev and
103 * another device. On success returns handle to the device that is connected
104 * to @dev, with the reference count for the found device incremented. Returns
105 * NULL if no matching connection was found, or ERR_PTR(-EPROBE_DEFER) when a
106 * connection was found but the other device has not been enumerated yet.
107 */
108struct device *device_connection_find(struct device *dev, const char *con_id)
109{
110 return device_connection_find_match(dev, con_id, NULL, generic_match);
111}
112EXPORT_SYMBOL_GPL(device_connection_find);
113
114/**
115 * device_connection_add - Register a connection description
116 * @con: The connection description to be registered
117 */
118void device_connection_add(struct device_connection *con)
119{
120 mutex_lock(&devcon_lock);
121 list_add_tail(&con->list, &devcon_list);
122 mutex_unlock(&devcon_lock);
123}
124EXPORT_SYMBOL_GPL(device_connection_add);
125
126/**
127 * device_connections_remove - Unregister connection description
128 * @con: The connection description to be unregistered
129 */
130void device_connection_remove(struct device_connection *con)
131{
132 mutex_lock(&devcon_lock);
133 list_del(&con->list);
134 mutex_unlock(&devcon_lock);
135}
136EXPORT_SYMBOL_GPL(device_connection_remove);
diff --git a/include/linux/device.h b/include/linux/device.h
index b093405ed525..204ff64279fd 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -730,6 +730,28 @@ struct device_dma_parameters {
730}; 730};
731 731
732/** 732/**
733 * struct device_connection - Device Connection Descriptor
734 * @endpoint: The names of the two devices connected together
735 * @id: Unique identifier for the connection
736 * @list: List head, private, for internal use only
737 */
738struct device_connection {
739 const char *endpoint[2];
740 const char *id;
741 struct list_head list;
742};
743
744void *device_connection_find_match(struct device *dev, const char *con_id,
745 void *data,
746 void *(*match)(struct device_connection *con,
747 int ep, void *data));
748
749struct device *device_connection_find(struct device *dev, const char *con_id);
750
751void device_connection_add(struct device_connection *con);
752void device_connection_remove(struct device_connection *con);
753
754/**
733 * enum device_link_state - Device link states. 755 * enum device_link_state - Device link states.
734 * @DL_STATE_NONE: The presence of the drivers is not being tracked. 756 * @DL_STATE_NONE: The presence of the drivers is not being tracked.
735 * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present. 757 * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present.