drm_dp_aux_bus.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 device on the far side of the bus is referred to as an
  7. * endpoint in this code.
  8. *
  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.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/of_device.h>
  18. #include <linux/pm_domain.h>
  19. #include <linux/pm_runtime.h>
  20. #include <drm/display/drm_dp_aux_bus.h>
  21. #include <drm/display/drm_dp_helper.h>
  22. struct dp_aux_ep_device_with_data {
  23. struct dp_aux_ep_device aux_ep;
  24. int (*done_probing)(struct drm_dp_aux *aux);
  25. };
  26. /**
  27. * dp_aux_ep_match() - The match function for the dp_aux_bus.
  28. * @dev: The device to match.
  29. * @drv: The driver to try to match against.
  30. *
  31. * At the moment, we just match on device tree.
  32. *
  33. * Return: True if this driver matches this device; false otherwise.
  34. */
  35. static int dp_aux_ep_match(struct device *dev, const struct device_driver *drv)
  36. {
  37. return !!of_match_device(drv->of_match_table, dev);
  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. struct dp_aux_ep_device_with_data *aux_ep_with_data =
  52. container_of(aux_ep, struct dp_aux_ep_device_with_data, aux_ep);
  53. int ret;
  54. ret = dev_pm_domain_attach(dev, true);
  55. if (ret)
  56. return dev_err_probe(dev, ret, "Failed to attach to PM Domain\n");
  57. ret = aux_ep_drv->probe(aux_ep);
  58. if (ret)
  59. goto err_attached;
  60. if (aux_ep_with_data->done_probing) {
  61. ret = aux_ep_with_data->done_probing(aux_ep->aux);
  62. if (ret) {
  63. /*
  64. * The done_probing() callback should not return
  65. * -EPROBE_DEFER to us. If it does, we treat it as an
  66. * error. Passing it on as-is would cause the _panel_
  67. * to defer.
  68. */
  69. if (ret == -EPROBE_DEFER) {
  70. dev_err(dev,
  71. "DP AUX done_probing() can't defer\n");
  72. ret = -EINVAL;
  73. }
  74. goto err_probed;
  75. }
  76. }
  77. return 0;
  78. err_probed:
  79. if (aux_ep_drv->remove)
  80. aux_ep_drv->remove(aux_ep);
  81. err_attached:
  82. dev_pm_domain_detach(dev, true);
  83. return ret;
  84. }
  85. /**
  86. * dp_aux_ep_remove() - The remove function for the dp_aux_bus.
  87. * @dev: The device to remove.
  88. *
  89. * Calls through to the endpoint driver remove.
  90. */
  91. static void dp_aux_ep_remove(struct device *dev)
  92. {
  93. struct dp_aux_ep_driver *aux_ep_drv = to_dp_aux_ep_drv(dev->driver);
  94. struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(dev);
  95. if (aux_ep_drv->remove)
  96. aux_ep_drv->remove(aux_ep);
  97. dev_pm_domain_detach(dev, true);
  98. }
  99. /**
  100. * dp_aux_ep_shutdown() - The shutdown function for the dp_aux_bus.
  101. * @dev: The device to shutdown.
  102. *
  103. * Calls through to the endpoint driver shutdown.
  104. */
  105. static void dp_aux_ep_shutdown(struct device *dev)
  106. {
  107. struct dp_aux_ep_driver *aux_ep_drv;
  108. if (!dev->driver)
  109. return;
  110. aux_ep_drv = to_dp_aux_ep_drv(dev->driver);
  111. if (aux_ep_drv->shutdown)
  112. aux_ep_drv->shutdown(to_dp_aux_ep_dev(dev));
  113. }
  114. static const struct bus_type dp_aux_bus_type = {
  115. .name = "dp-aux",
  116. .match = dp_aux_ep_match,
  117. .probe = dp_aux_ep_probe,
  118. .remove = dp_aux_ep_remove,
  119. .shutdown = dp_aux_ep_shutdown,
  120. };
  121. static ssize_t modalias_show(struct device *dev,
  122. struct device_attribute *attr, char *buf)
  123. {
  124. return of_device_modalias(dev, buf, PAGE_SIZE);
  125. }
  126. static DEVICE_ATTR_RO(modalias);
  127. static struct attribute *dp_aux_ep_dev_attrs[] = {
  128. &dev_attr_modalias.attr,
  129. NULL,
  130. };
  131. ATTRIBUTE_GROUPS(dp_aux_ep_dev);
  132. /**
  133. * dp_aux_ep_dev_release() - Free memory for the dp_aux_ep device
  134. * @dev: The device to free.
  135. */
  136. static void dp_aux_ep_dev_release(struct device *dev)
  137. {
  138. struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(dev);
  139. struct dp_aux_ep_device_with_data *aux_ep_with_data =
  140. container_of(aux_ep, struct dp_aux_ep_device_with_data, aux_ep);
  141. kfree(aux_ep_with_data);
  142. }
  143. static int dp_aux_ep_dev_modalias(const struct device *dev, struct kobj_uevent_env *env)
  144. {
  145. return of_device_uevent_modalias(dev, env);
  146. }
  147. static struct device_type dp_aux_device_type_type = {
  148. .groups = dp_aux_ep_dev_groups,
  149. .uevent = dp_aux_ep_dev_modalias,
  150. .release = dp_aux_ep_dev_release,
  151. };
  152. /**
  153. * of_dp_aux_ep_destroy() - Destroy an DP AUX endpoint device
  154. * @dev: The device to destroy.
  155. * @data: Not used
  156. *
  157. * This is just used as a callback by of_dp_aux_depopulate_bus() and
  158. * is called for _all_ of the child devices of the device providing the AUX bus.
  159. * We'll only act on those that are of type "dp_aux_bus_type".
  160. *
  161. * This function is effectively an inverse of what's in
  162. * of_dp_aux_populate_bus(). NOTE: since we only populate one child
  163. * then it's expected that only one device will match all the "if" tests in
  164. * this function and get to the device_unregister().
  165. *
  166. * Return: 0 if no error or negative error code.
  167. */
  168. static int of_dp_aux_ep_destroy(struct device *dev, void *data)
  169. {
  170. struct device_node *np = dev->of_node;
  171. if (dev->bus != &dp_aux_bus_type)
  172. return 0;
  173. if (!of_node_check_flag(np, OF_POPULATED))
  174. return 0;
  175. of_node_clear_flag(np, OF_POPULATED);
  176. of_node_put(np);
  177. device_unregister(dev);
  178. return 0;
  179. }
  180. /**
  181. * of_dp_aux_depopulate_bus() - Undo of_dp_aux_populate_bus
  182. * @aux: The AUX channel whose device we want to depopulate
  183. *
  184. * This will destroy the device that was created
  185. * by of_dp_aux_populate_bus().
  186. */
  187. void of_dp_aux_depopulate_bus(struct drm_dp_aux *aux)
  188. {
  189. device_for_each_child_reverse(aux->dev, NULL, of_dp_aux_ep_destroy);
  190. }
  191. EXPORT_SYMBOL_GPL(of_dp_aux_depopulate_bus);
  192. /**
  193. * of_dp_aux_populate_bus() - Populate the endpoint device on the DP AUX
  194. * @aux: The AUX channel whose device we want to populate. It is required that
  195. * drm_dp_aux_init() has already been called for this AUX channel.
  196. * @done_probing: Callback functions to call after EP device finishes probing.
  197. * Will not be called if there are no EP devices and this
  198. * function will return -ENODEV.
  199. *
  200. * This will populate the device (expected to be an eDP panel) under the
  201. * "aux-bus" node of the device providing the AUX channel (AKA aux->dev).
  202. *
  203. * When this function finishes, it is _possible_ (but not guaranteed) that
  204. * our sub-device will have finished probing. It should be noted that if our
  205. * sub-device returns -EPROBE_DEFER or is probing asynchronously for some
  206. * reason that we will not return any error codes ourselves but our
  207. * sub-device will _not_ have actually probed successfully yet.
  208. *
  209. * In many cases it's important for the caller of this function to be notified
  210. * when our sub device finishes probing. Our sub device is expected to be an
  211. * eDP panel and the caller is expected to be an eDP controller. The eDP
  212. * controller needs to be able to get a reference to the panel when it finishes
  213. * probing. For this reason the caller can pass in a function pointer that
  214. * will be called when our sub-device finishes probing.
  215. *
  216. * If this function succeeds you should later make sure you call
  217. * of_dp_aux_depopulate_bus() to undo it, or just use the devm version
  218. * of this function.
  219. *
  220. * Return: 0 if no error or negative error code; returns -ENODEV if there are
  221. * no children. The done_probing() function won't be called in that
  222. * case.
  223. */
  224. int of_dp_aux_populate_bus(struct drm_dp_aux *aux,
  225. int (*done_probing)(struct drm_dp_aux *aux))
  226. {
  227. struct device_node *bus = NULL, *np = NULL;
  228. struct dp_aux_ep_device *aux_ep;
  229. struct dp_aux_ep_device_with_data *aux_ep_with_data;
  230. int ret;
  231. /* drm_dp_aux_init() should have been called already; warn if not */
  232. WARN_ON_ONCE(!aux->ddc.algo);
  233. if (!aux->dev->of_node)
  234. return -ENODEV;
  235. bus = of_get_child_by_name(aux->dev->of_node, "aux-bus");
  236. if (!bus)
  237. return -ENODEV;
  238. np = of_get_next_available_child(bus, NULL);
  239. of_node_put(bus);
  240. if (!np)
  241. return -ENODEV;
  242. if (of_node_test_and_set_flag(np, OF_POPULATED)) {
  243. dev_err(aux->dev, "DP AUX EP device already populated\n");
  244. ret = -EINVAL;
  245. goto err_did_get_np;
  246. }
  247. aux_ep_with_data = kzalloc(sizeof(*aux_ep_with_data), GFP_KERNEL);
  248. if (!aux_ep_with_data) {
  249. ret = -ENOMEM;
  250. goto err_did_set_populated;
  251. }
  252. aux_ep_with_data->done_probing = done_probing;
  253. aux_ep = &aux_ep_with_data->aux_ep;
  254. aux_ep->aux = aux;
  255. aux_ep->dev.parent = aux->dev;
  256. aux_ep->dev.bus = &dp_aux_bus_type;
  257. aux_ep->dev.type = &dp_aux_device_type_type;
  258. aux_ep->dev.of_node = of_node_get(np);
  259. dev_set_name(&aux_ep->dev, "aux-%s", dev_name(aux->dev));
  260. ret = device_register(&aux_ep->dev);
  261. if (ret) {
  262. dev_err(aux->dev, "Failed to create AUX EP for %pOF: %d\n", np, ret);
  263. /*
  264. * As per docs of device_register(), call this instead
  265. * of kfree() directly for error cases.
  266. */
  267. put_device(&aux_ep->dev);
  268. goto err_did_set_populated;
  269. }
  270. return 0;
  271. err_did_set_populated:
  272. of_node_clear_flag(np, OF_POPULATED);
  273. err_did_get_np:
  274. of_node_put(np);
  275. return ret;
  276. }
  277. EXPORT_SYMBOL_GPL(of_dp_aux_populate_bus);
  278. static void of_dp_aux_depopulate_bus_void(void *data)
  279. {
  280. of_dp_aux_depopulate_bus(data);
  281. }
  282. /**
  283. * devm_of_dp_aux_populate_bus() - devm wrapper for of_dp_aux_populate_bus()
  284. * @aux: The AUX channel whose device we want to populate
  285. * @done_probing: Callback functions to call after EP device finishes probing.
  286. * Will not be called if there are no EP devices and this
  287. * function will return -ENODEV.
  288. *
  289. * Handles freeing w/ devm on the device "aux->dev".
  290. *
  291. * Return: 0 if no error or negative error code; returns -ENODEV if there are
  292. * no children. The done_probing() function won't be called in that
  293. * case.
  294. */
  295. int devm_of_dp_aux_populate_bus(struct drm_dp_aux *aux,
  296. int (*done_probing)(struct drm_dp_aux *aux))
  297. {
  298. int ret;
  299. ret = of_dp_aux_populate_bus(aux, done_probing);
  300. if (ret)
  301. return ret;
  302. return devm_add_action_or_reset(aux->dev,
  303. of_dp_aux_depopulate_bus_void, aux);
  304. }
  305. EXPORT_SYMBOL_GPL(devm_of_dp_aux_populate_bus);
  306. int __dp_aux_dp_driver_register(struct dp_aux_ep_driver *drv, struct module *owner)
  307. {
  308. drv->driver.owner = owner;
  309. drv->driver.bus = &dp_aux_bus_type;
  310. return driver_register(&drv->driver);
  311. }
  312. EXPORT_SYMBOL_GPL(__dp_aux_dp_driver_register);
  313. void dp_aux_dp_driver_unregister(struct dp_aux_ep_driver *drv)
  314. {
  315. driver_unregister(&drv->driver);
  316. }
  317. EXPORT_SYMBOL_GPL(dp_aux_dp_driver_unregister);
  318. static int __init dp_aux_bus_init(void)
  319. {
  320. int ret;
  321. ret = bus_register(&dp_aux_bus_type);
  322. if (ret)
  323. return ret;
  324. return 0;
  325. }
  326. static void __exit dp_aux_bus_exit(void)
  327. {
  328. bus_unregister(&dp_aux_bus_type);
  329. }
  330. subsys_initcall(dp_aux_bus_init);
  331. module_exit(dp_aux_bus_exit);
  332. MODULE_AUTHOR("Douglas Anderson <dianders@chromium.org>");
  333. MODULE_DESCRIPTION("DRM DisplayPort AUX bus");
  334. MODULE_LICENSE("GPL v2");