1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright 2021 Google Inc. 4 * 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. 8 * 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. 13 */ 14 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> 21 22 #include <drm/display/drm_dp_aux_bus.h> 23 #include <drm/display/drm_dp_helper.h> 24 25 /** 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. 29 * 30 * At the moment, we just match on device tree. 31 * 32 * Return: True if this driver matches this device; false otherwise. 33 */ 34 static int dp_aux_ep_match(struct device *dev, struct device_driver *drv) 35 { 36 return !!of_match_device(drv->of_match_table, dev); 37 } 38 39 /** 40 * dp_aux_ep_probe() - The probe function for the dp_aux_bus. 41 * @dev: The device to probe. 42 * 43 * Calls through to the endpoint driver probe. 44 * 45 * Return: 0 if no error or negative error code. 46 */ 47 static int dp_aux_ep_probe(struct device *dev) 48 { 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); 51 int ret; 52 53 ret = dev_pm_domain_attach(dev, true); 54 if (ret) 55 return dev_err_probe(dev, ret, "Failed to attach to PM Domain\n"); 56 57 ret = aux_ep_drv->probe(aux_ep); 58 if (ret) 59 dev_pm_domain_detach(dev, true); 60 61 return ret; 62 } 63 64 /** 65 * dp_aux_ep_remove() - The remove function for the dp_aux_bus. 66 * @dev: The device to remove. 67 * 68 * Calls through to the endpoint driver remove. 69 */ 70 static void dp_aux_ep_remove(struct device *dev) 71 { 72 struct dp_aux_ep_driver *aux_ep_drv = to_dp_aux_ep_drv(dev->driver); 73 struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(dev); 74 75 if (aux_ep_drv->remove) 76 aux_ep_drv->remove(aux_ep); 77 dev_pm_domain_detach(dev, true); 78 } 79 80 /** 81 * dp_aux_ep_shutdown() - The shutdown function for the dp_aux_bus. 82 * @dev: The device to shutdown. 83 * 84 * Calls through to the endpoint driver shutdown. 85 */ 86 static void dp_aux_ep_shutdown(struct device *dev) 87 { 88 struct dp_aux_ep_driver *aux_ep_drv; 89 90 if (!dev->driver) 91 return; 92 93 aux_ep_drv = to_dp_aux_ep_drv(dev->driver); 94 if (aux_ep_drv->shutdown) 95 aux_ep_drv->shutdown(to_dp_aux_ep_dev(dev)); 96 } 97 98 static struct bus_type dp_aux_bus_type = { 99 .name = "dp-aux", 100 .match = dp_aux_ep_match, 101 .probe = dp_aux_ep_probe, 102 .remove = dp_aux_ep_remove, 103 .shutdown = dp_aux_ep_shutdown, 104 }; 105 106 static ssize_t modalias_show(struct device *dev, 107 struct device_attribute *attr, char *buf) 108 { 109 return of_device_modalias(dev, buf, PAGE_SIZE); 110 } 111 static DEVICE_ATTR_RO(modalias); 112 113 static struct attribute *dp_aux_ep_dev_attrs[] = { 114 &dev_attr_modalias.attr, 115 NULL, 116 }; 117 ATTRIBUTE_GROUPS(dp_aux_ep_dev); 118 119 /** 120 * dp_aux_ep_dev_release() - Free memory for the dp_aux_ep device 121 * @dev: The device to free. 122 */ 123 static void dp_aux_ep_dev_release(struct device *dev) 124 { 125 kfree(to_dp_aux_ep_dev(dev)); 126 } 127 128 static struct device_type dp_aux_device_type_type = { 129 .groups = dp_aux_ep_dev_groups, 130 .uevent = of_device_uevent_modalias, 131 .release = dp_aux_ep_dev_release, 132 }; 133 134 /** 135 * of_dp_aux_ep_destroy() - Destroy an DP AUX endpoint device 136 * @dev: The device to destroy. 137 * @data: Not used 138 * 139 * This is just used as a callback by of_dp_aux_depopulate_ep_devices() and 140 * is called for _all_ of the child devices of the device providing the AUX bus. 141 * We'll only act on those that are of type "dp_aux_bus_type". 142 * 143 * This function is effectively an inverse of what's in the loop 144 * in of_dp_aux_populate_ep_devices(). 145 * 146 * Return: 0 if no error or negative error code. 147 */ 148 static int of_dp_aux_ep_destroy(struct device *dev, void *data) 149 { 150 struct device_node *np = dev->of_node; 151 152 if (dev->bus != &dp_aux_bus_type) 153 return 0; 154 155 if (!of_node_check_flag(np, OF_POPULATED)) 156 return 0; 157 158 of_node_clear_flag(np, OF_POPULATED); 159 of_node_put(np); 160 161 device_unregister(dev); 162 163 return 0; 164 } 165 166 /** 167 * of_dp_aux_depopulate_ep_devices() - Undo of_dp_aux_populate_ep_devices 168 * @aux: The AUX channel whose devices we want to depopulate 169 * 170 * This will destroy all devices that were created 171 * by of_dp_aux_populate_ep_devices(). 172 */ 173 void of_dp_aux_depopulate_ep_devices(struct drm_dp_aux *aux) 174 { 175 device_for_each_child_reverse(aux->dev, NULL, of_dp_aux_ep_destroy); 176 } 177 EXPORT_SYMBOL_GPL(of_dp_aux_depopulate_ep_devices); 178 179 /** 180 * of_dp_aux_populate_ep_devices() - Populate the endpoint devices on the DP AUX 181 * @aux: The AUX channel whose devices we want to populate. It is required that 182 * drm_dp_aux_init() has already been called for this AUX channel. 183 * 184 * This will populate all the devices under the "aux-bus" node of the device 185 * providing the AUX channel (AKA aux->dev). 186 * 187 * When this function finishes, it is _possible_ (but not guaranteed) that 188 * our sub-devices will have finished probing. It should be noted that if our 189 * sub-devices return -EPROBE_DEFER that we will not return any error codes 190 * ourselves but our sub-devices will _not_ have actually probed successfully 191 * yet. There may be other cases (maybe added in the future?) where sub-devices 192 * won't have been probed yet when this function returns, so it's best not to 193 * rely on that. 194 * 195 * If this function succeeds you should later make sure you call 196 * of_dp_aux_depopulate_ep_devices() to undo it, or just use the devm version 197 * of this function. 198 * 199 * Return: 0 if no error or negative error code. 200 */ 201 int of_dp_aux_populate_ep_devices(struct drm_dp_aux *aux) 202 { 203 struct device_node *bus, *np; 204 struct dp_aux_ep_device *aux_ep; 205 int ret; 206 207 /* drm_dp_aux_init() should have been called already; warn if not */ 208 WARN_ON_ONCE(!aux->ddc.algo); 209 210 if (!aux->dev->of_node) 211 return 0; 212 213 bus = of_get_child_by_name(aux->dev->of_node, "aux-bus"); 214 if (!bus) 215 return 0; 216 217 for_each_available_child_of_node(bus, np) { 218 if (of_node_test_and_set_flag(np, OF_POPULATED)) 219 continue; 220 221 aux_ep = kzalloc(sizeof(*aux_ep), GFP_KERNEL); 222 if (!aux_ep) 223 continue; 224 aux_ep->aux = aux; 225 226 aux_ep->dev.parent = aux->dev; 227 aux_ep->dev.bus = &dp_aux_bus_type; 228 aux_ep->dev.type = &dp_aux_device_type_type; 229 aux_ep->dev.of_node = of_node_get(np); 230 dev_set_name(&aux_ep->dev, "aux-%s", dev_name(aux->dev)); 231 232 ret = device_register(&aux_ep->dev); 233 if (ret) { 234 dev_err(aux->dev, "Failed to create AUX EP for %pOF: %d\n", np, ret); 235 of_node_clear_flag(np, OF_POPULATED); 236 of_node_put(np); 237 238 /* 239 * As per docs of device_register(), call this instead 240 * of kfree() directly for error cases. 241 */ 242 put_device(&aux_ep->dev); 243 244 /* 245 * Following in the footsteps of of_i2c_register_devices(), 246 * we won't fail the whole function here--we'll just 247 * continue registering any other devices we find. 248 */ 249 } 250 } 251 252 of_node_put(bus); 253 254 return 0; 255 } 256 EXPORT_SYMBOL_GPL(of_dp_aux_populate_ep_devices); 257 258 static void of_dp_aux_depopulate_ep_devices_void(void *data) 259 { 260 of_dp_aux_depopulate_ep_devices(data); 261 } 262 263 /** 264 * devm_of_dp_aux_populate_ep_devices() - devm wrapper for of_dp_aux_populate_ep_devices() 265 * @aux: The AUX channel whose devices we want to populate 266 * 267 * Handles freeing w/ devm on the device "aux->dev". 268 * 269 * Return: 0 if no error or negative error code. 270 */ 271 int devm_of_dp_aux_populate_ep_devices(struct drm_dp_aux *aux) 272 { 273 int ret; 274 275 ret = of_dp_aux_populate_ep_devices(aux); 276 if (ret) 277 return ret; 278 279 return devm_add_action_or_reset(aux->dev, 280 of_dp_aux_depopulate_ep_devices_void, 281 aux); 282 } 283 EXPORT_SYMBOL_GPL(devm_of_dp_aux_populate_ep_devices); 284 285 int __dp_aux_dp_driver_register(struct dp_aux_ep_driver *drv, struct module *owner) 286 { 287 drv->driver.owner = owner; 288 drv->driver.bus = &dp_aux_bus_type; 289 290 return driver_register(&drv->driver); 291 } 292 EXPORT_SYMBOL_GPL(__dp_aux_dp_driver_register); 293 294 void dp_aux_dp_driver_unregister(struct dp_aux_ep_driver *drv) 295 { 296 driver_unregister(&drv->driver); 297 } 298 EXPORT_SYMBOL_GPL(dp_aux_dp_driver_unregister); 299 300 static int __init dp_aux_bus_init(void) 301 { 302 int ret; 303 304 ret = bus_register(&dp_aux_bus_type); 305 if (ret) 306 return ret; 307 308 return 0; 309 } 310 311 static void __exit dp_aux_bus_exit(void) 312 { 313 bus_unregister(&dp_aux_bus_type); 314 } 315 316 subsys_initcall(dp_aux_bus_init); 317 module_exit(dp_aux_bus_exit); 318 319 MODULE_AUTHOR("Douglas Anderson <dianders@chromium.org>"); 320 MODULE_DESCRIPTION("DRM DisplayPort AUX bus"); 321 MODULE_LICENSE("GPL v2"); 322