Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright 2021 Google Inc.
0004  *
0005  * The DP AUX bus is used for devices that are connected over a DisplayPort
0006  * AUX bus. The device on the far side of the bus is referred to as an
0007  * endpoint in this code.
0008  *
0009  * There is only one device connected to the DP AUX bus: an eDP panel.
0010  * Though historically panels (even DP panels) have been modeled as simple
0011  * platform devices, putting them under the DP AUX bus allows the panel driver
0012  * to perform transactions on that bus.
0013  */
0014 
0015 #include <linux/init.h>
0016 #include <linux/kernel.h>
0017 #include <linux/module.h>
0018 #include <linux/of_device.h>
0019 #include <linux/pm_domain.h>
0020 #include <linux/pm_runtime.h>
0021 
0022 #include <drm/display/drm_dp_aux_bus.h>
0023 #include <drm/display/drm_dp_helper.h>
0024 
0025 struct dp_aux_ep_device_with_data {
0026     struct dp_aux_ep_device aux_ep;
0027     int (*done_probing)(struct drm_dp_aux *aux);
0028 };
0029 
0030 /**
0031  * dp_aux_ep_match() - The match function for the dp_aux_bus.
0032  * @dev: The device to match.
0033  * @drv: The driver to try to match against.
0034  *
0035  * At the moment, we just match on device tree.
0036  *
0037  * Return: True if this driver matches this device; false otherwise.
0038  */
0039 static int dp_aux_ep_match(struct device *dev, struct device_driver *drv)
0040 {
0041     return !!of_match_device(drv->of_match_table, dev);
0042 }
0043 
0044 /**
0045  * dp_aux_ep_probe() - The probe function for the dp_aux_bus.
0046  * @dev: The device to probe.
0047  *
0048  * Calls through to the endpoint driver probe.
0049  *
0050  * Return: 0 if no error or negative error code.
0051  */
0052 static int dp_aux_ep_probe(struct device *dev)
0053 {
0054     struct dp_aux_ep_driver *aux_ep_drv = to_dp_aux_ep_drv(dev->driver);
0055     struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(dev);
0056     struct dp_aux_ep_device_with_data *aux_ep_with_data =
0057         container_of(aux_ep, struct dp_aux_ep_device_with_data, aux_ep);
0058     int ret;
0059 
0060     ret = dev_pm_domain_attach(dev, true);
0061     if (ret)
0062         return dev_err_probe(dev, ret, "Failed to attach to PM Domain\n");
0063 
0064     ret = aux_ep_drv->probe(aux_ep);
0065     if (ret)
0066         goto err_attached;
0067 
0068     if (aux_ep_with_data->done_probing) {
0069         ret = aux_ep_with_data->done_probing(aux_ep->aux);
0070         if (ret) {
0071             /*
0072              * The done_probing() callback should not return
0073              * -EPROBE_DEFER to us. If it does, we treat it as an
0074              * error. Passing it on as-is would cause the _panel_
0075              * to defer.
0076              */
0077             if (ret == -EPROBE_DEFER) {
0078                 dev_err(dev,
0079                     "DP AUX done_probing() can't defer\n");
0080                 ret = -EINVAL;
0081             }
0082             goto err_probed;
0083         }
0084     }
0085 
0086     return 0;
0087 err_probed:
0088     if (aux_ep_drv->remove)
0089         aux_ep_drv->remove(aux_ep);
0090 err_attached:
0091     dev_pm_domain_detach(dev, true);
0092 
0093     return ret;
0094 }
0095 
0096 /**
0097  * dp_aux_ep_remove() - The remove function for the dp_aux_bus.
0098  * @dev: The device to remove.
0099  *
0100  * Calls through to the endpoint driver remove.
0101  */
0102 static void dp_aux_ep_remove(struct device *dev)
0103 {
0104     struct dp_aux_ep_driver *aux_ep_drv = to_dp_aux_ep_drv(dev->driver);
0105     struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(dev);
0106 
0107     if (aux_ep_drv->remove)
0108         aux_ep_drv->remove(aux_ep);
0109     dev_pm_domain_detach(dev, true);
0110 }
0111 
0112 /**
0113  * dp_aux_ep_shutdown() - The shutdown function for the dp_aux_bus.
0114  * @dev: The device to shutdown.
0115  *
0116  * Calls through to the endpoint driver shutdown.
0117  */
0118 static void dp_aux_ep_shutdown(struct device *dev)
0119 {
0120     struct dp_aux_ep_driver *aux_ep_drv;
0121 
0122     if (!dev->driver)
0123         return;
0124 
0125     aux_ep_drv = to_dp_aux_ep_drv(dev->driver);
0126     if (aux_ep_drv->shutdown)
0127         aux_ep_drv->shutdown(to_dp_aux_ep_dev(dev));
0128 }
0129 
0130 static struct bus_type dp_aux_bus_type = {
0131     .name       = "dp-aux",
0132     .match      = dp_aux_ep_match,
0133     .probe      = dp_aux_ep_probe,
0134     .remove     = dp_aux_ep_remove,
0135     .shutdown   = dp_aux_ep_shutdown,
0136 };
0137 
0138 static ssize_t modalias_show(struct device *dev,
0139                  struct device_attribute *attr, char *buf)
0140 {
0141     return of_device_modalias(dev, buf, PAGE_SIZE);
0142 }
0143 static DEVICE_ATTR_RO(modalias);
0144 
0145 static struct attribute *dp_aux_ep_dev_attrs[] = {
0146     &dev_attr_modalias.attr,
0147     NULL,
0148 };
0149 ATTRIBUTE_GROUPS(dp_aux_ep_dev);
0150 
0151 /**
0152  * dp_aux_ep_dev_release() - Free memory for the dp_aux_ep device
0153  * @dev: The device to free.
0154  */
0155 static void dp_aux_ep_dev_release(struct device *dev)
0156 {
0157     struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(dev);
0158     struct dp_aux_ep_device_with_data *aux_ep_with_data =
0159         container_of(aux_ep, struct dp_aux_ep_device_with_data, aux_ep);
0160 
0161     kfree(aux_ep_with_data);
0162 }
0163 
0164 static struct device_type dp_aux_device_type_type = {
0165     .groups     = dp_aux_ep_dev_groups,
0166     .uevent     = of_device_uevent_modalias,
0167     .release    = dp_aux_ep_dev_release,
0168 };
0169 
0170 /**
0171  * of_dp_aux_ep_destroy() - Destroy an DP AUX endpoint device
0172  * @dev: The device to destroy.
0173  * @data: Not used
0174  *
0175  * This is just used as a callback by of_dp_aux_depopulate_bus() and
0176  * is called for _all_ of the child devices of the device providing the AUX bus.
0177  * We'll only act on those that are of type "dp_aux_bus_type".
0178  *
0179  * This function is effectively an inverse of what's in
0180  * of_dp_aux_populate_bus(). NOTE: since we only populate one child
0181  * then it's expected that only one device will match all the "if" tests in
0182  * this function and get to the device_unregister().
0183  *
0184  * Return: 0 if no error or negative error code.
0185  */
0186 static int of_dp_aux_ep_destroy(struct device *dev, void *data)
0187 {
0188     struct device_node *np = dev->of_node;
0189 
0190     if (dev->bus != &dp_aux_bus_type)
0191         return 0;
0192 
0193     if (!of_node_check_flag(np, OF_POPULATED))
0194         return 0;
0195 
0196     of_node_clear_flag(np, OF_POPULATED);
0197     of_node_put(np);
0198 
0199     device_unregister(dev);
0200 
0201     return 0;
0202 }
0203 
0204 /**
0205  * of_dp_aux_depopulate_bus() - Undo of_dp_aux_populate_bus
0206  * @aux: The AUX channel whose device we want to depopulate
0207  *
0208  * This will destroy the device that was created
0209  * by of_dp_aux_populate_bus().
0210  */
0211 void of_dp_aux_depopulate_bus(struct drm_dp_aux *aux)
0212 {
0213     device_for_each_child_reverse(aux->dev, NULL, of_dp_aux_ep_destroy);
0214 }
0215 EXPORT_SYMBOL_GPL(of_dp_aux_depopulate_bus);
0216 
0217 /**
0218  * of_dp_aux_populate_bus() - Populate the endpoint device on the DP AUX
0219  * @aux: The AUX channel whose device we want to populate. It is required that
0220  *       drm_dp_aux_init() has already been called for this AUX channel.
0221  * @done_probing: Callback functions to call after EP device finishes probing.
0222  *                Will not be called if there are no EP devices and this
0223  *                function will return -ENODEV.
0224  *
0225  * This will populate the device (expected to be an eDP panel) under the
0226  * "aux-bus" node of the device providing the AUX channel (AKA aux->dev).
0227  *
0228  * When this function finishes, it is _possible_ (but not guaranteed) that
0229  * our sub-device will have finished probing. It should be noted that if our
0230  * sub-device returns -EPROBE_DEFER or is probing asynchronously for some
0231  * reason that we will not return any error codes ourselves but our
0232  * sub-device will _not_ have actually probed successfully yet.
0233  *
0234  * In many cases it's important for the caller of this function to be notified
0235  * when our sub device finishes probing. Our sub device is expected to be an
0236  * eDP panel and the caller is expected to be an eDP controller. The eDP
0237  * controller needs to be able to get a reference to the panel when it finishes
0238  * probing. For this reason the caller can pass in a function pointer that
0239  * will be called when our sub-device finishes probing.
0240  *
0241  * If this function succeeds you should later make sure you call
0242  * of_dp_aux_depopulate_bus() to undo it, or just use the devm version
0243  * of this function.
0244  *
0245  * Return: 0 if no error or negative error code; returns -ENODEV if there are
0246  *         no children. The done_probing() function won't be called in that
0247  *         case.
0248  */
0249 int of_dp_aux_populate_bus(struct drm_dp_aux *aux,
0250                int (*done_probing)(struct drm_dp_aux *aux))
0251 {
0252     struct device_node *bus = NULL, *np = NULL;
0253     struct dp_aux_ep_device *aux_ep;
0254     struct dp_aux_ep_device_with_data *aux_ep_with_data;
0255     int ret;
0256 
0257     /* drm_dp_aux_init() should have been called already; warn if not */
0258     WARN_ON_ONCE(!aux->ddc.algo);
0259 
0260     if (!aux->dev->of_node)
0261         return -ENODEV;
0262     bus = of_get_child_by_name(aux->dev->of_node, "aux-bus");
0263     if (!bus)
0264         return -ENODEV;
0265 
0266     np = of_get_next_available_child(bus, NULL);
0267     of_node_put(bus);
0268     if (!np)
0269         return -ENODEV;
0270 
0271     if (of_node_test_and_set_flag(np, OF_POPULATED)) {
0272         dev_err(aux->dev, "DP AUX EP device already populated\n");
0273         ret = -EINVAL;
0274         goto err_did_get_np;
0275     }
0276 
0277     aux_ep_with_data = kzalloc(sizeof(*aux_ep_with_data), GFP_KERNEL);
0278     if (!aux_ep_with_data) {
0279         ret = -ENOMEM;
0280         goto err_did_set_populated;
0281     }
0282 
0283     aux_ep_with_data->done_probing = done_probing;
0284 
0285     aux_ep = &aux_ep_with_data->aux_ep;
0286     aux_ep->aux = aux;
0287     aux_ep->dev.parent = aux->dev;
0288     aux_ep->dev.bus = &dp_aux_bus_type;
0289     aux_ep->dev.type = &dp_aux_device_type_type;
0290     aux_ep->dev.of_node = of_node_get(np);
0291     dev_set_name(&aux_ep->dev, "aux-%s", dev_name(aux->dev));
0292 
0293     ret = device_register(&aux_ep->dev);
0294     if (ret) {
0295         dev_err(aux->dev, "Failed to create AUX EP for %pOF: %d\n", np, ret);
0296 
0297         /*
0298          * As per docs of device_register(), call this instead
0299          * of kfree() directly for error cases.
0300          */
0301         put_device(&aux_ep->dev);
0302 
0303         goto err_did_set_populated;
0304     }
0305 
0306     return 0;
0307 
0308 err_did_set_populated:
0309     of_node_clear_flag(np, OF_POPULATED);
0310 
0311 err_did_get_np:
0312     of_node_put(np);
0313 
0314     return ret;
0315 }
0316 EXPORT_SYMBOL_GPL(of_dp_aux_populate_bus);
0317 
0318 static void of_dp_aux_depopulate_bus_void(void *data)
0319 {
0320     of_dp_aux_depopulate_bus(data);
0321 }
0322 
0323 /**
0324  * devm_of_dp_aux_populate_bus() - devm wrapper for of_dp_aux_populate_bus()
0325  * @aux: The AUX channel whose device we want to populate
0326  * @done_probing: Callback functions to call after EP device finishes probing.
0327  *                Will not be called if there are no EP devices and this
0328  *                function will return -ENODEV.
0329  *
0330  * Handles freeing w/ devm on the device "aux->dev".
0331  *
0332  * Return: 0 if no error or negative error code; returns -ENODEV if there are
0333  *         no children. The done_probing() function won't be called in that
0334  *         case.
0335  */
0336 int devm_of_dp_aux_populate_bus(struct drm_dp_aux *aux,
0337                 int (*done_probing)(struct drm_dp_aux *aux))
0338 {
0339     int ret;
0340 
0341     ret = of_dp_aux_populate_bus(aux, done_probing);
0342     if (ret)
0343         return ret;
0344 
0345     return devm_add_action_or_reset(aux->dev,
0346                     of_dp_aux_depopulate_bus_void, aux);
0347 }
0348 EXPORT_SYMBOL_GPL(devm_of_dp_aux_populate_bus);
0349 
0350 int __dp_aux_dp_driver_register(struct dp_aux_ep_driver *drv, struct module *owner)
0351 {
0352     drv->driver.owner = owner;
0353     drv->driver.bus = &dp_aux_bus_type;
0354 
0355     return driver_register(&drv->driver);
0356 }
0357 EXPORT_SYMBOL_GPL(__dp_aux_dp_driver_register);
0358 
0359 void dp_aux_dp_driver_unregister(struct dp_aux_ep_driver *drv)
0360 {
0361     driver_unregister(&drv->driver);
0362 }
0363 EXPORT_SYMBOL_GPL(dp_aux_dp_driver_unregister);
0364 
0365 static int __init dp_aux_bus_init(void)
0366 {
0367     int ret;
0368 
0369     ret = bus_register(&dp_aux_bus_type);
0370     if (ret)
0371         return ret;
0372 
0373     return 0;
0374 }
0375 
0376 static void __exit dp_aux_bus_exit(void)
0377 {
0378     bus_unregister(&dp_aux_bus_type);
0379 }
0380 
0381 subsys_initcall(dp_aux_bus_init);
0382 module_exit(dp_aux_bus_exit);
0383 
0384 MODULE_AUTHOR("Douglas Anderson <dianders@chromium.org>");
0385 MODULE_DESCRIPTION("DRM DisplayPort AUX bus");
0386 MODULE_LICENSE("GPL v2");