Path: blob/master/drivers/gpu/drm/display/drm_dp_aux_bus.c
26494 views
// SPDX-License-Identifier: GPL-2.01/*2* Copyright 2021 Google Inc.3*4* The DP AUX bus is used for devices that are connected over a DisplayPort5* AUX bus. The device on the far side of the bus is referred to as an6* endpoint in this code.7*8* There is only one device connected to the DP AUX bus: an eDP panel.9* Though historically panels (even DP panels) have been modeled as simple10* platform devices, putting them under the DP AUX bus allows the panel driver11* to perform transactions on that bus.12*/1314#include <linux/export.h>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>2122#include <drm/display/drm_dp_aux_bus.h>23#include <drm/display/drm_dp_helper.h>2425struct dp_aux_ep_device_with_data {26struct dp_aux_ep_device aux_ep;27int (*done_probing)(struct drm_dp_aux *aux);28};2930/**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.34*35* At the moment, we just match on device tree.36*37* Return: True if this driver matches this device; false otherwise.38*/39static int dp_aux_ep_match(struct device *dev, const struct device_driver *drv)40{41return !!of_match_device(drv->of_match_table, dev);42}4344/**45* dp_aux_ep_probe() - The probe function for the dp_aux_bus.46* @dev: The device to probe.47*48* Calls through to the endpoint driver probe.49*50* Return: 0 if no error or negative error code.51*/52static int dp_aux_ep_probe(struct device *dev)53{54struct dp_aux_ep_driver *aux_ep_drv = to_dp_aux_ep_drv(dev->driver);55struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(dev);56struct dp_aux_ep_device_with_data *aux_ep_with_data =57container_of(aux_ep, struct dp_aux_ep_device_with_data, aux_ep);58int ret;5960ret = dev_pm_domain_attach(dev, PD_FLAG_ATTACH_POWER_ON);61if (ret)62return dev_err_probe(dev, ret, "Failed to attach to PM Domain\n");6364ret = aux_ep_drv->probe(aux_ep);65if (ret)66goto err_attached;6768if (aux_ep_with_data->done_probing) {69ret = aux_ep_with_data->done_probing(aux_ep->aux);70if (ret) {71/*72* The done_probing() callback should not return73* -EPROBE_DEFER to us. If it does, we treat it as an74* error. Passing it on as-is would cause the _panel_75* to defer.76*/77if (ret == -EPROBE_DEFER) {78dev_err(dev,79"DP AUX done_probing() can't defer\n");80ret = -EINVAL;81}82goto err_probed;83}84}8586return 0;87err_probed:88if (aux_ep_drv->remove)89aux_ep_drv->remove(aux_ep);90err_attached:91dev_pm_domain_detach(dev, true);9293return ret;94}9596/**97* dp_aux_ep_remove() - The remove function for the dp_aux_bus.98* @dev: The device to remove.99*100* Calls through to the endpoint driver remove.101*/102static void dp_aux_ep_remove(struct device *dev)103{104struct dp_aux_ep_driver *aux_ep_drv = to_dp_aux_ep_drv(dev->driver);105struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(dev);106107if (aux_ep_drv->remove)108aux_ep_drv->remove(aux_ep);109dev_pm_domain_detach(dev, true);110}111112/**113* dp_aux_ep_shutdown() - The shutdown function for the dp_aux_bus.114* @dev: The device to shutdown.115*116* Calls through to the endpoint driver shutdown.117*/118static void dp_aux_ep_shutdown(struct device *dev)119{120struct dp_aux_ep_driver *aux_ep_drv;121122if (!dev->driver)123return;124125aux_ep_drv = to_dp_aux_ep_drv(dev->driver);126if (aux_ep_drv->shutdown)127aux_ep_drv->shutdown(to_dp_aux_ep_dev(dev));128}129130static const struct bus_type dp_aux_bus_type = {131.name = "dp-aux",132.match = dp_aux_ep_match,133.probe = dp_aux_ep_probe,134.remove = dp_aux_ep_remove,135.shutdown = dp_aux_ep_shutdown,136};137138static ssize_t modalias_show(struct device *dev,139struct device_attribute *attr, char *buf)140{141return of_device_modalias(dev, buf, PAGE_SIZE);142}143static DEVICE_ATTR_RO(modalias);144145static struct attribute *dp_aux_ep_dev_attrs[] = {146&dev_attr_modalias.attr,147NULL,148};149ATTRIBUTE_GROUPS(dp_aux_ep_dev);150151/**152* dp_aux_ep_dev_release() - Free memory for the dp_aux_ep device153* @dev: The device to free.154*/155static void dp_aux_ep_dev_release(struct device *dev)156{157struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(dev);158struct dp_aux_ep_device_with_data *aux_ep_with_data =159container_of(aux_ep, struct dp_aux_ep_device_with_data, aux_ep);160161kfree(aux_ep_with_data);162}163164static int dp_aux_ep_dev_modalias(const struct device *dev, struct kobj_uevent_env *env)165{166return of_device_uevent_modalias(dev, env);167}168169static struct device_type dp_aux_device_type_type = {170.groups = dp_aux_ep_dev_groups,171.uevent = dp_aux_ep_dev_modalias,172.release = dp_aux_ep_dev_release,173};174175/**176* of_dp_aux_ep_destroy() - Destroy an DP AUX endpoint device177* @dev: The device to destroy.178* @data: Not used179*180* This is just used as a callback by of_dp_aux_depopulate_bus() and181* is called for _all_ of the child devices of the device providing the AUX bus.182* We'll only act on those that are of type "dp_aux_bus_type".183*184* This function is effectively an inverse of what's in185* of_dp_aux_populate_bus(). NOTE: since we only populate one child186* then it's expected that only one device will match all the "if" tests in187* this function and get to the device_unregister().188*189* Return: 0 if no error or negative error code.190*/191static int of_dp_aux_ep_destroy(struct device *dev, void *data)192{193struct device_node *np = dev->of_node;194195if (dev->bus != &dp_aux_bus_type)196return 0;197198if (!of_node_check_flag(np, OF_POPULATED))199return 0;200201of_node_clear_flag(np, OF_POPULATED);202of_node_put(np);203204device_unregister(dev);205206return 0;207}208209/**210* of_dp_aux_depopulate_bus() - Undo of_dp_aux_populate_bus211* @aux: The AUX channel whose device we want to depopulate212*213* This will destroy the device that was created214* by of_dp_aux_populate_bus().215*/216void of_dp_aux_depopulate_bus(struct drm_dp_aux *aux)217{218device_for_each_child_reverse(aux->dev, NULL, of_dp_aux_ep_destroy);219}220EXPORT_SYMBOL_GPL(of_dp_aux_depopulate_bus);221222/**223* of_dp_aux_populate_bus() - Populate the endpoint device on the DP AUX224* @aux: The AUX channel whose device we want to populate. It is required that225* drm_dp_aux_init() has already been called for this AUX channel.226* @done_probing: Callback functions to call after EP device finishes probing.227* Will not be called if there are no EP devices and this228* function will return -ENODEV.229*230* This will populate the device (expected to be an eDP panel) under the231* "aux-bus" node of the device providing the AUX channel (AKA aux->dev).232*233* When this function finishes, it is _possible_ (but not guaranteed) that234* our sub-device will have finished probing. It should be noted that if our235* sub-device returns -EPROBE_DEFER or is probing asynchronously for some236* reason that we will not return any error codes ourselves but our237* sub-device will _not_ have actually probed successfully yet.238*239* In many cases it's important for the caller of this function to be notified240* when our sub device finishes probing. Our sub device is expected to be an241* eDP panel and the caller is expected to be an eDP controller. The eDP242* controller needs to be able to get a reference to the panel when it finishes243* probing. For this reason the caller can pass in a function pointer that244* will be called when our sub-device finishes probing.245*246* If this function succeeds you should later make sure you call247* of_dp_aux_depopulate_bus() to undo it, or just use the devm version248* of this function.249*250* Return: 0 if no error or negative error code; returns -ENODEV if there are251* no children. The done_probing() function won't be called in that252* case.253*/254int of_dp_aux_populate_bus(struct drm_dp_aux *aux,255int (*done_probing)(struct drm_dp_aux *aux))256{257struct device_node *bus = NULL, *np = NULL;258struct dp_aux_ep_device *aux_ep;259struct dp_aux_ep_device_with_data *aux_ep_with_data;260int ret;261262/* drm_dp_aux_init() should have been called already; warn if not */263WARN_ON_ONCE(!aux->ddc.algo);264265if (!aux->dev->of_node)266return -ENODEV;267bus = of_get_child_by_name(aux->dev->of_node, "aux-bus");268if (!bus)269return -ENODEV;270271np = of_get_next_available_child(bus, NULL);272of_node_put(bus);273if (!np)274return -ENODEV;275276if (of_node_test_and_set_flag(np, OF_POPULATED)) {277dev_err(aux->dev, "DP AUX EP device already populated\n");278ret = -EINVAL;279goto err_did_get_np;280}281282aux_ep_with_data = kzalloc(sizeof(*aux_ep_with_data), GFP_KERNEL);283if (!aux_ep_with_data) {284ret = -ENOMEM;285goto err_did_set_populated;286}287288aux_ep_with_data->done_probing = done_probing;289290aux_ep = &aux_ep_with_data->aux_ep;291aux_ep->aux = aux;292aux_ep->dev.parent = aux->dev;293aux_ep->dev.bus = &dp_aux_bus_type;294aux_ep->dev.type = &dp_aux_device_type_type;295device_set_node(&aux_ep->dev, of_fwnode_handle(of_node_get(np)));296dev_set_name(&aux_ep->dev, "aux-%s", dev_name(aux->dev));297298ret = device_register(&aux_ep->dev);299if (ret) {300dev_err(aux->dev, "Failed to create AUX EP for %pOF: %d\n", np, ret);301302/*303* As per docs of device_register(), call this instead304* of kfree() directly for error cases.305*/306put_device(&aux_ep->dev);307308goto err_did_set_populated;309}310311return 0;312313err_did_set_populated:314of_node_clear_flag(np, OF_POPULATED);315316err_did_get_np:317of_node_put(np);318319return ret;320}321EXPORT_SYMBOL_GPL(of_dp_aux_populate_bus);322323static void of_dp_aux_depopulate_bus_void(void *data)324{325of_dp_aux_depopulate_bus(data);326}327328/**329* devm_of_dp_aux_populate_bus() - devm wrapper for of_dp_aux_populate_bus()330* @aux: The AUX channel whose device we want to populate331* @done_probing: Callback functions to call after EP device finishes probing.332* Will not be called if there are no EP devices and this333* function will return -ENODEV.334*335* Handles freeing w/ devm on the device "aux->dev".336*337* Return: 0 if no error or negative error code; returns -ENODEV if there are338* no children. The done_probing() function won't be called in that339* case.340*/341int devm_of_dp_aux_populate_bus(struct drm_dp_aux *aux,342int (*done_probing)(struct drm_dp_aux *aux))343{344int ret;345346ret = of_dp_aux_populate_bus(aux, done_probing);347if (ret)348return ret;349350return devm_add_action_or_reset(aux->dev,351of_dp_aux_depopulate_bus_void, aux);352}353EXPORT_SYMBOL_GPL(devm_of_dp_aux_populate_bus);354355int __dp_aux_dp_driver_register(struct dp_aux_ep_driver *drv, struct module *owner)356{357drv->driver.owner = owner;358drv->driver.bus = &dp_aux_bus_type;359360return driver_register(&drv->driver);361}362EXPORT_SYMBOL_GPL(__dp_aux_dp_driver_register);363364void dp_aux_dp_driver_unregister(struct dp_aux_ep_driver *drv)365{366driver_unregister(&drv->driver);367}368EXPORT_SYMBOL_GPL(dp_aux_dp_driver_unregister);369370static int __init dp_aux_bus_init(void)371{372int ret;373374ret = bus_register(&dp_aux_bus_type);375if (ret)376return ret;377378return 0;379}380381static void __exit dp_aux_bus_exit(void)382{383bus_unregister(&dp_aux_bus_type);384}385386subsys_initcall(dp_aux_bus_init);387module_exit(dp_aux_bus_exit);388389MODULE_AUTHOR("Douglas Anderson <[email protected]>");390MODULE_DESCRIPTION("DRM DisplayPort AUX bus");391MODULE_LICENSE("GPL v2");392393394