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 device on the far side of the bus is referred to as an
7 * endpoint in this code.
9 * There is only one device connected to the DP AUX bus: an eDP 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/display/drm_dp_aux_bus.h>
23 #include <drm/display/drm_dp_helper.h>
25 struct dp_aux_ep_device_with_data {
26 struct dp_aux_ep_device aux_ep;
27 int (*done_probing)(struct drm_dp_aux *aux);
31 * dp_aux_ep_match() - The match function for the dp_aux_bus.
32 * @dev: The device to match.
33 * @drv: The driver to try to match against.
35 * At the moment, we just match on device tree.
37 * Return: True if this driver matches this device; false otherwise.
39 static int dp_aux_ep_match(struct device *dev, struct device_driver *drv)
41 return !!of_match_device(drv->of_match_table, dev);
45 * dp_aux_ep_probe() - The probe function for the dp_aux_bus.
46 * @dev: The device to probe.
48 * Calls through to the endpoint driver probe.
50 * Return: 0 if no error or negative error code.
52 static int dp_aux_ep_probe(struct device *dev)
54 struct dp_aux_ep_driver *aux_ep_drv = to_dp_aux_ep_drv(dev->driver);
55 struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(dev);
56 struct dp_aux_ep_device_with_data *aux_ep_with_data =
57 container_of(aux_ep, struct dp_aux_ep_device_with_data, aux_ep);
60 ret = dev_pm_domain_attach(dev, true);
62 return dev_err_probe(dev, ret, "Failed to attach to PM Domain\n");
64 ret = aux_ep_drv->probe(aux_ep);
68 if (aux_ep_with_data->done_probing) {
69 ret = aux_ep_with_data->done_probing(aux_ep->aux);
72 * The done_probing() callback should not return
73 * -EPROBE_DEFER to us. If it does, we treat it as an
74 * error. Passing it on as-is would cause the _panel_
77 if (ret == -EPROBE_DEFER) {
79 "DP AUX done_probing() can't defer\n");
88 if (aux_ep_drv->remove)
89 aux_ep_drv->remove(aux_ep);
91 dev_pm_domain_detach(dev, true);
97 * dp_aux_ep_remove() - The remove function for the dp_aux_bus.
98 * @dev: The device to remove.
100 * Calls through to the endpoint driver remove.
102 static void dp_aux_ep_remove(struct device *dev)
104 struct dp_aux_ep_driver *aux_ep_drv = to_dp_aux_ep_drv(dev->driver);
105 struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(dev);
107 if (aux_ep_drv->remove)
108 aux_ep_drv->remove(aux_ep);
109 dev_pm_domain_detach(dev, true);
113 * dp_aux_ep_shutdown() - The shutdown function for the dp_aux_bus.
114 * @dev: The device to shutdown.
116 * Calls through to the endpoint driver shutdown.
118 static void dp_aux_ep_shutdown(struct device *dev)
120 struct dp_aux_ep_driver *aux_ep_drv;
125 aux_ep_drv = to_dp_aux_ep_drv(dev->driver);
126 if (aux_ep_drv->shutdown)
127 aux_ep_drv->shutdown(to_dp_aux_ep_dev(dev));
130 static struct bus_type dp_aux_bus_type = {
132 .match = dp_aux_ep_match,
133 .probe = dp_aux_ep_probe,
134 .remove = dp_aux_ep_remove,
135 .shutdown = dp_aux_ep_shutdown,
138 static ssize_t modalias_show(struct device *dev,
139 struct device_attribute *attr, char *buf)
141 return of_device_modalias(dev, buf, PAGE_SIZE);
143 static DEVICE_ATTR_RO(modalias);
145 static struct attribute *dp_aux_ep_dev_attrs[] = {
146 &dev_attr_modalias.attr,
149 ATTRIBUTE_GROUPS(dp_aux_ep_dev);
152 * dp_aux_ep_dev_release() - Free memory for the dp_aux_ep device
153 * @dev: The device to free.
155 static void dp_aux_ep_dev_release(struct device *dev)
157 struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(dev);
158 struct dp_aux_ep_device_with_data *aux_ep_with_data =
159 container_of(aux_ep, struct dp_aux_ep_device_with_data, aux_ep);
161 kfree(aux_ep_with_data);
164 static struct device_type dp_aux_device_type_type = {
165 .groups = dp_aux_ep_dev_groups,
166 .uevent = of_device_uevent_modalias,
167 .release = dp_aux_ep_dev_release,
171 * of_dp_aux_ep_destroy() - Destroy an DP AUX endpoint device
172 * @dev: The device to destroy.
175 * This is just used as a callback by of_dp_aux_depopulate_bus() and
176 * is called for _all_ of the child devices of the device providing the AUX bus.
177 * We'll only act on those that are of type "dp_aux_bus_type".
179 * This function is effectively an inverse of what's in
180 * of_dp_aux_populate_bus(). NOTE: since we only populate one child
181 * then it's expected that only one device will match all the "if" tests in
182 * this function and get to the device_unregister().
184 * Return: 0 if no error or negative error code.
186 static int of_dp_aux_ep_destroy(struct device *dev, void *data)
188 struct device_node *np = dev->of_node;
190 if (dev->bus != &dp_aux_bus_type)
193 if (!of_node_check_flag(np, OF_POPULATED))
196 of_node_clear_flag(np, OF_POPULATED);
199 device_unregister(dev);
205 * of_dp_aux_depopulate_bus() - Undo of_dp_aux_populate_bus
206 * @aux: The AUX channel whose device we want to depopulate
208 * This will destroy the device that was created
209 * by of_dp_aux_populate_bus().
211 void of_dp_aux_depopulate_bus(struct drm_dp_aux *aux)
213 device_for_each_child_reverse(aux->dev, NULL, of_dp_aux_ep_destroy);
215 EXPORT_SYMBOL_GPL(of_dp_aux_depopulate_bus);
218 * of_dp_aux_populate_bus() - Populate the endpoint device on the DP AUX
219 * @aux: The AUX channel whose device we want to populate. It is required that
220 * drm_dp_aux_init() has already been called for this AUX channel.
221 * @done_probing: Callback functions to call after EP device finishes probing.
222 * Will not be called if there are no EP devices and this
223 * function will return -ENODEV.
225 * This will populate the device (expected to be an eDP panel) under the
226 * "aux-bus" node of the device providing the AUX channel (AKA aux->dev).
228 * When this function finishes, it is _possible_ (but not guaranteed) that
229 * our sub-device will have finished probing. It should be noted that if our
230 * sub-device returns -EPROBE_DEFER or is probing asynchronously for some
231 * reason that we will not return any error codes ourselves but our
232 * sub-device will _not_ have actually probed successfully yet.
234 * In many cases it's important for the caller of this function to be notified
235 * when our sub device finishes probing. Our sub device is expected to be an
236 * eDP panel and the caller is expected to be an eDP controller. The eDP
237 * controller needs to be able to get a reference to the panel when it finishes
238 * probing. For this reason the caller can pass in a function pointer that
239 * will be called when our sub-device finishes probing.
241 * If this function succeeds you should later make sure you call
242 * of_dp_aux_depopulate_bus() to undo it, or just use the devm version
245 * Return: 0 if no error or negative error code; returns -ENODEV if there are
246 * no children. The done_probing() function won't be called in that
249 int of_dp_aux_populate_bus(struct drm_dp_aux *aux,
250 int (*done_probing)(struct drm_dp_aux *aux))
252 struct device_node *bus = NULL, *np = NULL;
253 struct dp_aux_ep_device *aux_ep;
254 struct dp_aux_ep_device_with_data *aux_ep_with_data;
257 /* drm_dp_aux_init() should have been called already; warn if not */
258 WARN_ON_ONCE(!aux->ddc.algo);
260 if (!aux->dev->of_node)
262 bus = of_get_child_by_name(aux->dev->of_node, "aux-bus");
266 np = of_get_next_available_child(bus, NULL);
271 if (of_node_test_and_set_flag(np, OF_POPULATED)) {
272 dev_err(aux->dev, "DP AUX EP device already populated\n");
277 aux_ep_with_data = kzalloc(sizeof(*aux_ep_with_data), GFP_KERNEL);
278 if (!aux_ep_with_data) {
280 goto err_did_set_populated;
283 aux_ep_with_data->done_probing = done_probing;
285 aux_ep = &aux_ep_with_data->aux_ep;
287 aux_ep->dev.parent = aux->dev;
288 aux_ep->dev.bus = &dp_aux_bus_type;
289 aux_ep->dev.type = &dp_aux_device_type_type;
290 aux_ep->dev.of_node = of_node_get(np);
291 dev_set_name(&aux_ep->dev, "aux-%s", dev_name(aux->dev));
293 ret = device_register(&aux_ep->dev);
295 dev_err(aux->dev, "Failed to create AUX EP for %pOF: %d\n", np, ret);
298 * As per docs of device_register(), call this instead
299 * of kfree() directly for error cases.
301 put_device(&aux_ep->dev);
303 goto err_did_set_populated;
308 err_did_set_populated:
309 of_node_clear_flag(np, OF_POPULATED);
316 EXPORT_SYMBOL_GPL(of_dp_aux_populate_bus);
318 static void of_dp_aux_depopulate_bus_void(void *data)
320 of_dp_aux_depopulate_bus(data);
324 * devm_of_dp_aux_populate_bus() - devm wrapper for of_dp_aux_populate_bus()
325 * @aux: The AUX channel whose device we want to populate
326 * @done_probing: Callback functions to call after EP device finishes probing.
327 * Will not be called if there are no EP devices and this
328 * function will return -ENODEV.
330 * Handles freeing w/ devm on the device "aux->dev".
332 * Return: 0 if no error or negative error code; returns -ENODEV if there are
333 * no children. The done_probing() function won't be called in that
336 int devm_of_dp_aux_populate_bus(struct drm_dp_aux *aux,
337 int (*done_probing)(struct drm_dp_aux *aux))
341 ret = of_dp_aux_populate_bus(aux, done_probing);
345 return devm_add_action_or_reset(aux->dev,
346 of_dp_aux_depopulate_bus_void, aux);
348 EXPORT_SYMBOL_GPL(devm_of_dp_aux_populate_bus);
350 int __dp_aux_dp_driver_register(struct dp_aux_ep_driver *drv, struct module *owner)
352 drv->driver.owner = owner;
353 drv->driver.bus = &dp_aux_bus_type;
355 return driver_register(&drv->driver);
357 EXPORT_SYMBOL_GPL(__dp_aux_dp_driver_register);
359 void dp_aux_dp_driver_unregister(struct dp_aux_ep_driver *drv)
361 driver_unregister(&drv->driver);
363 EXPORT_SYMBOL_GPL(dp_aux_dp_driver_unregister);
365 static int __init dp_aux_bus_init(void)
369 ret = bus_register(&dp_aux_bus_type);
376 static void __exit dp_aux_bus_exit(void)
378 bus_unregister(&dp_aux_bus_type);
381 subsys_initcall(dp_aux_bus_init);
382 module_exit(dp_aux_bus_exit);
385 MODULE_DESCRIPTION("DRM DisplayPort AUX bus");
386 MODULE_LICENSE("GPL v2");