1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2021 Google Inc.
5 * The DP AUX bus is used for devices that are connected over a DisplayPort
6 * AUX bus. The devices on the far side of the bus are referred to as
7 * endpoints in this code.
9 * Commonly there is only one device connected to the DP AUX bus: a panel.
10 * Though historically panels (even DP panels) have been modeled as simple
11 * platform devices, putting them under the DP AUX bus allows the panel driver
12 * to perform transactions on that bus.
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/pm_domain.h>
20 #include <linux/pm_runtime.h>
22 #include <drm/drm_dp_aux_bus.h>
23 #include <drm/drm_dp_helper.h>
26 * dp_aux_ep_match() - The match function for the dp_aux_bus.
27 * @dev: The device to match.
28 * @drv: The driver to try to match against.
30 * At the moment, we just match on device tree.
32 * Return: True if this driver matches this device; false otherwise.
34 static int dp_aux_ep_match(struct device *dev, struct device_driver *drv)
36 return !!of_match_device(drv->of_match_table, dev);
40 * dp_aux_ep_probe() - The probe function for the dp_aux_bus.
41 * @dev: The device to probe.
43 * Calls through to the endpoint driver probe.
45 * Return: 0 if no error or negative error code.
47 static int dp_aux_ep_probe(struct device *dev)
49 struct dp_aux_ep_driver *aux_ep_drv = to_dp_aux_ep_drv(dev->driver);
50 struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(dev);
53 ret = dev_pm_domain_attach(dev, true);
55 return dev_err_probe(dev, ret, "Failed to attach to PM Domain\n");
57 ret = aux_ep_drv->probe(aux_ep);
59 dev_pm_domain_detach(dev, true);
65 * dp_aux_ep_remove() - The remove function for the dp_aux_bus.
66 * @dev: The device to remove.
68 * Calls through to the endpoint driver remove.
71 static void dp_aux_ep_remove(struct device *dev)
73 struct dp_aux_ep_driver *aux_ep_drv = to_dp_aux_ep_drv(dev->driver);
74 struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(dev);
76 if (aux_ep_drv->remove)
77 aux_ep_drv->remove(aux_ep);
78 dev_pm_domain_detach(dev, true);
82 * dp_aux_ep_shutdown() - The shutdown function for the dp_aux_bus.
83 * @dev: The device to shutdown.
85 * Calls through to the endpoint driver shutdown.
87 static void dp_aux_ep_shutdown(struct device *dev)
89 struct dp_aux_ep_driver *aux_ep_drv;
94 aux_ep_drv = to_dp_aux_ep_drv(dev->driver);
95 if (aux_ep_drv->shutdown)
96 aux_ep_drv->shutdown(to_dp_aux_ep_dev(dev));
99 static struct bus_type dp_aux_bus_type = {
101 .match = dp_aux_ep_match,
102 .probe = dp_aux_ep_probe,
103 .remove = dp_aux_ep_remove,
104 .shutdown = dp_aux_ep_shutdown,
107 static ssize_t modalias_show(struct device *dev,
108 struct device_attribute *attr, char *buf)
110 return of_device_modalias(dev, buf, PAGE_SIZE);
112 static DEVICE_ATTR_RO(modalias);
114 static struct attribute *dp_aux_ep_dev_attrs[] = {
115 &dev_attr_modalias.attr,
118 ATTRIBUTE_GROUPS(dp_aux_ep_dev);
121 * dp_aux_ep_dev_release() - Free memory for the dp_aux_ep device
122 * @dev: The device to free.
124 * Return: 0 if no error or negative error code.
126 static void dp_aux_ep_dev_release(struct device *dev)
128 kfree(to_dp_aux_ep_dev(dev));
131 static struct device_type dp_aux_device_type_type = {
132 .groups = dp_aux_ep_dev_groups,
133 .uevent = of_device_uevent_modalias,
134 .release = dp_aux_ep_dev_release,
138 * of_dp_aux_ep_destroy() - Destroy an DP AUX endpoint device
139 * @dev: The device to destroy.
142 * This is just used as a callback by of_dp_aux_depopulate_ep_devices() and
143 * is called for _all_ of the child devices of the device providing the AUX bus.
144 * We'll only act on those that are of type "dp_aux_bus_type".
146 * This function is effectively an inverse of what's in the loop
147 * in of_dp_aux_populate_ep_devices().
149 * Return: 0 if no error or negative error code.
151 static int of_dp_aux_ep_destroy(struct device *dev, void *data)
153 struct device_node *np = dev->of_node;
155 if (dev->bus != &dp_aux_bus_type)
158 if (!of_node_check_flag(np, OF_POPULATED))
161 of_node_clear_flag(np, OF_POPULATED);
164 device_unregister(dev);
170 * of_dp_aux_depopulate_ep_devices() - Undo of_dp_aux_populate_ep_devices
171 * @aux: The AUX channel whose devices we want to depopulate
173 * This will destroy all devices that were created
174 * by of_dp_aux_populate_ep_devices().
176 void of_dp_aux_depopulate_ep_devices(struct drm_dp_aux *aux)
178 device_for_each_child_reverse(aux->dev, NULL, of_dp_aux_ep_destroy);
180 EXPORT_SYMBOL_GPL(of_dp_aux_depopulate_ep_devices);
183 * of_dp_aux_populate_ep_devices() - Populate the endpoint devices on the DP AUX
184 * @aux: The AUX channel whose devices we want to populate. It is required that
185 * drm_dp_aux_init() has already been called for this AUX channel.
187 * This will populate all the devices under the "aux-bus" node of the device
188 * providing the AUX channel (AKA aux->dev).
190 * When this function finishes, it is _possible_ (but not guaranteed) that
191 * our sub-devices will have finished probing. It should be noted that if our
192 * sub-devices return -EPROBE_DEFER that we will not return any error codes
193 * ourselves but our sub-devices will _not_ have actually probed successfully
194 * yet. There may be other cases (maybe added in the future?) where sub-devices
195 * won't have been probed yet when this function returns, so it's best not to
198 * If this function succeeds you should later make sure you call
199 * of_dp_aux_depopulate_ep_devices() to undo it, or just use the devm version
202 * Return: 0 if no error or negative error code.
204 int of_dp_aux_populate_ep_devices(struct drm_dp_aux *aux)
206 struct device_node *bus, *np;
207 struct dp_aux_ep_device *aux_ep;
210 /* drm_dp_aux_init() should have been called already; warn if not */
211 WARN_ON_ONCE(!aux->ddc.algo);
213 if (!aux->dev->of_node)
216 bus = of_get_child_by_name(aux->dev->of_node, "aux-bus");
220 for_each_available_child_of_node(bus, np) {
221 if (of_node_test_and_set_flag(np, OF_POPULATED))
224 aux_ep = kzalloc(sizeof(*aux_ep), GFP_KERNEL);
229 aux_ep->dev.parent = aux->dev;
230 aux_ep->dev.bus = &dp_aux_bus_type;
231 aux_ep->dev.type = &dp_aux_device_type_type;
232 aux_ep->dev.of_node = of_node_get(np);
233 dev_set_name(&aux_ep->dev, "aux-%s", dev_name(aux->dev));
235 ret = device_register(&aux_ep->dev);
237 dev_err(aux->dev, "Failed to create AUX EP for %pOF: %d\n", np, ret);
238 of_node_clear_flag(np, OF_POPULATED);
242 * As per docs of device_register(), call this instead
243 * of kfree() directly for error cases.
245 put_device(&aux_ep->dev);
248 * Following in the footsteps of of_i2c_register_devices(),
249 * we won't fail the whole function here--we'll just
250 * continue registering any other devices we find.
260 static void of_dp_aux_depopulate_ep_devices_void(void *data)
262 of_dp_aux_depopulate_ep_devices(data);
266 * devm_of_dp_aux_populate_ep_devices() - devm wrapper for of_dp_aux_populate_ep_devices()
267 * @aux: The AUX channel whose devices we want to populate
269 * Handles freeing w/ devm on the device "aux->dev".
271 * Return: 0 if no error or negative error code.
273 int devm_of_dp_aux_populate_ep_devices(struct drm_dp_aux *aux)
277 ret = of_dp_aux_populate_ep_devices(aux);
281 return devm_add_action_or_reset(aux->dev,
282 of_dp_aux_depopulate_ep_devices_void,
285 EXPORT_SYMBOL_GPL(devm_of_dp_aux_populate_ep_devices);
287 int __dp_aux_dp_driver_register(struct dp_aux_ep_driver *drv, struct module *owner)
289 drv->driver.owner = owner;
290 drv->driver.bus = &dp_aux_bus_type;
292 return driver_register(&drv->driver);
294 EXPORT_SYMBOL_GPL(__dp_aux_dp_driver_register);
296 void dp_aux_dp_driver_unregister(struct dp_aux_ep_driver *drv)
298 driver_unregister(&drv->driver);
300 EXPORT_SYMBOL_GPL(dp_aux_dp_driver_unregister);
302 static int __init dp_aux_bus_init(void)
306 ret = bus_register(&dp_aux_bus_type);
313 static void __exit dp_aux_bus_exit(void)
315 bus_unregister(&dp_aux_bus_type);
318 subsys_initcall(dp_aux_bus_init);
319 module_exit(dp_aux_bus_exit);
322 MODULE_DESCRIPTION("DRM DisplayPort AUX bus");
323 MODULE_LICENSE("GPL v2");